Skip to content

Commit 54645ec

Browse files
fix: discard acknowledgements upon disconnection
Previously, getting disconnected while waiting for an acknowledgement would create a memory leak, as the acknowledgement was never received and the handler would stay in memory forever. See also: socketio/socket.io-client@34cbfbb Related: #446
1 parent b00ae8e commit 54645ec

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

Diff for: src/main/java/io/socket/client/Socket.java

+15-6
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,21 @@ private void onclose(String reason) {
283283
this.connected = false;
284284
this.id = null;
285285
super.emit(EVENT_DISCONNECT, reason);
286+
this.clearAcks();
287+
}
288+
289+
/**
290+
* Clears the acknowledgement handlers upon disconnection, since the client will never receive an acknowledgement from
291+
* the server.
292+
*/
293+
private void clearAcks() {
294+
for (Ack ack : this.acks.values()) {
295+
if (ack instanceof AckWithTimeout) {
296+
((AckWithTimeout) ack).onTimeout();
297+
}
298+
// note: basic Ack objects have no way to report an error, so they are simply ignored here
299+
}
300+
this.acks.clear();
286301
}
287302

288303
private void onpacket(Packet<?> packet) {
@@ -448,12 +463,6 @@ private void destroy() {
448463
this.subs = null;
449464
}
450465

451-
for (Ack ack : acks.values()) {
452-
if (ack instanceof AckWithTimeout) {
453-
((AckWithTimeout) ack).cancelTimer();
454-
}
455-
}
456-
457466
this.io.destroy();
458467
}
459468

0 commit comments

Comments
 (0)