Skip to content

Commit

Permalink
Set SO_REUSEADDR on epoll tcp listener sockets (#4544)
Browse files Browse the repository at this point in the history
Unix is a bit more strict about TIME_WAIT state, and actually puts any sockets that have had a valid accept() called on them into the TIME_WAIT state.

This makes writing a listener app difficult, as if that ever crashes the bind() will fail for the next few minutes.

Pretty much all other TCP libraries set SO_REUSEADDR (Including libuv, which is what our app has used before). Libuv sets it on all TCP sockets, but its generally less required on client sockets, as they rarely actually specify a local port.
  • Loading branch information
ThadHouse committed Sep 11, 2024
1 parent 7d8f18d commit 6976ecf
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/platform/datapath_epoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,29 @@ CxPlatSocketContextInitialize(
goto Exit;
}
}
} else if (SocketType == CXPLAT_SOCKET_TCP_LISTENER) {
//
// Set SO_REUSEADDR on listener sockets to avoid
// TIME_WAIT state on shutdown.
//
Option = TRUE;
Result =
setsockopt(
SocketContext->SocketFd,
SOL_SOCKET,
SO_REUSEADDR,
(const void*)&Option,
sizeof(Option));
if (Result == SOCKET_ERROR) {
Status = errno;
QuicTraceEvent(
DatapathErrorStatus,
"[data][%p] ERROR, %u, %s.",
Binding,
Status,
"setsockopt(SO_REUSEPORT) failed");
goto Exit;
}
}

CxPlatCopyMemory(&MappedAddress, &Binding->LocalAddress, sizeof(MappedAddress));
Expand Down

0 comments on commit 6976ecf

Please sign in to comment.