Skip to content

Commit 8224f1a

Browse files
feat(identify): keep connection alive while we are using it
Currently, the `connection_keep_alive` function of identify does not compute anything but its return value is set through the handler state machine. This is hard to understand and causes surprising behaviour because at the moment, we set `KeepAlive::No` as soon as the remote has answered our identify request. Depending on what else is happening on the connection, this might close the connection before we have successfully answered the remote's identify request. To fix this, we now compute `connection_keep_alive` based on whether we are still using the connection. Related: #3844. Pull-Request: #3876.
1 parent 81c424e commit 8224f1a

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

protocols/identify/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@
77
- Reduce the initial delay before running the identify protocol to 0 and make the option deprecated.
88
See [PR 3545].
99

10+
- Fix aborting the answering of an identify request in rare situations.
11+
See [PR 3876].
12+
1013
[PR 3698]: https://github.com/libp2p/rust-libp2p/pull/3698
1114
[PR 3715]: https://github.com/libp2p/rust-libp2p/pull/3715
1215
[PR 3545]: https://github.com/libp2p/rust-libp2p/pull/3545
16+
[PR 3876]: https://github.com/libp2p/rust-libp2p/pull/3876
1317

1418
## 0.42.2
1519

protocols/identify/src/handler.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,6 @@ pub struct Handler {
6464
/// Future that fires when we need to identify the node again.
6565
trigger_next_identify: Delay,
6666

67-
/// Whether the handler should keep the connection alive.
68-
keep_alive: KeepAlive,
69-
7067
/// The interval of `trigger_next_identify`, i.e. the recurrent delay.
7168
interval: Duration,
7269

@@ -132,7 +129,6 @@ impl Handler {
132129
reply_streams: VecDeque::new(),
133130
pending_replies: FuturesUnordered::new(),
134131
trigger_next_identify: Delay::new(initial_delay),
135-
keep_alive: KeepAlive::Yes,
136132
interval,
137133
public_key,
138134
protocol_version,
@@ -190,7 +186,6 @@ impl Handler {
190186
.push(ConnectionHandlerEvent::Custom(Event::Identified(
191187
remote_info,
192188
)));
193-
self.keep_alive = KeepAlive::No;
194189
}
195190
future::Either::Right(()) => self
196191
.events
@@ -210,7 +205,6 @@ impl Handler {
210205
.push(ConnectionHandlerEvent::Custom(Event::IdentificationError(
211206
err,
212207
)));
213-
self.keep_alive = KeepAlive::No;
214208
self.trigger_next_identify.reset(self.interval);
215209
}
216210
}
@@ -268,7 +262,19 @@ impl ConnectionHandler for Handler {
268262
}
269263

270264
fn connection_keep_alive(&self) -> KeepAlive {
271-
self.keep_alive
265+
if self.inbound_identify_push.is_some() {
266+
return KeepAlive::Yes;
267+
}
268+
269+
if !self.pending_replies.is_empty() {
270+
return KeepAlive::Yes;
271+
}
272+
273+
if !self.reply_streams.is_empty() {
274+
return KeepAlive::Yes;
275+
}
276+
277+
KeepAlive::No
272278
}
273279

274280
fn poll(

0 commit comments

Comments
 (0)