Skip to content

Commit 9581f9b

Browse files
fix(sio): do not throw when calling io.close() on a stopped server
Following [1], calling both `io.close()` and `httpServer.close()` would throw an ERR_SERVER_NOT_RUNNING exception, which was not the case before. Related: #5431 [1]: bb0b480
1 parent 579d43f commit 9581f9b

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

packages/socket.io/lib/index.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -831,14 +831,13 @@ export class Server<
831831
restoreAdapter();
832832

833833
if (this.httpServer) {
834-
await new Promise<void>((resolve, reject) => {
834+
return new Promise<void>((resolve) => {
835835
this.httpServer.close((err) => {
836836
fn && fn(err);
837837
if (err) {
838-
reject(err);
839-
} else {
840-
resolve();
838+
debug("server was not running");
841839
}
840+
resolve();
842841
});
843842
});
844843
} else {

packages/socket.io/test/close.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,27 @@ describe("close", () => {
7070
});
7171
});
7272

73+
it("should not throw when the underlying HTTP server is not running (callback)", (done) => {
74+
const httpServer = createServer();
75+
const io = new Server(httpServer);
76+
77+
io.close((err) => {
78+
expect((err as Error & { code: string }).code).to.eql(
79+
"ERR_SERVER_NOT_RUNNING",
80+
);
81+
done();
82+
});
83+
});
84+
85+
it("should not throw when the underlying HTTP server is not running (Promise)", (done) => {
86+
const httpServer = createServer();
87+
const io = new Server(httpServer);
88+
89+
io.close()
90+
.then(() => done())
91+
.catch((e) => done(e));
92+
});
93+
7394
describe("graceful close", () => {
7495
function fixture(filename) {
7596
return (

0 commit comments

Comments
 (0)