OplogObserveDriver: `_beSteady` deadlocks the write fence against its own ObserveMultiplexer queue (#14564, oplog-driver edition)
Summary
Under the oplog observe driver, a DDP write fence can deadlock
permanently against an ObserveMultiplexer queue. After that:
- the method's
updatedmessage is never sent, - every later write fence that touches the affected observer hangs,
- every new observer on the same cursor (i.e. a new client's subscription) never becomes ready.
Nothing is logged, and the event loop and plain reads stay healthy. Only a process restart clears it.
This is the same circular wait that #14564 fixed for
ChangeStreamObserveDriver._flushWritesToCommit in 3.5.1.
OplogObserveDriver._beSteady has the identical pattern and was not changed.
It still deadlocks on 3.5.1, 3.5.2 and 3.6-beta.1, and devel has the same
code. #14610 (the regression test for #14564) also covers only the
change-stream driver.
Versions
- Reproduced on Meteor 3.5.2 (
[email protected],[email protected],[email protected]), 3.5.1 ([email protected]) and 3.6-beta.1 ([email protected]). packages/mongo/oplog_observe_driver.js,observe_multiplex.ts,packages/ddp-server/writefence.jsandpackages/meteor/asynchronous_queue.jsare unchanged ondevel.- Needs
packages.mongo.reactivityto select the oplog driver (["oplog", "polling"]). The 3.5 default tries change streams first, so a default app takes the #14564-fixed path instead. - Last version without it: not known. We haven't tested anything older than 3.5.1.
- OS: reproduced on macOS (dev); seen in production on Linux (Node 24).
Reproduction
Repo: https://github.com/morehek/meteor-oplog-fence-deadlock, a bare Meteor 3.5.2 app with one server file.
npm install
npm run repro # meteor run --once --settings settings.json; exit code 1 = deadlockIt runs three scenarios. Each one does what ddp-server's method handler
does (create a _WriteFence, run the body inside it, arm() it), with an
oplog-driven observeChanges({x: 1}) standing in for a subscription:
[repro] control: driver=OplogObserveDriver fence=ok arm=ok newObserverReady=ok queue(running=false pending=0)
[repro] fix : driver=OplogObserveDriver fence=ok arm=ok newObserverReady=ok queue(running=false pending=0)
[repro] bug : driver=OplogObserveDriver fence=TIMEOUT arm=ok newObserverReady=TIMEOUT queue(running=true pending=2)
[repro] event loop / reads while wedged: ok
[repro] RESULT: DEADLOCK REPRODUCED- control: an awaited update of an unpublished doc with
$set: {x: 1}, so the driver has to fetch it. - bug: the same, plus one un-awaited async write that lands after
arm(). That's an app helper called withoutawait: a very common pattern, and all over the code paths that were active when this hit us. - fix: the same late write run under
DDPServer._CurrentWriteFence.withValue(undefined, …), as an app-side workaround.
By default the repro delays the oplog driver's doc fetch by 300 ms, standing in
for a slow read, to make the timing deterministic. It also deadlocks with no
delay at all (REPRO_FETCH_DELAY_MS=0) when the late write lands within ~1 ms
(REPRO_LATE_WRITE_DELAY_MS=0 or 1).
Expected: the fence fires once the driver is steady; updated is sent;
later observers become ready.
Actual: the fence never fires; the multiplexer queue stays
running=true with pending tasks forever; a second observeChanges on the
same cursor never returns.
Mechanism
Links are to release/[email protected] (4b13d6bb).
- A method's fence contains a write that the oplog driver
Dhas to fetch (unpublished doc, modifier touches the selector), soDgoesFETCHING. fence.arm()→_maybeFire()takesoutstanding_writes++and runs the before-fire round. The oplog driver's handler (oplog_observe_driver.js#L185-L207) deletesfence._oplogObserveDrivers(L187), waits for oplog catch-up, and, becauseDisn't steady, parks abeginWrite()inD._writesToCommitWhenWeReachSteady(L204). The round ends with that write still outstanding, so the fence stays open.arm()resolves, so theresultis sent.- A late write in the same fence runs now: an un-awaited async call whose
updateAsyncstarts after the method body returned, still in the fence's async context. ItslistenAllcallback finds no_oplogObserveDrivers, so it creates a new set and registers a secondonBeforeFirehandler. The fence isn't fired yet, so that's allowed. Dfinishes fetching →_beSteady()(#L599-L613) →multiplexer.onFlush(async () => { for (w of writes) await w.committed() }).onFlushis arunTaskon the multiplexer's queue (observe_multiplex.ts#L151-L162).- That parked commit is the fence's last outstanding write, so
committed()awaits_maybeFire()(writefence.js#L24-L31, #L69-L101), which runs the round-2 handler from step 3 inside the running queue task. That handler callsD._multiplexer.onFlush(write.committed)(L202), arunTaskon the same queue. AsynchronousQueue.runTaskhas no re-entrancy guard (asynchronous_queue.js#L122-L145). The new task queues behind the running one, and the running one is awaiting it. It's a circular wait, forever.
The STEADY branch at L202 is safe: it runs inside a _maybeFire round that
holds outstanding_writes++, so a commit there can never be the one that fires
the fence. Only the commit in _beSteady, which happens outside any round, can
fire it from inside the queue. The polling driver also commits inside
onFlush, but it registers no onBeforeFire handler that re-enters its own
multiplexer, so it can't close the loop on its own.
ddp-server sends result after await fence.arm() and updated from
onAllCommitted
(livedata_server.js#L592-L601, #L625-L628).
So the client sees result but never updated, just as described in #14564.
Impact
We believe this is what froze one instance of a production app pinned to
["oplog", "polling"] for about 36 minutes. We never caught the wedged queue in
the live process. The diagnosis comes from its logs, which rule out the other
causes, plus this repro, which reproduces the same process-level symptoms:
- Every client that connected logged in, then sat on the loading screen: the
resume login got its
result, but subscriptions to the wedged cursor never becameready. - Methods writing that collection never got
updated. - A second instance on the same database kept working.
- MongoDB showed nothing unusual, and the process logged no error. In
particular,
Meteor: oplog catching up took too longnever fired, because the wait isn't in_waitUntilCaughtUp.
It only cleared on restart.
Suggested fix
Mirror #14564 in OplogObserveDriver._beSteady: start the commits in a
microtask instead of awaiting them inside the onFlush task. Commits still
start only after the flush point, so clients still get added/changed before
updated, but the queue task no longer holds the queue while the fence fires.
--- a/packages/mongo/oplog_observe_driver.js
+++ b/packages/mongo/oplog_observe_driver.js
@@ -601,13 +601,22 @@ Object.assign(OplogObserveDriver.prototype, {
self._registerPhaseChange(PHASE.STEADY);
var writes = self._writesToCommitWhenWeReachSteady || [];
self._writesToCommitWhenWeReachSteady = [];
- await self._multiplexer.onFlush(async function () {
- try {
- for (const w of writes) {
- await w.committed();
- }
- } catch (e) {
- console.error("_beSteady error", {writes}, e);
+ await self._multiplexer.onFlush(function () {
+ // Commit in a microtask instead of awaiting inside this queue task.
+ // committed() on the fence's last outstanding write fires the fence, and
+ // the fence's onBeforeFire handler (registered by the listenAll callback
+ // in the constructor) re-enters this same multiplexer queue via onFlush.
+ // Awaiting that chain from inside the current queue task deadlocks the
+ // queue: the fire waits on a task queued behind this one, which can never
+ // run. Deferring keeps the ordering guarantee - commits still start only
+ // after the flush point - without holding the queue while the fence
+ // fires. Same fix as #14564 for ChangeStreamObserveDriver.
+ for (const w of writes) {
+ Promise.resolve()
+ .then(() => w.committed())
+ .catch((e) => {
+ console.error("_beSteady deferred write commit failed", {writes}, e);
+ });
}
});
},With this applied as a local packages/mongo override, the repro passes on
3.5.2 (all three scenarios, exit code 0), including the zero-delay variants. We
also get the same result on 3.5.1 with [email protected]. The patch applies
unchanged to devel. The repro repo includes it
as oplog-beSteady.patch, with steps to verify.
A more general safeguard would be a re-entrancy guard (or at least a loud
error) in AsynchronousQueue.runTask when it's called from a task running on
the same queue. That would turn the next variant of this into an exception
instead of a silent hang.
Happy to open a PR with the fix and an oplog-driver regression test alongside #14610.
Source: meteor/meteor