Fix socket watching read and write conflict. #4430
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The TCPSocketStarboard and UDPSocketStarboard classes used one single socket watcher for watching both reading and writing without tracking which interests were being tracked.
A single watcher was used because the underlying platform code does not allow watching the same descriptor twice, but the existing logic for determining when to stop watching was fragile for moments when a socket was being watched for both reading and writing. This tracks the interests explicitly to make sure the watcher is registered with the correct interests, and registered when no interests are left.
Before this change, if a socket needed a write interest at the same time as a read interest, both interests would remain active and with their possible callbacks until they both were satisfied.
This may fix the 3rd top crasher, and should improve performance by avoiding spurious callbacks for obsolete interests.
Note: Socket watching is done when a socket either is not ready for sending or has no data to receive. Socket watching serves to get a callback when that condition changes. The socket watching is registered with an interest, indicating whether the registrant is interested in reading or writing from/to the socket.
b/361151407