Skip to content

Commit ef36032

Browse files
authored
fix: ensure PeerDisconnected is emitted on write-path disconnects (#435)
This fixes flaky failures in some of the integration tests coming soon. When a peer disconnects and the write path (broken pipe) detects it before the read path (EOF), the `PeerDisconnected` event could be lost entirely. `receive_message()` returned `ConnectionFailed` instead of `PeerDisconnected`, causing the reader loop to hit the catch-all error handler which slept 1 second before cleanup. During that sleep, the maintenance loop could silently remove the peer from the pool, preventing the event from ever being emitted. - Return `PeerDisconnected` from `receive_message()` when connection state is already cleared - Remove unnecessary 1-second sleep in the reader loop error handler
1 parent 2a2b141 commit ef36032

File tree

2 files changed

+3
-7
lines changed

2 files changed

+3
-7
lines changed

dash-spv/src/network/manager.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,8 +656,6 @@ impl PeerNetworkManager {
656656
}
657657
}
658658

659-
// For other errors, wait a bit then break
660-
tokio::time::sleep(Duration::from_secs(1)).await;
661659
break;
662660
}
663661
}

dash-spv/src/network/peer.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -368,11 +368,9 @@ impl Peer {
368368

369369
/// Receive a message from the peer.
370370
pub async fn receive_message(&mut self) -> NetworkResult<Option<Message>> {
371-
// First check if we have a state
372-
let state_arc = self
373-
.state
374-
.as_ref()
375-
.ok_or_else(|| NetworkError::ConnectionFailed("Not connected".to_string()))?;
371+
// If the state was cleared e.g. by a write-path broken pipe, treat as disconnected
372+
// so the reader loop handles it identically to a read-path EOF.
373+
let state_arc = self.state.as_ref().ok_or(NetworkError::PeerDisconnected)?;
376374

377375
// Lock the state for the entire read operation
378376
// This ensures no concurrent access to the socket

0 commit comments

Comments
 (0)