Skip to content

[Windows] Prevent socket callback from falsely detecting a connection close #578

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/event/event_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,15 +364,19 @@ _dispatch_socket_callback(PTP_CALLBACK_INSTANCE inst, void *context,
} else if (lNetworkEvents & FD_READ) {
ioctlsocket(sock, FIONREAD, &dwBytesAvailable);
}
if (lNetworkEvents & FD_READ) {
// ioctlsocket could legally return 0 and that doesn't mean the socket is closed.
// Here and below we have to double-check dwBytesAvailable and make sure we don't
// wake up read/write handlers with 0 value for live (not closed) socket, because
// that would force handler to shut down.
if (((lNetworkEvents & FD_READ) && dwBytesAvailable > 0) || (lNetworkEvents & FD_CLOSE)) {
Copy link
Member

Choose a reason for hiding this comment

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

This seems plausible to occur. But FD_READ is already set when FD_CLOSE is set, so this is rather confusing.

_dispatch_muxnote_retain(dmn);
if (!PostQueuedCompletionStatus(hPort, dwBytesAvailable,
(ULONG_PTR)DISPATCH_PORT_SOCKET_READ, (LPOVERLAPPED)dmn)) {
DISPATCH_INTERNAL_CRASH(GetLastError(),
"PostQueuedCompletionStatus");
}
}
if (lNetworkEvents & FD_WRITE) {
if (((lNetworkEvents & FD_WRITE) && dwBytesAvailable > 0) || (lNetworkEvents & FD_CLOSE)) {
Copy link
Member

Choose a reason for hiding this comment

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

This doesn't make sense - dwBytesAavailable is guaranteed 1 if only a write event occurred. This condition doesn't seem correct. It should be either a write or a close event occurred I believe.

_dispatch_muxnote_retain(dmn);
if (!PostQueuedCompletionStatus(hPort, dwBytesAvailable,
(ULONG_PTR)DISPATCH_PORT_SOCKET_WRITE, (LPOVERLAPPED)dmn)) {
Expand Down