Repository navigation
fix(iocp): hold the listen port exclusively and run one Listen cycle at a time - #640
Draft
edsontmarques wants to merge 1 commit into
Draft
edsontmarques wants to merge 1 commit into
edsontmarques wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
On Windows, the IOCP provider's listen cycle had four defects:
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.bindorlistendid not say why. The message gave neither the port nor the error code.Listenoverloads that take callbacks did not run them.Listendid not clean up. It left Winsock started and could leave the socket open.Change
SO_EXCLUSIVEADDRUSE, and the return ofsetsockoptis checked.socket,setsockopt,bindorlistenreports the port and the code. The code is read beforeclosesocket. 10048 (port in use) and 10013 (port denied by Windows) each have their own message.WSAStartupreleases what was opened and balancesWSACleanup, so the nextListenin the same process finds the port free.OnListenandOnStopListen,nilincluded, as the HttpSys and Console providers do.Listencycle at a time.Listencalled while another cycle is up, starting or stopping is refused, and the server that is listening keeps running.Listenthread returns only after the release has ended.Tests
tests/src/tests/Tests.Horse.Provider.IOCP.Listen.pasis part ofIOCPUnitTestsand runs on real sockets on port 19266. It checks:TIME_WAITon the port;Listencycle.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 (
masterplus 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