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: 34cbfbb532

Related: https://github.com/socketio/socket.io-client-java/issues/446
This commit is contained in:
Damien Arrachequesne
2024-07-10 11:38:53 +02:00
parent b00ae8eec1
commit 54645ece2c

View File

@@ -283,6 +283,21 @@ public class Socket extends Emitter {
this.connected = false; this.connected = false;
this.id = null; this.id = null;
super.emit(EVENT_DISCONNECT, reason); 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) { private void onpacket(Packet<?> packet) {
@@ -448,12 +463,6 @@ public class Socket extends Emitter {
this.subs = null; this.subs = null;
} }
for (Ack ack : acks.values()) {
if (ack instanceof AckWithTimeout) {
((AckWithTimeout) ack).cancelTimer();
}
}
this.io.destroy(); this.io.destroy();
} }