Skip to content

fix(iocp): hold the listen port exclusively and run one Listen cycle at a time - #640

Draft
edsontmarques wants to merge 1 commit into
HashLoad:masterfrom
edsontmarques:fix/iocp-listen-cycle
Draft

edsontmarques wants to merge 1 commit into
HashLoad:masterfrom
edsontmarques:fix/iocp-listen-cycle

Conversation

@edsontmarques

Copy link
Copy Markdown

Problem

On Windows, the IOCP provider's listen cycle had four defects:

  • The listen socket used SO_REUSEADDR. On Windows this lets a second socket bind a port that another one is already listening on. A second server came up on the same port with no error, and the connections were split between the two.
  • A failed bind or listen did not say why. The message gave neither the port nor the error code.
  • The callbacks were ignored. The Listen overloads that take callbacks did not run them.
  • A failed Listen did not clean up. It left Winsock started and could leave the socket open.

Change

  • Exclusive port. The listen socket uses SO_EXCLUSIVEADDRUSE, and the return of setsockopt is checked.
  • Error messages. A failure of socket, setsockopt, bind or listen reports the port and the code. The code is read before closesocket. 10048 (port in use) and 10013 (port denied by Windows) each have their own message.
  • Clean state after a failure. A failure after WSAStartup releases what was opened and balances WSACleanup, so the next Listen in the same process finds the port free.
  • Callbacks. The overloads that take callbacks set OnListen and OnStopListen, nil included, as the HttpSys and Console providers do.
  • One Listen cycle at a time.
    • A Listen called while another cycle is up, starting or stopping is refused, and the server that is listening keeps running.
    • The refusal comes before the call changes the port, the host or the callbacks.
    • The start and the release of the listen resources run under one lock, and the Listen thread returns only after the release has ended.
    • A listen callback that raises takes the server down with it.

Tests

tests/src/tests/Tests.Horse.Provider.IOCP.Listen.pas is part of IOCPUnitTests and runs on real sockets on port 19266. It checks:

  • the busy port and the exclusive port;
  • the messages;
  • listening again after a graceful stop and after the process is killed, with a server-side TIME_WAIT on the port;
  • the callbacks;
  • the clean state after a failure;
  • the Listen cycle.

tests/iocp-listen/ has the holder process, the runner and the README. The runner can run each check in a process of its own (-Tests). The README has the negative control against the previous source.

One check is ignored: StopWithRequestsArrivingReturns. A stop made while keep-alive requests keep arriving can wait forever for a worker. This also happens without this change, and it will be fixed separately.

Measured on Windows with Delphi 10.2 Tokyo, on this branch (master plus this commit): 15 tests found, 14 passed, 1 ignored, no leaks. Against the previous source, every check that has a negative control fails, as described in the README.

These tests need a licensed Delphi, so the FPC CI job does not run them.

🤖 Generated with Claude Code

…at a time

The listen socket used SO_REUSEADDR, which on Windows lets a second
socket bind a port another one is already listening on: a second
server came up on the same port with no error and the connections were
split between them. It now uses SO_EXCLUSIVEADDRUSE, and the return of
setsockopt is checked.

A failed socket, setsockopt, bind or listen now says which port and
which code (read before closesocket), with its own text for 10048 (port
in use) and 10013 (port denied by Windows). A failure after WSAStartup
releases what was opened and balances WSACleanup, so the next Listen in
the same process finds the port free.

The Listen overloads that take callbacks ignored them. They now set
OnListen and OnStopListen, nil included, as HttpSys and Console do.

One Listen cycle runs at a time. A Listen while another cycle is up,
starting or stopping is refused before it changes the port, the host
or the callbacks, and the server already listening keeps running. The
start and the release of the listen resources run under one lock, and
the Listen thread waits for the release to end, so that a release never
runs over the resources of the next cycle. A listen callback that
raises takes the server down with it.

tests/src/tests/Tests.Horse.Provider.IOCP.Listen.pas, in IOCPUnitTests,
checks the busy and the exclusive port, the messages, listening again
after a graceful stop and after a killed process (with a server-side
TIME_WAIT on the port), the callbacks, the clean state and the Listen
cycle. tests/iocp-listen has the holder process, the runner (which can
run each check in a process of its own) and the negative controls.
The check of a stop with requests arriving is ignored: that stop can
hang, before this change too, and it has a fix of its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants