Skip to content

Commit 04c8dd9

Browse files
fix(sio-client): close the engine upon decoding exception
Related: #5128
1 parent 2194264 commit 04c8dd9

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

.github/workflows/ci-browser.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ permissions:
1414
jobs:
1515
test-browser:
1616
runs-on: ubuntu-latest
17-
timeout-minutes: 10
17+
timeout-minutes: 20
1818

1919
steps:
2020
- name: Checkout repository

packages/socket.io-client/lib/manager.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,6 @@ export class Manager<
531531
this.skipReconnect = true;
532532
this._reconnecting = false;
533533
this.onclose("forced close");
534-
if (this.engine) this.engine.close();
535534
}
536535

537536
/**
@@ -544,14 +543,19 @@ export class Manager<
544543
}
545544

546545
/**
547-
* Called upon engine close.
546+
* Called when:
547+
*
548+
* - the low-level engine is closed
549+
* - the parser encountered a badly formatted packet
550+
* - all sockets are disconnected
548551
*
549552
* @private
550553
*/
551554
private onclose(reason: string, description?: DisconnectDescription): void {
552555
debug("closed due to %s", reason);
553556

554557
this.cleanup();
558+
this.engine?.close();
555559
this.backoff.reset();
556560
this._readyState = "closed";
557561
this.emitReserved("close", reason, description);

packages/socket.io-client/test/connection.ts

+27
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import hasCORS from "has-cors";
44
import { install } from "@sinonjs/fake-timers";
55
import textBlobBuilder from "text-blob-builder";
66
import { BASE_URL, wrap } from "./support/util";
7+
import { nextTick } from "engine.io-client";
78

89
describe("connection", () => {
910
it("should connect to localhost", () => {
@@ -896,4 +897,30 @@ describe("connection", () => {
896897
});
897898
});
898899
});
900+
901+
it("should close the engine upon decoding exception", () => {
902+
return wrap((done) => {
903+
const manager = new Manager(BASE_URL, {
904+
autoConnect: true,
905+
reconnectionDelay: 50,
906+
});
907+
908+
let engine = manager.engine;
909+
910+
manager.on("open", () => {
911+
nextTick(() => {
912+
// @ts-expect-error emit() is private
913+
manager.engine.emit("data", "bad");
914+
});
915+
});
916+
917+
manager.on("reconnect", () => {
918+
expect(manager.engine === engine).to.be(false);
919+
expect(engine.readyState).to.eql("closed");
920+
921+
manager._close();
922+
done();
923+
});
924+
});
925+
});
899926
});

0 commit comments

Comments
 (0)