fix: gracefully handle AddrInUse error instead of panicking on startup #2238
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
When starting
freenet networkwhile port 7509 is already in use (e.g., another freenet process is running), the node panics with an unhelpful stack trace: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:std::io::Result<()>instead of()AddrInUseerrors, provide a clear message: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, returnsResultserve_gateway(),serve_gateway_for_test(),serve_gateway_in(): Returnstd::io::Result?Testing
cargo check --all --testspassescargo clippy -p freenet --all-targetspassesCloses #2237
[AI-assisted - Claude]