Skip to content

hysteria2: bind UDP socket before spawning, return bind errors instead of panicking#143

Open
zytakeshi wants to merge 1 commit into
cfal:masterfrom
zytakeshi:fix/hysteria2-graceful-bind
Open

hysteria2: bind UDP socket before spawning, return bind errors instead of panicking#143
zytakeshi wants to merge 1 commit into
cfal:masterfrom
zytakeshi:fix/hysteria2-graceful-bind

Conversation

@zytakeshi

Copy link
Copy Markdown

start_hysteria2_server returns io::Result<Vec<JoinHandle<()>>>, but the per-endpoint loop did the UDP socket bind and the quinn endpoint creation inside tokio::spawn(async move { ... }) and .unwrap()d both.

The function pushes the join handles and returns Ok to its caller before any spawned task body runs. So a bind failure -- e.g. EADDRINUSE during a restart or migration -- did not surface as the io::Error the signature already allows; it panicked a worker thread while the caller had already observed a successful start.

Fix:

  • Hoist the UDP socket bind out of the spawned task to before tokio::spawn, still inside the for _ in 0..num_endpoints loop (one bind per endpoint, preserving the SO_REUSEPORT multi-endpoint behavior), and propagate its error with ? so a bind failure is returned to the caller.
  • Move the now-bound socket into the spawned task (captured by async move).
  • For the quinn endpoint creation that remains inside the task, replace .unwrap() with log-and-return so it degrades gracefully instead of panicking.

No config or protocol change; pure robustness. The bind -- the hard failure case -- is now surfaced synchronously to the caller.

start_hysteria2_server unwrapped the UDP socket bind inside the spawned
per-endpoint task, but the function returns Ok to the caller before the
task runs. A bind failure (e.g. EADDRINUSE on restart) therefore panicked
a worker thread while the caller saw a successful start. Hoist the bind
ahead of tokio::spawn (kept inside the per-endpoint loop to preserve
SO_REUSEPORT) and propagate the error via ? so callers can handle it;
log-and-return on in-task endpoint creation failure instead of unwrapping.
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