feat(network/onion): detect liveness via TcpStream close - #5168
Merged
Conversation
jetjinser
force-pushed
the
5161
branch
2 times, most recently
from
April 12, 2026 14:52
c4b6a66 to
cdc4bce
Compare
eval-exec
reviewed
Apr 14, 2026
Collaborator
|
I'm thinking we might not need to depend on torut after all. 🤔 |
Contributor
Author
Sounds reasonable, I'll try to do so |
Contributor
Author
|
@eval-exec Hi, I made a PR for this attempt: #5271 |
instead of polling
Contributor
Author
|
Since #5271 has been merged, I pushed a simpler implementation and updated the PR description. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR changes Tor liveness detection in the onion service from periodic GETINFO uptime polling to an event-driven approach by waiting for the Tor control TcpStream to close, reducing control-port traffic and failure-detection latency.
Changes:
- Added a oneshot-based disconnect notification from the Tor control connection reader task.
- Exposed
wait_for_disconnect()onTorController/TorConnectionto await connection loss. - Replaced the 3-second polling loop in
OnionServicewith atokio::select!awaiting disconnect or shutdown.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| util/onion/src/tor_controller.rs | Adds a wait_for_disconnect() API forwarding to the underlying connection (docs currently mismatched with return type). |
| util/onion/src/tor_connection.rs | Introduces oneshot disconnect signaling from the reader task and a wait_for_disconnect() awaitable (currently risks dropping stream halves too early). |
| util/onion/src/onion_service.rs | Switches liveness monitoring from periodic uptime polling to waiting on the disconnect notification. |
Comment on lines
+252
to
255
| /// Wait for the underlying TCP connection to close. | ||
| pub async fn wait_for_disconnect(self) -> bool { | ||
| self.disconnect_rx.await.is_ok() | ||
| } |
Comment on lines
+107
to
+112
| /// Returns `Some(())` when the connection is lost, or `None` if the internal | ||
| /// notification channel was closed unexpectedly (which should not occur during | ||
| /// normal operation). | ||
| pub async fn wait_for_disconnect(self) -> bool { | ||
| self.inner.wait_for_disconnect().await | ||
| } |
Comment on lines
+252
to
255
| /// Wait for the underlying TCP connection to close. | ||
| pub async fn wait_for_disconnect(self) -> bool { | ||
| self.disconnect_rx.await.is_ok() | ||
| } |
eval-exec
approved these changes
Jul 24, 2026
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What problem does this PR solve?
Issue Number: close #5161
Follow-up to #5271.
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.What is changed and how it works?
What's Changed:
oneshotchannel notifiesTorControllerwhen either copy direction terminates (EOF or error).OnionServicewith a branch awaitingwait_for_disconnect().Related changes
Check List
Tests
Manual test steps:
ckb runwith config listen_on_onion = true."Tor control connection lost"within milliseconds, not after a multi-second delay.Side effects
Performance regressionBreaking backward compatibility