Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak locking around Conn.sender #401

Merged
merged 3 commits into from
Dec 27, 2022
Merged

Conversation

zenhack
Copy link
Contributor

@zenhack zenhack commented Dec 27, 2022

Contrary to the comment, there was no actual need to not be holding the lock when calling answer.sendMsg, and flipping the contract allows us to remove a superfluous unlock/lock pair.

This also changes the send queue back to using spsc and protects it with Conn.lk, since we no longer add to the send queue when we're not holding the connection lock anyway. We'd tried this in the past, but backed it out due to the now-removed unlock/lock pair.

Doing this required splitting it into two separate fields, so the receive end could still be outside Conn.lk.

We now expect the caller to be holding the lock. This makes more sense
since the other place we use c.sender is also holding the lock.

This lets us get rid of another Unlock/Lock pair, which was unnecessary.

Future work includes moving c.sender inside c.lk, since we never
actually send when not holding the lock anymore.
Specifically:

- Split it into separate fields for Rx and Tx
- Move the send end inside Conn.lk
- Use spsc instead of mpsc, now that it's protected by Conn.lk
- Removed an unlock/lock pair, which was now incorrect since we changed
  the contract on answer.sendMsg to require holding the lock. This also
  removes a deadlock in the event that the finish had already been
  received, since we were re-locking twice; both calls to Lock() have been
  removed.
- Removed a duplicate call to ans.destory; in the case where the finish
  had already been received we would have (but for the deadlock) called
  this twice, once in the select and once at the end of the function. I
  suspect there was supposed to be a return after the first call, but it's
  cleaner to just do it in one place anyway.
@zenhack
Copy link
Contributor Author

zenhack commented Dec 27, 2022

Tacked on one more fix; see the commit message on the last commit.

Copy link
Collaborator

@lthibault lthibault left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I really like the way this lock-reduction effort is panning out overall 👍

Quick note: I'm going to let you hit the merge button until we get around to fixing CI. I know most of these failures are known/probably-harmless issues, but I'd rather leave the final decision to merge to you.

@zenhack zenhack merged commit cbfcdc8 into capnproto:main Dec 27, 2022
@zenhack zenhack deleted the sender-lock branch December 27, 2022 23:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants