Set SO_REUSEADDR on epoll tcp listener sockets (#4544)

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.
This commit is contained in:
Thad House 2024-09-11 14:25:33 -07:00 коммит произвёл GitHub
Родитель 7d8f18d509
Коммит 6976ecff74
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
1 изменённых файлов: 23 добавлений и 0 удалений

Просмотреть файл

@ -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));