[Windows] Prevent socket callback from falsely detecting a connection close #578
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.
This fixes a hard-to-reproduce issue we did observe in our live product (powered by Swift on Windows). The application communicates with the backend a lot, and some network requests were randomly timing out. All attempts to reproduce that behavior synthetically failed, but in the end we got some low but stable repro rate and had a chance to debug this.
It appeared that sometimes Dispatch catches
FD_READ
event, butioctlsocket
says that we have nothing to read at the moment of event processing. That is perfectly ok, non-zero result is not guaranteed there, as the reading could occur asynchronously.But 0 is also used as a special value in
_dispatch_event_merge
as a marker of a closed socket. Such sockets are exclude from any further handling, making the connection hang.The suggested solution avoids read/write processing with 0 available bytes, except for
FD_CLOSE
event, when we really need to poke all handlers to actually shut down the socket. TheFD_READ
event with 0 bytes becomes a no-op and doesn't do anything.We applied this fix locally more than a year ago and since then the connection behavior seems to be stable.