cleanupEmptyChildNamespaces releases a child namespace while a socket is in its middleware, and the cleanup unregisters a newer namespace by name
Les mainteneurs répondent en général sous 2 jours
Évaluation
- Difficulté
- 3/5
- Temps estimé
- 1-2 jours
- Accessibilité débutants
- 54/100
- Type d'issue
- Bug
- Clarté
- Clairement spécifiée
- Activité
- Active
- Stack technique
- node.js, typescript
- Domaine
- backend, networking
Piste de recherche
Start in dist/parent-namespace.js at the cleanupEmptyChildNamespaces handler in ParentNamespace.createChild; trace how middleware sockets are tracked and how the _nsps entry is removed. Add regression coverage for a socket held in async middleware while another connects and disconnects. Done when broadcasts reach every connected socket and cleanup preserves the currently registered namespace; the payload does not name a test file.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
Describe the bug
With cleanupEmptyChildNamespaces: true, a child namespace is released as soon as namespace.sockets.size === 0, even while another socket of that namespace is still in its (async) middleware. That socket then joins the released namespace object, while the next connection gets a new object under the same name. Two consequences:
- Broadcasting with
io.of(name).emit(...)reaches only the sockets of the newer object; the socket admitted into the released object receives nothing. - When the released object later empties, the cleanup deletes the
_nspsentry by name, which unregisters the newer object while its sockets are still connected;io.of(name)then creates yet another object, and broadcasts no longer reach any connected socket.
(Related but different: #4772, cleanup after a socket is rejected in middleware.)
To Reproduce
Socket.IO server version: 4.8.3
const http = require('http');
const { Server } = require('socket.io');
const { io } = require('socket.io-client');
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const once = (emitter, event) => new Promise((resolve) => emitter.once(event, resolve));
const httpServer = http.createServer();
const ioServer = new Server(httpServer, { cleanupEmptyChildNamespaces: true });
let releaseHandshake;
const handshakeGate = new Promise((resolve) => { releaseHandshake = resolve; });
let handshakes = 0;
ioServer.of(/^\/room-\w+$/).use(async (socket, next) => {
handshakes++;
if (handshakes === 2) await handshakeGate; // e.g. an auth lookup that takes a while
next();
});
httpServer.listen(0, async () => {
const url = `http://localhost:${httpServer.address().port}/room-a`;
const opts = { forceNew: true, transports: ['websocket'] };
const first = io(url, opts);
await once(first, 'connect');
const nspAtStart = ioServer._nsps.get('/room-a');
const held = io(url, opts); // its handshake waits in the middleware
await sleep(100);
first.disconnect(); // the only connected socket leaves: the namespace is released
await sleep(100);
console.log('1. namespace released while "held" is still in its middleware:',
ioServer._nsps.get('/room-a') !== nspAtStart);
releaseHandshake();
await once(held, 'connect'); // "held" joins the released namespace object
const third = io(url, opts);
await once(third, 'connect'); // a new namespace object is created under the same name
const received = { held: 0, third: 0 };
held.on('news', () => received.held++);
third.on('news', () => received.third++);
ioServer.of('/room-a').emit('news');
await sleep(200);
console.log('2. broadcast to of("/room-a") with two connected sockets:', received,
'(expected { held: 1, third: 1 })');
held.disconnect(); // the released object empties: cleanup deletes the _nsps entry by name
await sleep(200);
console.log('3. after "held" leaves, the namespace "third" is in is still registered:',
ioServer._nsps.get('/room-a') != null);
received.third = 0;
ioServer.of('/room-a').emit('news');
await sleep(200);
console.log('4. broadcast to of("/room-a") with "third" still connected:', { third: received.third },
'(expected { third: 1 })');
third.disconnect();
ioServer.close();
process.exit(0);
});
Output (socket.io 4.8.3, socket.io-client 4.8.3, Node.js 24):
1. namespace released while "held" is still in its middleware: true
2. broadcast to of("/room-a") with two connected sockets: { held: 0, third: 1 } (expected { held: 1, third: 1 })
3. after "held" leaves, the namespace "third" is in is still registered: false
4. broadcast to of("/room-a") with "third" still connected: { third: 0 } (expected { third: 1 })
Expected behavior
- A namespace with a socket still in its middleware (
_preConnectSockets) is not released, or a socket finishing its handshake joins the namespace object currently registered under that name. - The cleanup removes the
_nspsentry only if it still points to the object being released (if (this._nsps.get(name) === namespace) this._nsps.delete(name)).
Platform:
- Server: Node.js 24, macOS and Linux
Additional context
The release check is in ParentNamespace.createChild (dist/parent-namespace.js, the cleanupEmptyChildNamespaces handler on namespace.sockets.size === 0), which does not consider namespace._preConnectSockets. We work around it on our side by keeping per-namespace-object state instead of per-name state.
- Langage dominant
- TypeScript
- Étoiles
- 63.2k
- Forks
- 10.2k
- Merge moyen
- 6 j 4 h
- PR mergées (30 j)
- 6
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de socketio/socket.io
-
Difficulté 2/5 1-3 heures Accessibilité débutants 88/100
Les mainteneurs répondent en général sous 2 jours
-
Raise Engine.IO minimum to 6.6.10 for published security fixesPeut-être pris @harshit-exe-dev l’a pris il y a 28 jours. Ouverte
Difficulté 1/5 1-3 heures Accessibilité débutants 78/100
Les mainteneurs répondent en général sous 2 jours
-
Can't set response headers in Server.handleUpgradePeut-être pris @MsfPablo l’a pris il y a 89 jours. Ouvertequestion
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
socketio/socket.io#5497 · 2 commentaires ·
Les mainteneurs répondent en général sous 2 jours
-
WebTransport ignores query parameters in URLPeut-être pris @cyphercodes l’a pris il y a 177 jours. Ouverteneeds major release question
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
socketio/socket.io#5496 · 3 commentaires ·
Les mainteneurs répondent en général sous 2 jours
-
Direct Sockets APIOuverteenhancement
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 20/100
Les mainteneurs répondent en général sous 2 jours
Toutes les issues de socketio/socket.io
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
excalidraw/excalidraw#12590 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
Automattic/data-liberation-agent#1657 ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 79/100
HGInsights/hgi-cli#23 ·
Les mainteneurs répondent en général sous 1 jour
-
Impact: 3-Minor Scope: Core
Difficulté 2/5 1-3 heures Accessibilité débutants 68/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 1/5 1-3 heures Accessibilité débutants 74/100
sugarlabs/musicblocks-v4#1029 · 1 commentaire ·
Les mainteneurs répondent en général sous 2 jours