socketio / socketio/socket.io

Socket server tries to close external http server, causing `ERR_SERVER_NOT_RUNNING` error

Open
#4,546 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
TypeScript
Stars
63.2k
Forks
10.3k
Avg merge
11d 20h
Merged PRs (30d)
2

Description

Describe the bug

When you initialize socket server, you can either give it an external instance of HTTPServer, or let socket.io create its own internal instance.

However, in either case, SocketIO tries to close the server when shutting down:

https://github.com/socketio/socket.io/blob/3b7ced7af7e0a2a66392577f94af1ee5ed190ab1/lib/index.ts#L685

In my case, I have my own shutdown procedure elsewhere, and it causes server to throw ERR_SERVER_NOT_RUNNING error, because server.close() was called twice.

This might be appearing now due to new stricter behavior in node.js, I am not sure. This is running on v14.17.3.

Expected behavior

IMO socket.io should only close HTTP server if it's its own instance. If an instance is provided, it's someone else's responsibility to close that server. Socket.IO shouldn't interfere.

Mitigation

I am currently using this hack to prevent the uncaught exception:

ioServer['httpServer'] = null;
ioServer.close();

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/index.ts around line 685 and inspect the shutdown path for externally supplied HTTPServer instances. Reproduce the double-close scenario described in the issue, then verify that an externally owned server is not closed by Socket.IO while an internally created server still shuts down correctly.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, typescript
Domain
backend, networking
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.