Skip to content

fix: retry managed marimo startup after losing its port - #122

Merged
peter-gy merged 2 commits into
mainfrom
pgy/managed-port-race
Oct 25, 2026
Merged

peter-gy merged 2 commits into
mainfrom
pgy/managed-port-race

Conversation

@peter-gy

@peter-gy peter-gy commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

ManagedServer picks a free loopback port, closes the probe socket, and hands the port to marimo, which binds it only after its imports finish. Another process can take the port in that window. uvicorn then exits with an address-in-use error and the build fails with a bare the managed marimo server exited during startup. Parallel test workers hit this in CI, and so can several builds or captures running at once on one machine.

When an exited server's log reports an address in use (errno 98, 48, or 10048, or address already in use), the owner now stops that process tree through its usual cleanup, truncates the log, and starts again with a fresh port and a fresh access token. Readiness polling may already have sent the old token to whoever held the port. Five attempts and the existing startup deadline bound the retries. Every other early exit still fails at once.

Startup failures that read the server log now end their message with the exit code and the last three log lines, redacted, for example exited during startup with code 1: ... | AttributeError: module 'uvicorn' has no attribute '__version__'. The redacted log in details.log now keeps its end, where the failure is reported.

ManagedServer picks a free loopback port and closes the socket before
marimo binds it, seconds later after imports. Another process can take
the port in between, uvicorn exits with an address-in-use error, and the
build fails with an opaque server_start_failed.

When an exited server's log reports an address in use, stop its process
tree, truncate the log, and start again on a fresh port, bounded by five
attempts and one startup deadline. Every other early exit still fails at
once. Startup failures now append the exit code and the last server log
lines to the message, so CLI and test output name the cause.
Copilot AI balanced review requested due to automatic review settings October 25, 2026 17:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

Readiness polling sends the bearer token to whichever process holds the
port, so a server that lost its port race also lost its token. Each
attempt now starts with a fresh token.

The retry cleanup runs inside the failed attempt's handler, so a
cleanup failure keeps the port error as its context. Startup
diagnostics drop a line cut by the 8 KiB window, which could otherwise
leave an unredacted token fragment, and details.log keeps the end of
the rendered log, where the failure is reported.
@peter-gy
peter-gy merged commit 1292c5d into main Oct 25, 2026
21 checks passed
@peter-gy
peter-gy deleted the pgy/managed-port-race branch October 25, 2026 18:46
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