Skip to content

Commit d62737b

Browse files
fix(sio-client): close the engine upon decoding exception
1 parent fcbecd4 commit d62737b

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

.github/workflows/ci-browser.yml

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 27 additions & 0 deletions
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", () => {
@@ -894,4 +895,30 @@ describe("connection", () => {
894895
});
895896
});
896897
});
898+
899+
it("should close the engine upon decoding exception", () => {
900+
return wrap((done) => {
901+
const manager = new Manager(BASE_URL, {
902+
autoConnect: true,
903+
reconnectionDelay: 50,
904+
});
905+
906+
let engine = manager.engine;
907+
908+
manager.on("open", () => {
909+
nextTick(() => {
910+
// @ts-expect-error emit() is private
911+
manager.engine.emit("data", "bad");
912+
});
913+
});
914+
915+
manager.on("reconnect", () => {
916+
expect(manager.engine === engine).to.be(false);
917+
expect(engine.readyState).to.eql("closed");
918+
919+
manager._close();
920+
done();
921+
});
922+
});
923+
});
897924
});

0 commit comments

Comments
 (0)