You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 00f97b3
Browse filesBrowse the repository at this point in the historyBrowse files
fix(service-queue): the database queue main loop claims with one compare-and-set write (#38820)
repo:objectstack@os-justin · os-justin ·
session_01QSeKYnrQ7UMtHofKQVjcH2
Fixes#38578
Clause-②: no
Metadata-protocol: none
## 维护者速读
- 用户能看到什么变化:多节点部署下,数据库队列里逐条处理的任务(发邮件、附件回收、API 流订阅)不再被两个节点各执行一次;单节点无感。
- 为什么做:解决数据库队列主循环"同一条消息被两个节点同时领取"的问题,与批量路径已做的修复保持一致。
- 出错会怎样:最坏情况是某个节点这一轮领不到消息、下一轮再领,不会丢消息;改动只在一个服务包内,可直接撤回。
- 要你做什么:无。
## What changes
- `packages/services/service-queue/src/db-queue-adapter.ts`: the main
loop's claim (`claimBatch`) now takes a tick's candidate rows with ONE
compare-and-set write over their id set, matched on `status: 'pending'`
and due as well as id, then one read of the ids this worker holds when
fewer rows matched. The write core is factored out of the batch path's
`claimSet` into one private helper, `claimCandidates`, which both call;
`claimSet`'s behaviour is unchanged. A sole writer's lease entries are
recorded for won rows only (for every candidate only when the write
throws, as before on the batch path).
- Pacing is unchanged: the read still over-fetches `batchSize * 3`, at
most `batchSize` rows are claimed per tick, and each message goes to its
handler alone.
- Four in-memory engine doubles (`job-queue-retention.test.ts` and three
`plugin-email` suites) implemented only the by-id update; they now open
`update` with `assertEngineUpdateDispatch` and answer the predicate
(`multi: true`) write, and the three `plugin-email` matchers answer
`$in`, the shape `db-queue-idle-backoff.test.ts` already carries.
Fixture-only: no assertion changed.
- `.changeset/38578-db-queue-main-loop-cas.md`: patch for
`@objectstack/service-queue`.
## Tests
- `db-queue-adapter.test.ts`: PR #38575's CONTROL case is flipped. Two
adapters over one store that read the same 5 pending per-message rows
now handle each message once (it pinned each handled twice), with one
claim write per adapter tick. A new case pins pacing: `batchSize: 2`
drains 5 messages as 2, 2, 1, 0 in claim order with 3 claim writes.
<details><summary>Ablation: drop status pending from the claim
match</summary>
On-disk edit removing `status: 'pending'` from the `claimCandidates`
where, restored with `git checkout HEAD` (porcelain clean after):
```
FAIL ... > the main loop claims a tick's rows with one compare-and-set write, so two adapters over one store hand each per-message message to one handler call
AssertionError: expected 10 to be 5
FAIL ... > two adapters over one store that read the same pending rows hand each batch message to one handler call
AssertionError: expected 40 to be 20
Tests 2 failed | 50 passed (52)
os-verify-lock: VERDICT command-exit 1
```
</details>
<details><summary>Package runs at 4700c4a (code identical to
b14de17)</summary>
```
pnpm --filter @objectstack/service-queue test -> Test Files 5 passed (5), Tests 107 passed (107)
pnpm --filter @objectstack/plugin-email test -> Test Files 54 passed (54), Tests 1053 passed (1053)
pnpm --filter @objectstack/trigger-api test -> Test Files 3 passed (3), Tests 73 passed (73)
typecheck service-queue, plugin-email (incl. check:test-typecheck), trigger-api -> Done, VERDICT command-exit 0
```
Before the fixture fix the new claim turned 6 service-queue and 16
plugin-email tests red (the by-id-only doubles refused the predicate
write).
</details>
## Acceptance notes
- Consumers: mail send (`email-plugin.queue-delivery.test.ts`,
`email-plugin.outbox-sweep.test.ts`), mail attachment reclaim
(`email-plugin.attachment-storage.test.ts`) and API-flow subscriptions
(`packages/triggers/trigger-api/src/api-trigger.test.ts`; its double is
its own queue stand-in, not the database adapter, so it is unaffected
and green).
- Ledger gates are green at e799299: `pnpm
check:engine-double-contract` exit 0, `node
scripts/check-tenant-audit-census.mjs` exit 0, and its `--self-test`
exit 0 (29 cases pass). The service-queue census now counts 12 write
call sites (was 13: the two claim writes are now one), and
`scripts/engine-double-contract.pinned.json` records the four doubles as
pinned.
- `scripts/engine-double-contract.seams.json` is unchanged by `--write`
(0 seams added or lost), so it is not in the diff.
- Four RECONCILED rows remain in the engine-double shrink-only baseline
(the three plugin-email suites and job-queue-retention): they pass and
only over-license, the baseline is off this card's surface and optional
to tighten, so they are left.
- Not in scope (per the card): the batch path, the lease length,
at-least-once re-delivery after a crash.
## Verification
- Read first: `db-queue-adapter.ts` `claimCandidates` (the where clause
is the fix), then `claimBatch`, then the flipped two-adapter case in
`db-queue-adapter.test.ts`.
- Two minutes: `pnpm --filter @objectstack/service-queue exec vitest run
--maxWorkers=2 src/db-queue-adapter.test.ts` -> all green; delete
`status: 'pending',` from the `claimCandidates` where and re-run -> the
two two-adapter cases go red (10 vs 5, 40 vs 20).
- Gates: `dispatch-gates --commands` derived 76 families; `--ran`
reconciled 74 run, 2 NOT-MEASURED (`check:dual-build-cjs-loads` exit 3
prerequisite, `check:query-options-erasure` repo-wide eslint over the
foreground budget), 0 unrun; both ledger gates green at e799299 after
the regeneration commit.
---
_Generated by [Claude
Code](https://claude.ai/code/session_01QSeKYnrQ7UMtHofKQVjcH2)_
---------
Co-authored-by: Claude <noreply@anthropic.com>
fix(service-queue): the database queue's main loop claims a tick's rows with one compare-and-set write, so two nodes never both run a per-message message
6
+
7
+
Clause-②: no
8
+
Metadata-protocol: none
9
+
10
+
The database queue adapter's main loop (every per-message `subscribe()` queue: mail send, mail attachment reclaim, API-flow subscriptions) claimed each due row with a write matched by id alone. On a deployment with more than one node, two nodes that both read a row as `pending` both claimed it, the second overwriting the first's lease, and both ran it — a mail could be sent twice. The main loop now claims its candidate rows the way the batch path already does: one `multi: true` write over the id set, matched on `status: 'pending'` and due as well as id, then one read of the ids this worker holds when fewer rows matched. The loser of a row matches nothing and does not run it. Pacing is unchanged: at most `batchSize` messages per `pollIntervalMs` tick, each handed to its handler alone. No configuration change is needed.
it('CONTROL — the main loop\'s claim, matched by id alone and unchanged here, lets both adapters claim the rows they both read',async()=>{
880
-
// The same race over the per-message path. It reproduces the double claim
881
-
// on the main loop, which this change leaves alone on purpose (per-message
882
-
// queues keep their behaviour and pacing); it also shows the harness above
883
-
// can produce the interleaving the batch-path case is safe against.
879
+
it('the main loop claims a tick\'s rows with one compare-and-set write, so two adapters over one store hand each per-message message to one handler call',async()=>{
880
+
// [#38578] The same race over the per-message path. Its claim used to be
881
+
// one write per row matched by id alone, and both adapters claimed and ran
882
+
// every row they both read (each message handled twice). It now takes the
883
+
// tick's rows with the batch path's claim: one write over the id set,
884
+
// matched on `pending` and due, so the loser of each row matches nothing.
0 commit comments