Every test project binds its servers on port 0 and reads the bound port instead of reserving and releasing one #362

Closed
opened 2026-11-06 19:51:56 -05:00 by claude · 0 comments
Owner
  • Draft: none (found 2026-11-06 while merging epic #8 part 2)
  • Priority: medium (reserve-and-release races cause CI flakes, more often on Windows and on the loaded asgard runner)
  • Agent: coder (protocol-coder for the transport projects)
  • Depends on: epic #8 part 2 (it adds BoundPort on ListenSocketSet, UdpDnsListener and TcpDnsListener)
  • Decisions: none

Why

AGENTS.md ("Test") asks every test project to start its server on port 0 and read the port it bound, instead of reserving a free port, releasing it and binding again. Another process can take the port in between. The rule says to convert a project when a change touches it, so projects nobody touched still use the old pattern.

Epic #8 part 2 converts Ninlil.Dns.Protocol.Plain.Tests and Ninlil.Dns.Server.Core.Tests. Their remaining calls are justified in comments, because several sockets share one port number. Ninlil.Dhcp.Server.Tftp.Tests uses TftpServer.BoundPort.

Remaining call sites of GetFreePort / GetFreeUdpPort / GetFreeTcpPort, with call counts, at d1a83213:

  • Ninlil.Dhcp.Protocol.Dhcpv4.Tests: Dhcpv4ListenerResilienceTests (2), Dhcpv4LiveServiceControlTests (2), Dhcpv4UdpRoundTripTests (4)
  • Ninlil.Dhcp.Protocol.Dhcpv6.Tests: Dhcpv6ListenerResilienceTests (2), Dhcpv6LiveServiceControlTests (2), Dhcpv6UdpRoundTripTests (2)
  • Ninlil.Dhcp.Server.Relay.Tests: Dhcpv4RelayAgentTests (15), Dhcpv4RelayRoundTripTests (3), Dhcpv6RelayAgentTests (2), Dhcpv6RelayRoundTripTests (3)
  • Ninlil.Dns.Protocol.DnsCrypt.Tests: DnsCryptDiscoveryLimitTests, DnsCryptExpiredCertificateTests, DnsCryptListenAddressTests, DnsCryptListenerTestFixture, DnsCryptProviderNameTests (1 each)
  • Ninlil.Dns.Protocol.DnsOverHttps.Tests: DnsOverHttpsCacheControlTests (1), DnsOverHttpsDropTests (1), DnsOverHttpsHostedServiceTests (2), DnsOverHttpsRequestBytesTests (1)
  • Ninlil.Dns.Protocol.DnsOverQuic.Tests: DnsOverQuicListenerTests (1)
  • Ninlil.Dns.Protocol.DnsOverTls.Tests: DnsOverTlsListenerTests (1)
  • Ninlil.Dns.Server.Authoritative.Tests: Forwarding/SecondaryForwarderZoneTransferTests (1)
  • Ninlil.Dns.Server.Forwarding.ClientProtocols.Tests: PlainForwarderClientTests (1)
  • Ninlil.Web.Tests: DhcpServiceControlControllerTests (1), StartupRefusalProcessTests (3), TransportSettingsHotReloadTests (1). The environment-variable ports in NinlilDnsWebApplicationFactory stay, as AGENTS.md says.

What changes

  • For each call site, start the server on port 0 and read the port it bound. Where a listener does not expose it yet, add a BoundPort like the one in part 2, for example on the DHCPv4/v6 listeners, the relay agents, DNSCrypt, DoH, DoQ and DoT.
  • Where port 0 cannot work, for example several sockets that must share one number, a deliberately unused port for a "nothing listens here" test, or a port the host must know before it starts, keep the call and say why in a comment. Where a bind can still race, retry a bounded number of times on AddressInUse, as AGENTS.md describes.
  • Split the work by project family if it gets large: DHCP (DHCPv4, DHCPv6 and the relay), the encrypted DNS transports, and the rest. One PR per family is fine. List each in this issue.

Acceptance

  • No call site in the projects above reserves and releases a port without a comment saying why port 0 cannot work.
  • The tests of every touched project pass on Linux (CI container) and natively on Windows (Lofnheim).
  • Every item in Before you finish that applies is done.
- **Draft:** none (found 2026-11-06 while merging epic #8 part 2) - **Priority:** medium (reserve-and-release races cause CI flakes, more often on Windows and on the loaded asgard runner) - **Agent:** coder (protocol-coder for the transport projects) - **Depends on:** epic #8 part 2 (it adds `BoundPort` on `ListenSocketSet`, `UdpDnsListener` and `TcpDnsListener`) - **Decisions:** none ### Why AGENTS.md ("Test") asks every test project to start its server on port 0 and read the port it bound, instead of reserving a free port, releasing it and binding again. Another process can take the port in between. The rule says to convert a project when a change touches it, so projects nobody touched still use the old pattern. Epic #8 part 2 converts `Ninlil.Dns.Protocol.Plain.Tests` and `Ninlil.Dns.Server.Core.Tests`. Their remaining calls are justified in comments, because several sockets share one port number. `Ninlil.Dhcp.Server.Tftp.Tests` uses `TftpServer.BoundPort`. Remaining call sites of `GetFreePort` / `GetFreeUdpPort` / `GetFreeTcpPort`, with call counts, at d1a83213: - `Ninlil.Dhcp.Protocol.Dhcpv4.Tests`: Dhcpv4ListenerResilienceTests (2), Dhcpv4LiveServiceControlTests (2), Dhcpv4UdpRoundTripTests (4) - `Ninlil.Dhcp.Protocol.Dhcpv6.Tests`: Dhcpv6ListenerResilienceTests (2), Dhcpv6LiveServiceControlTests (2), Dhcpv6UdpRoundTripTests (2) - `Ninlil.Dhcp.Server.Relay.Tests`: Dhcpv4RelayAgentTests (15), Dhcpv4RelayRoundTripTests (3), Dhcpv6RelayAgentTests (2), Dhcpv6RelayRoundTripTests (3) - `Ninlil.Dns.Protocol.DnsCrypt.Tests`: DnsCryptDiscoveryLimitTests, DnsCryptExpiredCertificateTests, DnsCryptListenAddressTests, DnsCryptListenerTestFixture, DnsCryptProviderNameTests (1 each) - `Ninlil.Dns.Protocol.DnsOverHttps.Tests`: DnsOverHttpsCacheControlTests (1), DnsOverHttpsDropTests (1), DnsOverHttpsHostedServiceTests (2), DnsOverHttpsRequestBytesTests (1) - `Ninlil.Dns.Protocol.DnsOverQuic.Tests`: DnsOverQuicListenerTests (1) - `Ninlil.Dns.Protocol.DnsOverTls.Tests`: DnsOverTlsListenerTests (1) - `Ninlil.Dns.Server.Authoritative.Tests`: Forwarding/SecondaryForwarderZoneTransferTests (1) - `Ninlil.Dns.Server.Forwarding.ClientProtocols.Tests`: PlainForwarderClientTests (1) - `Ninlil.Web.Tests`: DhcpServiceControlControllerTests (1), StartupRefusalProcessTests (3), TransportSettingsHotReloadTests (1). The environment-variable ports in `NinlilDnsWebApplicationFactory` stay, as AGENTS.md says. ### What changes - For each call site, start the server on port 0 and read the port it bound. Where a listener does not expose it yet, add a `BoundPort` like the one in part 2, for example on the DHCPv4/v6 listeners, the relay agents, DNSCrypt, DoH, DoQ and DoT. - Where port 0 cannot work, for example several sockets that must share one number, a deliberately unused port for a "nothing listens here" test, or a port the host must know before it starts, keep the call and say why in a comment. Where a bind can still race, retry a bounded number of times on `AddressInUse`, as AGENTS.md describes. - Split the work by project family if it gets large: DHCP (DHCPv4, DHCPv6 and the relay), the encrypted DNS transports, and the rest. One PR per family is fine. List each in this issue. ### Acceptance - [ ] No call site in the projects above reserves and releases a port without a comment saying why port 0 cannot work. - [ ] The tests of every touched project pass on Linux (CI container) and natively on Windows (Lofnheim). - [ ] Every item in [Before you finish](https://git.lantea.one/lantea-one/ninlil/src/branch/main/AGENTS.md#before-you-finish) that applies is done.
Sign in to join this conversation.
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
lantea-one/ninlil#362
No description provided.