Skip to content

Commit

Permalink
transport: Fix double lock and state overwrite on disconnected peers (#…
Browse files Browse the repository at this point in the history
…179)

This PR fixes an issue where the secondary connection is rejected
because it has a different connection ID.

The issue comes from `fn dial_address` which can overwrite the state of
a disconnected peer with in-flight dial requests.


### High level repro case
- T0: dial_address 1
- T1: dial_address 2 which overwrites address 1
- T2: dialed address 1 responds with a connection
- T3: connection is rejected

While at it, this PR also:
- removes the need to double lock the peer state (once to read and once
to write)
- enforces connection ID checks on dial records in `on_dial_failure`
- adjust the repro case from
#176 for the fix

### Detailed repro case

- T0: Dial address A connection ID 1 -> State : Dialing { in-flight-1 }
- T1: Connection is established by remote address C connection ID 3 ->
State : Connected { in-flight-dial 1 }
- T2: Closed connection established in T2 -> State : Disconnected {
in-flight-dial 1}

// This is where the state was wrongfully overwritten
- T3: Dial address B connection ID 2 -> State: Dialing { in-flight-2 } 

- T4: Connection is established by remote address C connection ID 3 ->
State : Connected { in-flight-dial 2 }

// The issue was detected here
- T5: Dial from T0 receives a response however it expects in-flight-dial
1 and the state has in-flight-dial 2


Closes: #172

---------

Signed-off-by: Alexandru Vasile <alexandru.vasile@parity.io>
  • Loading branch information
lexnv committed Aug 2, 2024
1 parent 19bf131 commit 7befb3a
Show file tree
Hide file tree
Showing 2 changed files with 257 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/transport/manager/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl AddressRecord {

/// Update score of an address.
pub fn update_score(&mut self, score: i32) {
self.score += score;
self.score = self.score.saturating_add(score);
}

/// Set `ConnectionId` for the [`AddressRecord`].
Expand Down
Loading

0 comments on commit 7befb3a

Please sign in to comment.