Skip to content

Commit

Permalink
fix: discard acknowledgements upon disconnection
Browse files Browse the repository at this point in the history
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
  • Loading branch information
darrachequesne committed Jul 10, 2024
1 parent b00ae8e commit 54645ec
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/main/java/io/socket/client/Socket.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,21 @@ private void onclose(String reason) {
this.connected = false;
this.id = null;
super.emit(EVENT_DISCONNECT, reason);
this.clearAcks();
}

/**
* Clears the acknowledgement handlers upon disconnection, since the client will never receive an acknowledgement from
* the server.
*/
private void clearAcks() {
for (Ack ack : this.acks.values()) {
if (ack instanceof AckWithTimeout) {
((AckWithTimeout) ack).onTimeout();
}
// note: basic Ack objects have no way to report an error, so they are simply ignored here
}
this.acks.clear();
}

private void onpacket(Packet<?> packet) {
Expand Down Expand Up @@ -448,12 +463,6 @@ private void destroy() {
this.subs = null;
}

for (Ack ack : acks.values()) {
if (ack instanceof AckWithTimeout) {
((AckWithTimeout) ack).cancelTimer();
}
}

this.io.destroy();
}

Expand Down

0 comments on commit 54645ec

Please sign in to comment.