Skip to content

Commit

Permalink
stdsock: Fix a bug in windows close
Browse files Browse the repository at this point in the history
We must call shutdown before closing the socket.  If we do not, it
leaves the socket open on the remote if there is another connection to
the same system.  The socket will only close when all connections to
that system close.  Windows is broken in so many ways.

Signed-off-by: Corey Minyard <[email protected]>
  • Loading branch information
cminyard committed Dec 18, 2024
1 parent 1c74c72 commit faf855e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/gensio_stdsock.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,15 @@ close_socket(struct gensio_os_funcs *o, int fd)

assert(fd != -1);
#ifdef _WIN32
err = closesocket(fd);
/*
* We must call shutdown before closing the socket. If we do not, it
* leaves the socket open on the remote if there is another connection
* to the same system. The socket will only close when all connections
* to that system close. Windows is broken in so many ways.
*/
err = shutdown(fd, SD_BOTH);
if (!err)
err = closesocket(fd);
#else
err = close(fd);
#endif
Expand Down
4 changes: 3 additions & 1 deletion lib/gensio_win.c
Original file line number Diff line number Diff line change
Expand Up @@ -1284,8 +1284,10 @@ win_iod_socket_clean(struct gensio_iod_win *wiod)
WSACloseEvent(swiod->sockev);
if (swiod->wakeev != WSA_INVALID_EVENT)
WSACloseEvent(swiod->wakeev);
if (wiod->fd != -1)
if (wiod->fd != -1) {
shutdown(wiod->fd, SD_BOTH);
closesocket(wiod->fd);
}
}

static int
Expand Down

0 comments on commit faf855e

Please sign in to comment.