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
Related: #5128
  • Loading branch information
darrachequesne committed Sep 18, 2024
1 parent 2194264 commit 04c8dd9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-browser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ permissions:
jobs:
test-browser:
runs-on: ubuntu-latest
timeout-minutes: 10
timeout-minutes: 20

steps:
- name: Checkout repository
Expand Down
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 @@ -896,4 +897,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 04c8dd9

Please sign in to comment.