Skip to content

Commit

Permalink
fix(sio-client): close the engine upon decoding exception
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Sep 17, 2024
1 parent fcbecd4 commit a71f3a4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/socket.io-client/lib/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,6 @@ export class Manager<
this.skipReconnect = true;
this._reconnecting = false;
this.onclose("forced close");
if (this.engine) this.engine.close();
}

/**
Expand All @@ -544,14 +543,19 @@ export class Manager<
}

/**
* Called upon engine close.
* Called when:
*
* - the low-level engine is closed
* - the parser encountered a badly formatted packet
* - all sockets are disconnected
*
* @private
*/
private onclose(reason: string, description?: DisconnectDescription): void {
debug("closed due to %s", reason);

this.cleanup();
this.engine?.close();
this.backoff.reset();
this._readyState = "closed";
this.emitReserved("close", reason, description);
Expand Down
27 changes: 27 additions & 0 deletions packages/socket.io-client/test/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import hasCORS from "has-cors";
import { install } from "@sinonjs/fake-timers";
import textBlobBuilder from "text-blob-builder";
import { BASE_URL, wrap } from "./support/util";
import { nextTick } from "engine.io-client";

describe("connection", () => {
it("should connect to localhost", () => {
Expand Down Expand Up @@ -894,4 +895,30 @@ describe("connection", () => {
});
});
});

it("should close the engine upon decoding exception", () => {
return wrap((done) => {
const manager = new Manager(BASE_URL, {
autoConnect: true,
reconnectionDelay: 50,
});

let engine = manager.engine;

manager.on("open", () => {
nextTick(() => {
// @ts-expect-error emit() is private
manager.engine.emit("data", "bad");
});
});

manager.on("reconnect", () => {
expect(manager.engine === engine).to.be(false);
expect(engine.readyState).to.eql("closed");

manager._close();
done();
});
});
});
});

0 comments on commit a71f3a4

Please sign in to comment.