Skip to content

Fix HTTP test port race and self-request origin#3570

Merged
itowlson merged 2 commits into
spinframework:mainfrom
ChihweiLHBird:flaky-integration-test-port-conflict-fix
Jun 16, 2026
Merged

Fix HTTP test port race and self-request origin#3570
itowlson merged 2 commits into
spinframework:mainfrom
ChihweiLHBird:flaky-integration-test-port-conflict-fix

Conversation

@ChihweiLHBird

@ChihweiLHBird ChihweiLHBird commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes flaky HTTP integration test startup caused by a test harness port race.

The harness previously selected a port by binding localhost:0, reading the assigned port, dropping that listener, and later launching spin up --listen 127.0.0.1:{port}. In parallel test runs, another process could claim that port before this Spin instance bound it. The old readiness check only proved that something accepted TCP connections on the port, so a test could accidentally send requests to the wrong Spin app and see a spurious 404.

Change

Production code

  • Track the actual bound HTTP listener address and use it for self-request origin setup.

Test framework

  • Start Spin with --listen 127.0.0.1:0, then parse the actual bound port from this Spin process's startup output.
  • Report early Spin startup failures with stdout/stderr.

Why the server change is needed

After the test harness switched to 127.0.0.1:0, HttpServer::listen_addr could remain 127.0.0.1:0 even though the actual listener was bound to an assigned port. Self requests must use the real bound address, not the configured :0 address. The server now stores listener.local_addr() after binding and uses that address when configuring SelfRequestOrigin.

For in-process tests, InProcessSpin builds a server and drives handle() directly without calling serve(), so no socket address is recorded. That path falls back to the configured listen address.

@ChihweiLHBird ChihweiLHBird force-pushed the flaky-integration-test-port-conflict-fix branch from 2b379f9 to 0c53025 Compare June 13, 2026 08:23
@ChihweiLHBird ChihweiLHBird marked this pull request as draft June 13, 2026 19:04
Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
@ChihweiLHBird ChihweiLHBird force-pushed the flaky-integration-test-port-conflict-fix branch from 0c53025 to 6b9e838 Compare June 13, 2026 21:03
@ChihweiLHBird ChihweiLHBird changed the title Fix flaky HTTP integration test startup Fix HTTP test port race and self-request origin Jun 13, 2026
@ChihweiLHBird ChihweiLHBird marked this pull request as ready for review June 13, 2026 23:27
@itowlson itowlson requested a review from rylev June 14, 2026 20:24

@itowlson itowlson left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'll defer to @rylev on the test stuff, but I'd like to understand how often we are seeing port race failures - I don't think I've ever seen one myself. Unless you're being actively affected by them, I would tend to be conservative with changes.

I can see the issue with self-requests though. So we probably need to fix that and if the rest of it comes along for the ride well fair enough.

Comment thread crates/trigger-http/src/server.rs Outdated
Comment thread crates/trigger-http/src/server.rs Outdated
.local_addr()?
.port())
fn parse_serving_port(output: &str) -> Option<u16> {
parse_plain_serving_port(output).or_else(|| parse_json_serving_port(output))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Wait, don't we know whether it's printing JSON or plain text?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There was a test I added in the previous PR for testing JSON output, and all other tests are plain text. I can update the test code to force every test to output JSON, which is easier to handle. How does that sound?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I was thinking more that we would be able to know when calling parse_serving_port whether the output was set to JSON. But maybe that info is buried in some Command arg that appears only in a callback somewhere and is inaccessible. It's fine to leave this as is.

@ChihweiLHBird

Copy link
Copy Markdown
Contributor Author

@itowlson Thanks for the feedback! Actually, I saw more than once impact of this issue, sometimes it's very implicit, shown as 404 not found error, when a test code hits another test spin instance which doesn't have the expected path.

@itowlson

Copy link
Copy Markdown
Collaborator

I saw more than once impact of this issue, sometimes it's very implicit, shown as 404 not found error, when a test code hits another test spin instance which doesn't have the expected path.

Interesting. If it's happening that often, I'm surprised we don't get a lot more flakes in CI - I mean, yes, we do get flakes, but if they show up in manual testing then I'd expect them to absolutely wreck CI, which runs about a gazillion times on every PR. But of course we'd want it to be correct anyway!

Signed-off-by: Zhiwei Liang <zhiwei.liang@zliang.me>
@ChihweiLHBird ChihweiLHBird force-pushed the flaky-integration-test-port-conflict-fix branch from 6b9e838 to c4617b7 Compare June 16, 2026 04:45
@ChihweiLHBird

ChihweiLHBird commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

I saw more than once impact of this issue, sometimes it's very implicit, shown as 404 not found error, when a test code hits another test spin instance which doesn't have the expected path.

Interesting. If it's happening that often, I'm surprised we don't get a lot more flakes in CI - I mean, yes, we do get flakes, but if they show up in manual testing then I'd expect them to absolutely wreck CI, which runs about a gazillion times on every PR. But of course we'd want it to be correct anyway!

Actually, I saw that from one CI run in my another PR and started investigating into it. I didn't catch it locally

@itowlson itowlson merged commit 50c43b9 into spinframework:main Jun 16, 2026
17 checks passed
@ChihweiLHBird ChihweiLHBird deleted the flaky-integration-test-port-conflict-fix branch June 17, 2026 05:59
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