Commit Graph
1 Commits
Author SHA1 Message Date
Lauri Ojansivu 302bd90410 A removed DDP session no longer crashes the server on its 101st message.
From the #6492 comments today, on WeKan 10.40 with real users:

  SyncedCron: Fatal error encountered (uncaughtException):
  TypeError: self._pendingRemoveFunction is not a function
      at Session.send (packages/ddp-server/livedata_server.js:362:14)
      at Session._stopSubscription (packages/ddp-server/livedata_server.js:823:10)
      at Session.unsub (packages/ddp-server/livedata_server.js:562:12)
  snap.wekan.wekan.service: Main process exited, code=exited, status=1/FAILURE
  snap.wekan.wekan.service: Scheduled restart job, restart counter is at 4.

It is a state-machine hole in Meteor's ddp-server. `_removeSession` sets
`session.messageQueue = []` and `session._pendingRemoveFunction = remove`; when
`remove()` runs it clears the function and deletes the session but LEAVES the
queue - an empty array, which is truthy. `Session.send` then takes the queueing
branch for a session that no longer exists, and the message past
`maxMessageQueueLength` (100 by default) calls that now-null function. It runs
from an Immediate, via unsub -> _stopSubscription -> send, with no try/catch
above it, so it is an uncaught exception - and synced-cron's handler exits the
process on one. systemd restarts, every user is disconnected, new cards stop
arriving until a reload, and the restart loop reads as "WeKan is slow and the CPU
is high", which is exactly what the reporter describes.

It needs a busy server: a session has to disconnect while more than ~100 further
messages are still addressed to it. That is why it shows up with fifty users and
never in a quiet test.

The guard patches Session.send once, from the first connection (the Session class
is not exported, so an instance is how the prototype is reached): a queue with no
remove function belongs to a session that is already gone, so the queue is
dropped and send() takes its ordinary path - which, with no socket, does nothing.
Nothing is lost that was not already lost, the disconnect-grace queue for a real
reconnect is untouched, and a valid overflow still removes the session.

tests/ddpSessionSendGuard.test.cjs replays the upstream state machine: it asserts
the unguarded version really does throw on the 101st message, that the guarded
one does not, that a session still waiting for its client keeps its queue, that a
legitimate overflow still calls the remove function exactly once, and that a live
session sends as before.

NOT verified here: there is no server in this environment, so this is reasoned
from the ddp-server source in .meteor/local and replayed against a model of it.
The high CPU the issue opened with is a separate thread (the FerretDB
$changeStream busy loop); this fixes the crash-restart loop reported today.

Thanks to bluetopaz1204, Nissulya and xet7 !

Fixes #6492,
2026-07-27 17:17:36 +03:00