Skip to content

Commit 2cd6d59

Browse files
fix(sio): do not throw when calling io.close() with an already stopped server
1 parent 579d43f commit 2cd6d59

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
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: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createServer } from "http";
22
import { io as ioc } from "socket.io-client";
33
import { join } from "path";
44
import { exec } from "child_process";
5-
import { Server } from "..";
5+
import { Server } from "../lib";
66
import expect from "expect.js";
77
import {
88
createClient,
@@ -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)