drainOnce published a row stamped with an earlier companion under that
companion's pubkey while the MQTT clientId was the companion connected
now. The documented ACL binds the topic to ${clientid}, so the broker
denies that publish, and with EMQX's default deny_action=ignore it still
sends a PUBACK. The row was removed from the queue and never stored.
Such rows now stay in the queue (counted as `waiting`) and go out once
their own companion is connected. The hold is asked for the connected
companion only. The debug log says once how many rows are waiting.
Tests: 430 pass (sandboxed run of the full suite). Not tested against a
live broker.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NEgJ9U9V5WmHyRjL96MaDz
With the hold, a queue can keep one companion's rows for a long time.
Connecting another, linked companion then published that backlog under
the new pubkey: someone else's receptions counted as its coverage, and
data from an unlinked companion got past requireLinkedCompanion. Rows
now carry the capturing companion (rx, rx_name; not part of any MQTT
payload), drain publishes each under its own pubkey, and the hold is
decided per row. Rows queued before this version behave as before.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
One optional precondition next to publisher.connected(): when held, the
pass publishes nothing and does not touch the queue. Push pending now
names the hold instead of blaming the broker.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A clean v1.11.1 field log drained the 59-record backlog four seconds after
connecting - then logged "published 49 record(s)" one second later, for a queue
that had held 59. 59 - 49 = 10 = COMMIT_EVERY, which pins the mechanism exactly.
drain() had no mutual exclusion and four uncoordinated callers: the 5s drainLoop,
the broker 'connect' event, the 'online' event and the "Push pending now" button.
Two overlapping passes each call queue.takeAll(), so both see and both PUBLISH the
rows the other has not committed yet. Here the second pass started just after the
first committed its opening batch of 10, found the other 49 still queued, and
delivered every one of them to the ingestor a second time.
The hole predates the incremental-commit change - the old all-or-nothing drain
could double-publish just as easily - but batching the commits is what made it
visible and measurable in a log.
serialiseDrain() now holds one in-flight pass; a caller arriving mid-pass joins it
rather than starting its own. Joining rather than queueing a follow-up is
deliberate: the second caller wants the outcome, and anything queued after the
running pass took its snapshot is picked up by the next 5s tick. The slot is
released on rejection too, so a failed pass cannot wedge every future drain.
Otherwise that log is healthy and confirms the v1.11.1 fixes: the 59 buffered
records went out in one pass, and the new instrumentation shows publisher #1 alone
with connack rc=0, no retired-publisher events and no keepalive timeouts - the
4-second reconnect loop is gone.
Tests: 237 pass (+3). SW cache v37 -> v38.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
A field log showed 59 records buffered and not one published, with ~18
"Keepalive timeout" errors against a single successful CONNACK. Probing the broker
from a fixed line ruled out the obvious causes: publishes to /packets, /rf and
/regions were all PUBACKed in ~85ms with returnCode=0 and no disconnect, and a
deliberate same-clientId takeover makes the victim report a bare 'close', never a
keepalive error. So neither the ACL nor session contention was involved.
What was: two defects that together made a flaky mobile link unrecoverable and
undiagnosable.
1. drain() discarded its own progress. It collected published ids and called
queue.remove ONCE after the loop, so a single rejecting publish threw and every
record the broker had already acknowledged stayed queued to be sent again.
Publishes are sequential, so a 59-record backlog needs ~5s of continuous link at
86ms round-trip and ~18s at 300ms - on a link that stayed up about a second at a
time, the design could never commit anything at all. The loop now lives in
src/drain.js with two invariants under test: a record is removed if and only if
the broker acked it, and one unpublishable record can never block the records
behind it. A link drop is charged to the link, not to the record, so repeated
drops cannot quarantine a good reception.
2. Every Publisher ever created reported into one module-level handler writing one
global brokerState, so an orphaned client's endless 4s reconnect loop overwrote
a healthy connection's state and filled the log with failures nothing could
attribute. Each Publisher now carries an id; events from a retired one are named
once and then ignored. startPublisher no longer ADOPTS a disconnected publisher
(which made connectAll print "CoreScope connected" over a looping client) - it
retires it, which also stops that loop.
Also: keepalive is now set explicitly and quoted in the log next to the CONNACK
return code. mqtt.js arms its keepalive manager only inside _onConnect and only if
one does not already exist (client.js:1101), never re-arming an existing one, so a
surviving manager can report a bogus timeout on a healthy link. That error is now
evidence instead of a red herring.
Tests: 234 pass (+14). SW cache v36 -> v37.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>