Skip to content

pyln-testing: wait for listen port release before starting a node#9355

Open
ksedgwic wants to merge 1 commit into
ElementsProject:masterfrom
ksedgwic:fix-restart-rebind-race
Open

pyln-testing: wait for listen port release before starting a node#9355
ksedgwic wants to merge 1 commit into
ElementsProject:masterfrom
ksedgwic:fix-restart-rebind-race

Conversation

@ksedgwic

Copy link
Copy Markdown
Collaborator

Fixes #9354 : when a test restarts a node, the previous connectd --
slow to exit under valgrind -- can still hold the listen socket, so
the new connectd fails with 'Address already in use' and the test
times out waiting for 'Server started with public key'.

Before launching lightningd, bind-probe the node's port with
SO_REUSEADDR and wait until the bind succeeds.

Validated by holding a stopped node's port from the test process and
restarting: without this change that reproduces the CI failure
exactly (same BROKEN bind message and timeout); with it, start waits
out the holder and proceeds.

Changelog-None

A node's connectd is a separate process holding the listen socket,
and on shutdown it exits on its own schedule after lightningd itself
is gone -- under valgrind, its teardown can lag by tens of seconds.
If a test restarts the node in that window, the new connectd fails
with 'Address already in use', lightningd exits, and the test times
out waiting for 'Server started with public key'.  Seen in CI in
test_emergencyrecoverpenaltytxn, where the port was still held 37
seconds after the old connectd began shutting down.

The filesystem port locks don't cover this: they keep other workers
from reserving the port, but the restarting node owns its reservation
both times; nothing waits for the old connectd to actually release
the socket.

Before launching lightningd, bind-probe the node's port (with
SO_REUSEADDR, matching connectd, so TIME_WAIT sockets don't count as
in use) and only proceed once the bind succeeds.

Fixes: ElementsProject#9354
Changelog-None
@ksedgwic
ksedgwic requested a review from cdecker as a code owner July 24, 2026 20:05
@ksedgwic
ksedgwic requested review from daywalker90 and removed request for cdecker July 24, 2026 20:06
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.

CI flake: node restart races the old connectd's exit ("Address already in use")

1 participant