Skip to content

fix(server): release inherited Windows sockets with closesocket - #4872

Merged
dgageot merged 2 commits into
mainfrom
fix/windows-inherited-listener
Nov 9, 2026
Merged

dgageot merged 2 commits into
mainfrom
fix/windows-inherited-listener

Conversation

@dgageot

@dgageot dgageot commented Nov 9, 2026

Copy link
Copy Markdown
Member

Fix the flaky TestServerInheritedListenerAuth observed in #4869's Windows job. On Windows, os.NewFile closes a socket handle with CloseHandle instead of closesocket, 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.File has 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; host go 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 CloseHandle defect, fixed by Go commit 00be0f2640a11f9964c11836fe3c64be29d32f1a. This application patch cannot fix that independent source.

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
@dgageot
dgageot requested a review from a team as a code owner November 9, 2026 13:02
Copilot AI balanced review requested due to automatic review settings November 9, 2026 13:02
rumpl
rumpl previously approved these changes Nov 9, 2026
@dgageot
dgageot enabled auto-merge November 9, 2026 13:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 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 aheritier added area/api For features/issues/fixes related to the usage of the cagent API area/docs Documentation changes kind/fix PR fixes a bug (maps to fix:). Use on PRs only. labels Nov 9, 2026

@aheritier aheritier left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Comment thread pkg/server/listen_fd_windows_test.go Resolved
Copilot AI balanced review requested due to automatic review settings November 9, 2026 16:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔵 Needs a closer look

Native Windows testing is still needed to verify Winsock cleanup, non-socket fallback, and recycled-handle safety, which cross-compilation and linting cannot establish.

No open issues 🎉

Review effort: Balanced

@dgageot
dgageot requested a review from aheritier November 9, 2026 16:25
@dgageot
dgageot added this pull request to the merge queue Nov 9, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 9, 2026
@dgageot
dgageot added this pull request to the merge queue Nov 9, 2026
Merged via the queue into main with commit 92fb0d2 Nov 9, 2026
24 checks passed
@dgageot
dgageot deleted the fix/windows-inherited-listener branch November 9, 2026 19:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/api For features/issues/fixes related to the usage of the cagent API area/docs Documentation changes kind/fix PR fixes a bug (maps to fix:). Use on PRs only.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants