Skip to content

feat(network/onion): detect liveness via TcpStream close#5168

Open
jetjinser wants to merge 2 commits intonervosnetwork:developfrom
jetjinser:5161
Open

feat(network/onion): detect liveness via TcpStream close#5168
jetjinser wants to merge 2 commits intonervosnetwork:developfrom
jetjinser:5161

Conversation

@jetjinser
Copy link
Copy Markdown
Contributor

What problem does this PR solve?

Issue Number: close #5161

Problem Summary:

The onion service currently polls the Tor daemon every 3 seconds via get_uptime() to detect connection loss. This adds unnecessary latency (up to 3s for failure detection) and constant I/O overhead. A passive, event-driven approach would react instantly and reduce resource usage.

P.S. This implementation represents the cleanest approach I could devise given the constraints of the existing torut controller API. If there is a more idiomatic or efficient way to monitor the underlying TcpStream liveness, I am open to feedback and happy to revise this PR accordingly.

What is changed and how it works?

What's Changed:

  • Introduced tokio::io::duplex as an in-memory proxy between TcpStream and the TorControl.
  • Spawned a background task that performs bidirectional copy between the TCP socket and the duplex stream.
  • An mpsc channel notifies TorController when either copy direction terminates (EOF or error).
  • Replaced the 3-second ticker loop in OnionService with a branch awaiting wait_for_disconnect().

Related changes

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Manual test steps:

  1. ckb run with config listen_on_onion = true.
  2. Observe the onion service starts successfully.
  3. Kill/exit the Tor process.
  4. Verify the node logs "Tor control connection lost" within milliseconds, not after a multi-second delay.
  5. Restart the Tor daemon and verify the onion service can be re-established.

Side effects

  • Performance regression
  • Breaking backward compatibility
  • Few more memory consumption

@jetjinser jetjinser requested a review from a team as a code owner April 11, 2026 07:10
@jetjinser jetjinser requested review from doitian and removed request for a team April 11, 2026 07:10
@jetjinser jetjinser force-pushed the 5161 branch 2 times, most recently from c4b6a66 to cdc4bce Compare April 12, 2026 14:52
Comment thread util/onion/src/tor_controller.rs Outdated
debug!("Tor DuplexStream exited.");
});

let mut utc: UnauthenticatedConn<_> = UnauthenticatedConn::new(client);
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.

UnauthenticatedConn<_>

Could UnauthenticatedConn replace _ with the real inner type?

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.

done :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: 🆕 New

Development

Successfully merging this pull request may close these issues.

network/onion: detect Tor liveness via socket close instead of polling

3 participants