Skip to content

Conversation

@sanity
Copy link
Collaborator

@sanity sanity commented Dec 6, 2025

Problem

When starting freenet network while port 7509 is already in use (e.g., another freenet process is running), the node panics with an unhelpful stack trace:

thread 'tokio-runtime-worker' panicked at .../src/server/mod.rs:72:68:
called `Result::unwrap()` on an `Err` value: Os { code: 98, kind: AddrInUse, message: "Address already in use" }

This panic cascades through the system, making it difficult to diagnose the actual problem.

Solution

Changed serve() to bind the TCP listener synchronously before spawning the async server task. This allows proper error propagation:

  1. Bind listener first (before spawning task)
  2. Return std::io::Result<()> instead of ()
  3. For AddrInUse errors, provide a clear message:

    "Port X is already in use. Another freenet process may be running. Use 'pkill freenet' to stop it, or specify a different port with --gateway-port."

The key insight is that the original code spawned a task and then tried to bind - so the error happened asynchronously after serve() returned, making it impossible to propagate to the caller. By binding before spawning, errors surface immediately and can be handled properly.

Changes

  • serve(): Now async, binds listener before spawning, returns Result
  • serve_gateway(), serve_gateway_for_test(), serve_gateway_in(): Return std::io::Result
  • All callers updated to propagate the error with ?

Testing

  • cargo check --all --tests passes
  • cargo clippy -p freenet --all-targets passes

Closes #2237

[AI-assisted - Claude]

When starting freenet while port 7509 is already in use, the node
previously panicked with an unhelpful stack trace. This change:

- Makes serve() bind the listener before spawning the server task
- Returns a proper Result instead of using unwrap()
- Provides a clear error message for AddrInUse:
  "Port X is already in use. Another freenet process may be running.
   Use 'pkill freenet' to stop it, or specify a different port with --gateway-port."

Closes #2237

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@sanity sanity enabled auto-merge December 6, 2025 23:37
The test was holding the TcpListener socket while trying to start
the gateway on the same port, causing an AddrInUse error. This was
a pre-existing race condition that my changes to return proper errors
from serve_gateway exposed.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
@sanity sanity added this pull request to the merge queue Dec 7, 2025
Merged via the queue into main with commit 681af63 Dec 7, 2025
8 checks passed
@sanity sanity deleted the fix/graceful-addr-in-use-error branch December 7, 2025 04:02
Copy link
Collaborator

@iduartgomez iduartgomez left a comment

Choose a reason for hiding this comment

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

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.

fix: gracefully handle AddrInUse error instead of panicking on startup

3 participants