Skip to content

fix: add timeouts to handshake I/O operations (#11)#17

Open
rafabd1 wants to merge 1 commit intomainfrom
fix/issue-11-handshake-timeouts
Open

fix: add timeouts to handshake I/O operations (#11)#17
rafabd1 wants to merge 1 commit intomainfrom
fix/issue-11-handshake-timeouts

Conversation

@rafabd1
Copy link
Owner

@rafabd1 rafabd1 commented Mar 14, 2026

Fix: Add timeouts to handshake I/O operations

Closes #11

Problem

When I2P tunnels are degraded (frequent during bootstrapping or network instability), peer connection establishment hangs indefinitely because the handshake I/O operations (read_framed / write_framed) have no timeouts.

The I2P streaming layer accepts the inbound connection, but the application-level handshake never completes because data can't flow through broken tunnels — and nothing ever times out to report the failure.

Solution

Added timeouts to all handshake I/O operations:

  1. In handle_incoming (responder side):

    • Read HANDSHAKE_INIT with 60-second timeout (given I2P latency)
    • Send HANDSHAKE_ACK with 30-second timeout (writing should be fast)
  2. In initiate_session (initiator side):

    • Send HANDSHAKE_INIT with 30-second timeout
    • Wait for HANDSHAKE_ACK with 60-second timeout
  3. Emit user-visible errors:

    • When handshake fails (timeout or otherwise), emit a connection_error event with a descriptive message
    • Users now see "Connection failed: handshake timeout: peer did not send INIT within 60s" instead of indefinite hanging

Security considerations

  • Timeouts prevent resource exhaustion on degraded I2P tunnels
  • No sensitive information is exposed in error messages
  • Error messages are generic enough not to leak timing attacks about key derivation

Implementation details

  • Used tokio::time::timeout with appropriate Duration values
  • Write operations have shorter timeouts (30s) since they should complete quickly
  • Read operations have longer timeouts (60s) to account for I2P network latency
  • All error messages are user-facing and descriptive

- Add timeout(60s) to read_framed in handle_incoming for reading HANDSHAKE_INIT
- Add timeout(30s) to write_framed in handle_incoming for sending HANDSHAKE_ACK
- Add timeout(30s) to write_framed in initiate_session for sending HANDSHAKE_INIT
- Add timeout(60s) to read_framed in initiate_session for reading HANDSHAKE_ACK
- Emit connection_error event with user-visible error message on handshake failures
- All timeouts use Duration from tokio::time to prevent indefinite hangs on degraded I2P tunnels

This fixes issue #11 where peer connections would hang indefinitely when I2P tunnels
are degraded and handshake I/O operations have no timeouts.
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.

Peer connection hangs indefinitely: missing timeouts on handshake I/O over degraded I2P tunnels

1 participant