Skip to content

event: handle socket events more precisely #581

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

Merged
merged 1 commit into from
Oct 12, 2021

Conversation

compnerd
Copy link
Member

This adjusts the socket event to correct a potential spurious wakeup
which can incorrectly mark the socket as closed.

When working with a SOCK_STREAM socket, FIONREAD returns the total
amount of data that can be read in a single receive operation.
Normally, this is the same amount of data queued on the socket, but this
is not guaranteed as a data-stream is byte-oriented. Additionally, a
read on a separate thread may have drained the data between the event
trigger and the ioctl to read the available bytes. Only wake up the
handler if there are bytes to read.

In order to differentiate between the trigger for a close event and a
read-event, we explicitly check the close and the read or write event.
This allows waking the handlers one last time after a socket is closed.

This adjusts the socket event to correct a potential spurious wakeup
which can incorrectly mark the socket as closed.

When working with a `SOCK_STREAM` socket, `FIONREAD` returns the total
amount of data that can be read in a single receive operation.
Normally, this is the same amount of data queued on the socket, but this
is not guaranteed as a data-stream is byte-oriented.  Additionally, a
read on a separate thread may have drained the data between the event
trigger and the ioctl to read the available bytes.  Only wake up the
handler if there are bytes to read.

In order to differentiate between the trigger for a close event and a
read-event, we explicitly check the close and the read or write event.
This allows waking the handlers one last time after a socket is closed.
@compnerd
Copy link
Member Author

@swift-ci please test

@compnerd
Copy link
Member Author

CC: @rokhinip

@compnerd compnerd merged commit ceabeea into swiftlang:main Oct 12, 2021
@compnerd compnerd deleted the zero-read-close branch October 12, 2021 16:13
@lxbndr lxbndr mentioned this pull request Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant