Skip to content

Re-draw a port the OS handed to another test - #11086

Merged
danielhanchen merged 2 commits into
mainfrom
fix/port-probe-redraws-a-taken-port
Sep 16, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
fix/port-probe-redraws-a-taken-port

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Summary

Tauri Linux debug build (no codesign) failed on an unrelated PR, one Rust test out of 555:

---- commands::tests::a_port_with_nothing_on_it_reads_as_death_not_a_stall stdout ----
panicked at src/commands.rs:1573:14:
nothing is listening on this port: Response { url: "http://127.0.0.1:36767/api/liveness", status: 200, ... }

Something answered on the port the test had just established was free.

Why

The setup binds port 0, reads the port the OS assigned, and drops the listener. That frees the
port, it does not reserve it. Rust runs the tests in this binary concurrently and several of them
bind ephemeral ports of their own, so the kernel is free to hand this one straight to one of those
between the drop and the probe. The request is then answered rather than refused, and
expect_err panics with the response it got, which is exactly what CI printed.

The fix

Obtaining a genuinely closed port is setup, not the property under test. The test now re-draws a
port, up to 16 times, until the connection is actually refused, and only then asserts what it is
there to assert: that a refused port reads as death rather than a stall, and inherits the busy
budget. The assertions are unchanged.

Verification

cargo check --bin unsloth-studio --tests passes with the change. The two warnings it emits are
in main.rs and process.rs, both pre-existing and untouched here.

A note on what is not verified: this workspace has no Rust toolchain of its own, and
generate_context! needs a built frontend/dist that does not exist here, so a stub index.html
was needed to get the macro past its check. That means the compile is verified but the test was
not executed locally. CI is the first place this actually runs.

The failure is a race, so a green run would not prove much either way. What is addressed here is
the mechanism: the test no longer assumes a released port stays free.

`Tauri Linux debug build (no codesign)` failed on an unrelated PR, one Rust test out of 555:

```
---- commands::tests::a_port_with_nothing_on_it_reads_as_death_not_a_stall stdout ----
panicked at src/commands.rs:1573:14:
nothing is listening on this port: Response { url: "http://127.0.0.1:36767/api/liveness", status: 200, ... }
```

Something answered on the port the test had just established was free.

## Why

The setup binds port 0, reads the port the OS assigned, and drops the listener. That frees the
port, it does not reserve it. Rust runs the tests in this binary concurrently and several of them
bind ephemeral ports of their own, so the kernel is free to hand this one straight to one of those
between the drop and the probe. The request is then answered rather than refused, and
`expect_err` panics with the response it got, which is exactly what CI printed.

## The fix

Obtaining a genuinely closed port is setup, not the property under test. The test now re-draws a
port, up to 16 times, until the connection is actually refused, and only then asserts what it is
there to assert: that a refused port reads as death rather than a stall, and inherits the busy
budget. The assertions are unchanged.

## Verification

`cargo check --bin unsloth-studio --tests` passes with the change. The two warnings it emits are
in `main.rs` and `process.rs`, both pre-existing and untouched here.

A note on what is not verified: this workspace has no Rust toolchain of its own, and
`generate_context!` needs a built `frontend/dist` that does not exist here, so a stub index.html
was needed to get the macro past its check. That means the compile is verified but the test was
not executed locally. CI is the first place this actually runs.

The failure is a race, so a green run would not prove much either way. What is addressed here is
the mechanism: the test no longer assumes a released port stays free.
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for security reviews. Please try again later.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 16, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-09-16T08:10:21.222907Z 7931dd7 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: b3596f5ad9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/src-tauri/src/commands.rs Outdated
error = Some(refused);
break;
}
Ok(_) => continue,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Avoid stealing another test's one-shot connection

When a released port is reassigned to a one-shot fixture, Ok(_) means this probe has already consumed that fixture's sole accepted connection; for example, loopback_http.rs:65-71 accepts exactly once before its owning test sends the intended request. Continuing can make this test pass while the owning test subsequently gets a refusal or timeout, so the same parallel-suite flake merely moves to another test (and repeated retries can disturb additional fixtures). Prevent or serialize cross-test port reuse rather than treating a successful response as harmless and retryable.

Useful? React with 👍 / 👎.

Retrying a port that answered was the wrong repair. Answering means the probe
had just consumed somebody's connection, and the one-shot fixture in
loopback_http accepts exactly once before its own test sends the request it
cares about, so a retry would hand that test a refusal instead. That relocates
the flake rather than fixing it.

Every other test in this binary binds port 0, so the allocator can only ever
give them an ephemeral port: 32768 and up on Linux, 49152 and up on macOS and
Windows. Drawing from below that range means a sibling cannot be given this
port at all, which removes the race rather than reacting to it.

A port in that window being occupied is a real listener on the machine, not a
sibling test, so the test walks a small range to find a free one and then makes
the same single assertion it made before.
@danielhanchen

Copy link
Copy Markdown
Member Author

Agreed, and this is the better fix. Retrying a port that answered was the wrong repair for exactly the reason you give: answering means the probe had already consumed somebody's connection, and loopback_http's fixture accepts once before its own test sends the request it cares about, so the retry would hand that test a refusal. That moves the flake instead of removing it.

7931dd707 prevents the reuse rather than reacting to it. Every other test in this binary binds port 0, so the allocator can only give them an ephemeral port, 32768 and up on Linux and 49152 and up on macOS and Windows. The probe now draws from below that range, where a sibling cannot be assigned a port at all.

A port in that window being occupied is a real listener on the machine rather than a sibling test, so the test walks a small range to find a free one and then makes the same single assertion as before, with no retry around the probe itself.

cargo check --bin unsloth-studio --tests passes; the two warnings are pre-existing in main.rs and process.rs. As noted in the description, this workspace has no Rust toolchain of its own and generate_context! needs a built frontend/dist, so the compile is verified but the test is not executed until CI.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: 7931dd7076

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit ae6bc9e into main Sep 16, 2026
32 of 35 checks passed
@danielhanchen
danielhanchen deleted the fix/port-probe-redraws-a-taken-port branch September 16, 2026 08:23
shaojun pushed a commit to shaojun/unsloth that referenced this pull request Sep 29, 2026
`Tauri Linux debug build (no codesign)` failed on one Rust test out of 555:

    ---- commands::tests::a_port_with_nothing_on_it_reads_as_death_not_a_stall stdout ----
    panicked at src/commands.rs:1573:14:
    nothing is listening on this port: Response { url: "http://127.0.0.1:36767/api/liveness", status: 200, ... }

Something answered on the port the test had just established was free.

## Why

The setup binds port 0, reads the assigned port, and drops the listener. That frees the port, it
does not reserve it. Rust runs the tests in this binary concurrently and several bind ephemeral
ports of their own, so the kernel can hand this one straight to one of those between the drop and
the probe.

## The fix

Draw from below the ephemeral range rather than retrying. Every other test here binds port 0, so
the allocator can only ever give them an ephemeral port: 32768 and up on Linux, 49152 and up on
macOS and Windows. A port below that cannot be assigned to a sibling at all, which removes the
race instead of reacting to it.

Retrying a port that answered was the first attempt and it was wrong: answering means the probe
had already consumed somebody's connection, and the one-shot fixture in `loopback_http` accepts
exactly once before its own test sends the request it cares about. That relocates the flake
rather than fixing it. Codex caught this during review.

A port in the chosen window being occupied is a real listener on the machine, not a sibling, so
the test walks a small range for a free one and then makes the same single assertion as before.

## Verification

`cargo check --bin unsloth-studio --tests` passes; its two warnings are pre-existing in `main.rs`
and `process.rs`.

Stated plainly: this workspace has no Rust toolchain of its own and `generate_context!` needs a
built `frontend/dist`, so the compile is verified but the test was not executed locally. CI is the
first place it runs.
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.

1 participant