Skip to content

fix(adapter): do not retain unused namespaces with connection state recovery - #5653

Open
imeerkhurram wants to merge 1 commit into
socketio:mainfrom
imeerkhurram:fix/session-aware-adapter-timer
Open

imeerkhurram wants to merge 1 commit into
socketio:mainfrom
imeerkhurram:fix/session-aware-adapter-timer

Conversation

@imeerkhurram

Copy link
Copy Markdown

The kind of change this PR does introduce

  • a bug fix
  • a new feature
  • an update to the documentation
  • a code change that improves performance
  • other

Current behavior

SessionAwareAdapter starts an interval in its constructor and never clears it. The interval holds the adapter, and the adapter holds its namespace, so with connectionStateRecovery enabled a child namespace removed by cleanupEmptyChildNamespaces can never be garbage-collected:

const io = new Server(3000, {
  cleanupEmptyChildNamespaces: true,
  connectionStateRecovery: {},
});
io.of(/^\/room-\d+$/);

I connected and disconnected one client on each of 50 such namespaces, then forced a GC and counted the namespaces that were still alive: 50 of 50. Without connectionStateRecovery it is 0 of 50.

New behavior

The timer is started when the first session or packet is stored, stops once both are empty, and is cleared in close(). The same script now reports 0 of 50.

I did not only clear it in close(), because a server can be used again after io.close() with io.listen(). The timer would then never come back and packets would grow forever. A broadcast from a disconnect handler (which runs after the adapter is closed) starts it again, and it stops by itself when the packet expires.

Other information (e.g. related issues)

Tests added in packages/socket.io-adapter/test/index.ts, they fail without the change. I also ran the suites of socket.io, socket.io-client, the cluster adapter, the cluster engine and both emitters (Redis 7 and Postgres running locally).

…ecovery

The SessionAwareAdapter class started an interval in its constructor and never cleared it, which prevented
the adapter (and its namespace) from being garbage-collected once closed, for example when the
cleanupEmptyChildNamespaces option is enabled.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant