<!-- llms-explorer concept facts · https://llms-explorer.com/tree/pinned-event-mongodb-worker-claim-expiry-and-stale-completion-boundaries/ · pack 2026-11-02 · ~2366 tokens -->

# Pinned event-mongodb worker claim expiry and stale completion boundaries

> Source-anchored research for Pinned event-mongodb worker claim expiry and stale completion boundaries. Run status: COMPLETED.

Parent: [Change Data Capture Patterns](https://llms-explorer.com/tree/change-data-capture-patterns/) · 4 facets · 15 facts · page: https://llms-explorer.com/tree/pinned-event-mongodb-worker-claim-expiry-and-stale-completion-boundaries/

## How it works

- MarkProcessed filters only by `_id`, sets completed status and expiry to now plus s.completionTTL, and calls UpdateOne without upsert or a matched-count check. It does not compare worker identity, processing status, or lease expiry. Code inference: an old worker can complete a replacement claim with the same message ID; completion of a missing document returns nil when the database operation succeeds, without recreating the claim. (low confidence) — [source](https://raw.githubusercontent.com/rbaliyan/event-mongodb/ca444aedec31e7e959c0932a8f7577c15312f37e/distributed/mongodb.go)

## Problems, failure modes and limitations

- Acquire uses an upserting find-and-update operation returning the post-update document and returns whether the returned WorkerID matches the newly generated worker ID. Duplicate-key errors return false without error. On `mongo.ErrNoDocuments`, a follow-up read rejects a document whose ExpiresAt is after the captured now; otherwise tryInsert attempts a new processing document, treating duplicate key as non-acquisition. Other database errors propagate. (low confidence) — [source](https://raw.githubusercontent.com/rbaliyan/event-mongodb/ca444aedec31e7e959c0932a8f7577c15312f37e/distributed/mongodb.go)
- WorkerPoolMiddleware calls the handler directly when the context has no event ID. If Acquire returns an error, it optionally logs a warning, signals AcquisitionAcquired when a signal exists, and returns the handler result directly. These early returns bypass the later payload storage, MarkProcessed, and Reset branches. Failure-open handling therefore does not establish a successful database claim. (medium confidence) — [source](https://raw.githubusercontent.com/rbaliyan/event/b112463682d44b0e57d8545e3cdf5d51e112e914/distributed/middleware.go) · [also](https://raw.githubusercontent.com/rbaliyan/event/b112463682d44b0e57d8545e3cdf5d51e112e914/distributed/middleware_test.go)
- WorkerPoolMiddleware defaults payload storage to false unless explicitly configured; WithBus derives storage from redelivery support and WithPayloadRecovery enables it. After acquisition, storage requires a PayloadStore, nonempty raw payload, and an acceptable size; only a successful StorePayload sets payloadStored. A storage error logs and still proceeds to the handler. (low confidence) — [source](https://raw.githubusercontent.com/rbaliyan/event/b112463682d44b0e57d8545e3cdf5d51e112e914/distributed/middleware.go)
- After a claimed handler succeeds, WorkerPoolMiddleware clears payload if payloadStored (ignoring the clear error), then calls MarkProcessed; a marking error is logged but the middleware still returns the nil handler result. On handler failure it leaves state for recovery when payloadStored, otherwise calls Reset, logs a reset error if present, and returns the handler error. (low confidence) — [source](https://raw.githubusercontent.com/rbaliyan/event/b112463682d44b0e57d8545e3cdf5d51e112e914/distributed/middleware.go)
- The pinned middleware fixtures use mockCoordinator and mockCoordWithPayload test doubles. They assert handler dispatch on acquisition error, skipping on non-acquisition, marking on success, reset on handler failure without stored payload, and preservation without reset when payload was stored. These observations do not execute WorkerPoolMiddleware against MongoStateManager across lease expiry. (low confidence) — [source](https://raw.githubusercontent.com/rbaliyan/event/b112463682d44b0e57d8545e3cdf5d51e112e914/distributed/middleware_test.go)

## Comparisons and alternatives

- After an error-free Acquire returning false, WorkerPoolMiddleware signals AcquisitionSkipped when available and returns nil without calling the handler. Successful acquisition signals AcquisitionAcquired and continues. The mock-based NotAcquired fixture asserts that its handler was not called; this is a branch assertion rather than MongoDB execution evidence. (medium confidence) — [source](https://raw.githubusercontent.com/rbaliyan/event/b112463682d44b0e57d8545e3cdf5d51e112e914/distributed/middleware.go) · [also](https://raw.githubusercontent.com/rbaliyan/event/b112463682d44b0e57d8545e3cdf5d51e112e914/distributed/middleware_test.go)
- Bounded code inference: the middleware has no lease-renewal loop around its synchronous handler call, Acquire returns a boolean rather than a claim token, and MongoStateManager completion/reset accept only a message ID. Therefore the inspected implementation and fixtures do not establish end-to-end exclusivity for a handler that outlives its lease. An expired owner may still run while another caller reacquires, and the old completion/reset predicates do not fence that owner. (medium confidence) — [source](https://raw.githubusercontent.com/rbaliyan/event/b112463682d44b0e57d8545e3cdf5d51e112e914/distributed/middleware.go) · [also](https://raw.githubusercontent.com/rbaliyan/event-mongodb/ca444aedec31e7e959c0932a8f7577c15312f37e/distributed/mongodb.go) · [also](https://raw.githubusercontent.com/rbaliyan/event-mongodb/ca444aedec31e7e959c0932a8f7577c15312f37e/distributed/mongodb_integration_test.go)

## Facts and statements

- The Acquire method of MongoStateManager matches `_id` plus an alternative: `expires_at` is strictly `$lt` the local `time.Now()`, `status` is released, or status does not exist. It sets processing status, a generated `worker_id`, expiry at now plus ttl, and updated time; creation time is set only on insert. The released-status alternative explicitly handles same-millisecond Reset/reacquisition despite BSON date truncation. Its expiry alternative does not exclude completed documents after their expiry. (low confidence) — [source](https://raw.githubusercontent.com/rbaliyan/event-mongodb/ca444aedec31e7e959c0932a8f7577c15312f37e/distributed/mongodb.go)
- Reset filters only by `_id`. In regular mode it deletes that document; in capped mode it updates status to released and both expiry and updated time to now. Neither branch checks worker identity or current status. Code inference: a delayed old worker Reset can remove or release a newer claim for the same ID. (low confidence) — [source](https://raw.githubusercontent.com/rbaliyan/event-mongodb/ca444aedec31e7e959c0932a8f7577c15312f37e/distributed/mongodb.go)
- ListStale selects processing documents with `updated_at` strictly before now minus staleTimeout and optionally limits results; it does not use `expires_at`. ResetStale first lists IDs, then filters only by `_id` in that list for DeleteMany in regular mode or released-status UpdateMany in capped mode. Code inference: the second operation does not recheck staleness, status, or ownership, so an intervening completion or reacquisition is not excluded by its mutation filter. (low confidence) — [source](https://raw.githubusercontent.com/rbaliyan/event-mongodb/ca444aedec31e7e959c0932a8f7577c15312f37e/distributed/mongodb.go)
- The pinned integration fixture TestIntegration_ConcurrentAcquire releases a barrier for 20 goroutines to Acquire the same ID with a one-minute lease; it asserts no unexpected errors and exactly one successful Acquire. It exercises acquisition contention, not simultaneous handler execution or completion after lease replacement. These are assertions in fixture source, not an execution-result report. (low confidence) — [source](https://raw.githubusercontent.com/rbaliyan/event-mongodb/ca444aedec31e7e959c0932a8f7577c15312f37e/distributed/mongodb_integration_test.go)
- The pinned TestIntegrationAcquireExpiredLease fixture acquires with `50*time.Millisecond`, sleeps `120 * time.Millisecond`, and expects a new Acquire with time.Minute to succeed. It does not keep an old handler running or invoke its completion after the second acquisition. This source assertion supports expiry reacquisition, not exclusive processing throughout a long handler. (low confidence) — [source](https://raw.githubusercontent.com/rbaliyan/event-mongodb/ca444aedec31e7e959c0932a8f7577c15312f37e/distributed/mongodb_integration_test.go)
- The pinned integration fixtures assert that MarkProcessed with an hour completion TTL blocks immediate reacquisition, Reset permits immediate reacquisition, and ListStale/ResetStale detect and reset two entries using a tiny stale timeout after creating claims with one-minute TTLs. These sequential fixtures do not interleave stale selection with a competing completion or reacquisition. (low confidence) — [source](https://raw.githubusercontent.com/rbaliyan/event-mongodb/ca444aedec31e7e959c0932a8f7577c15312f37e/distributed/mongodb_integration_test.go)
- The pinned TestIntegrationCappedCollectionLifecycle fixture creates a capped collection, resets and reacquires a claim, and repeats 50 tight Reset/Acquire cycles. Its stated regression target is BSON millisecond truncation. It does not assert owner-specific reset or completion fencing. (low confidence) — [source](https://raw.githubusercontent.com/rbaliyan/event-mongodb/ca444aedec31e7e959c0932a8f7577c15312f37e/distributed/mongodb_integration_test.go)
