Skip to content

fix(tcp): rebind the listener after a dropped connection - #193

Open
ID-VerNe wants to merge 1 commit into
teamclouday:mainfrom
ID-VerNe:fix/tcp-auto-relisten
Open

ID-VerNe wants to merge 1 commit into
teamclouday:mainfrom
ID-VerNe:fix/tcp-auto-relisten

Conversation

@ID-VerNe

@ID-VerNe ID-VerNe commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Problem

When the TCP connection ends (phone app closed, Wi-Fi switched, streaming IO error), TcpStreamer::next() turns the end of the stream into a fatal ConnectError::Disconnected / WriteError::Io. The streamer runner then drops the TcpStreamer — and with it the TcpListener — and the UI falls back to Disconnected. The phone cannot reconnect until the user performs a manual Disconnect → Connect cycle.

Rebinding the same port immediately can also fail while the previous socket is in TIME_WAIT. That is the same port-in-use symptom reported in #151 for ADB mode.

Changes

  • Bind through TcpSocket with SO_REUSEADDR (bind_reusable()), so a rebind right after a drop does not collide with the previous socket in TIME_WAIT.
  • Treat recoverable stream endings as a relisten instead of a fatal error:
    • EOF (framed.next() returns None) and streaming IO errors other than TimedOut / WouldBlock drop the framed connection, rebind the same port, clear the per-connection process_cache, and emit StreamerMsg::Listening.
    • Rebind backs off 500 ms, doubling per failure and capped at 5 s; the delay resets after a successful bind.
    • Decode errors stay fatal: a malformed packet is a protocol bug, not a transport drop.
  • A manual Stop drops the streamer and cancels the in-flight rebind, so the listener is not resurrected.
  • Handshake failures, ADB/USB device errors, and deserializer errors still propagate as before.

Behavior

After a phone app crash or a dropped connection the PC returns to Listening on its own and accepts the next connection; no manual Connect is needed. Between sessions the UI shows Listening instead of Disconnected.

Verification

  • cargo fmt --all -- --check
  • CI run 44646365696: ubuntu / windows / macOS / Android all green.
  • Windows 11 + Xiaomi MI9 (TCP, debug build of the merged fork main): after a phone-side graceful disconnect the PC went back to Listening on the same port in ~0.73 ms with no PC-side click, and the phone reconnected immediately.
  • Manual Disconnect on the PC: no rebind observed for 65 s; port 54345 stayed closed, so a user-initiated stop is not resurrected.
  • Note: cargo build / cargo test were not run locally (protoc is not installed on this machine); CI covers the builds.

Related to #151 and #136. The ADB reverse-proxy rebuild after a drop is a separate follow-up PR.

When the phone dropped over Wi-Fi, the TCP streamer turned end-of-stream
(and streaming IO errors) into a fatal ConnectError. The runner replaced
the streamer with a DummyStreamer, dropping the TcpListener, and the UI
fell back to Default; the port stayed closed until a manual
Disconnect -> Connect cycle.

Treat end-of-stream and streaming IO errors as recoverable: the TCP
streamer tears down the framed connection and rebinds the same port with
a bounded exponential backoff (500ms, doubling, capped at 5s), then
returns to Listening and emits StreamerMsg::Listening so the UI and tray
follow. Bind through TcpSocket with SO_REUSEADDR so an immediate rebind
does not fail on a TIME_WAIT socket. A manual Stop drops the streamer,
which cancels the in-flight rebind, so the listener is not resurrected.

Handshake failures, adb/usb device errors, and deserializer errors still
propagate as fatal. TCP keepalive and ADB reverse-proxy re-establishment
are left as follow-ups.
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