Repository navigation
fix(server): release inherited Windows sockets with closesocket - #4872
Conversation
os.NewFile closes Windows handles with CloseHandle, which leaves stale Winsock state attached to recycled socket handles. Use closesocket for adopted sockets and retain their file wrappers so finalizers cannot close unrelated handles later. Preserve Unix cleanup and non-socket error-path cleanup. Add Windows subprocess coverage for handle reuse after TCP adoption and UDP rejection, and strengthen listener lifetime and address checks without changing the authentication assertions. Document fd:// ownership. Validated task build/test/lint, 1000 focused repetitions, 100 focused race repetitions, 20 full server race/shuffle repetitions, Windows amd64/arm64 test compilation and Windows-targeted lint. Native Windows execution remains outstanding. The installed Go 1.27.3 also retains a separate temporary-socket CloseHandle defect fixed upstream in 00be0f2640a1; this change addresses application-owned sockets only. Signed-off-by: David Gageot <david.gageot@docker.com> Assisted-By: OpenAI
There was a problem hiding this comment.
🔵 Needs a closer look
Native Windows runtime validation is still needed for Winsock cleanup, finalizer safety, handle reuse, and inherited-listener authentication, since the reported Windows checks only compiled and linted the tests.
No open issues 🎉
What changed in this PR
Fixes inherited listener cleanup on Windows by using closesocket, preventing stale Winsock state when handles are reused. Unix cleanup remains unchanged.
Changes:
- Adds platform-specific cleanup with a non-socket fallback and pins socket wrappers to prevent finalizer-driven double closes.
- Strengthens listener ownership, address, connectivity, and authentication tests; adds Windows subprocess coverage.
- Documents inherited descriptor ownership and authentication behavior.
Windows-specific tests still require native Windows CI validation.
| File | Description |
|---|---|
pkg/server/security_test.go |
Verifies inherited listener address and authentication after closing the original listener. |
pkg/server/listen.go |
Shares wrapper pinning and delegates inherited descriptor cleanup by platform. |
pkg/server/listen_windows.go |
Uses closesocket, with ordinary file cleanup for non-sockets. |
pkg/server/listen_unix.go |
Preserves ordinary file-close behavior on Unix. |
pkg/server/listen_test.go |
Strengthens ownership, address preservation, and connectivity checks. |
pkg/server/listen_fd_windows_test.go |
Adds non-socket cleanup and isolated TCP/UDP handle-reuse tests. |
docs/features/api-server/index.md |
Documents fd://N ownership and inherited listener behavior. |
Review effort: Balanced
aheritier
left a comment
There was a problem hiding this comment.
Head verified unchanged at 1619f3a. The PR is not draft and has no merge conflict.
Reviewed all seven changed paths against the stated requirements: Windows socket cleanup and non-socket fallback, unchanged Unix behavior, descriptor ownership, address preservation, connectivity, authentication, and documentation. The linked #4869 failure and upstream Go fix supply background, not additional delivery requirements. No additional runtime defect demonstrated. See inline comment.
CI: run 42622023441 completed with test-windows and required ci / gate failing. Linux tests, lint, cross-compilation, TypeScript, and image checks succeeded. Windows artifacts confirm TestListen_FD and both inherited-listener authentication cases passed natively. The PR is not merge-ready; approval is withheld pending the correction and green required CI.
Prior threads: none; paginated issue comments, review comments, and thread state were read completely. Approval review 5826232393 predates the Windows failure. Review 5826245081's native-Windows-validation concern was reassessed against the completed Windows run.
Assisted-By: OpenAI
Fix the flaky
TestServerInheritedListenerAuthobserved in #4869's Windows job. On Windows,os.NewFilecloses a socket handle withCloseHandleinstead ofclosesocket, leaving stale Winsock state when the handle is reused.Use platform-specific cleanup, with an ordinary file-close fallback for non-sockets; Unix behavior is unchanged. Since
os.Filehas no public detach API, pin the wrapper to prevent its finalizer from closing a reused handle. This retains one small wrapper per adopted socket for the process lifetime.Strengthen listener ownership, address, and connectivity checks, and add Windows subprocess stress coverage for TCP adoption, UDP rejection, and non-socket cleanup. The bounded handle-reuse test skips if Windows does not reuse the target handle, avoiding a new flaky hard failure.
Validation:
task build,task test,task lint; hostgo test -race -shuffle=on ./pkg/server -run '^(TestListen_FD|TestServerInheritedListenerAuth)' -count=100; Windows amd64 and arm64 server test binaries compiled; Windows-targeted golangci-lint passed. Windows-specific tests were compiled/linted but not executed locally on the macOS host; native Windows CI validation is needed.Go 1.27.3 also has a separate upstream temporary-socket
CloseHandledefect, fixed by Go commit 00be0f2640a11f9964c11836fe3c64be29d32f1a. This application patch cannot fix that independent source.