Repository navigation
fix: deleted events, undefined perKey keys, uncapped fan-out errors and defer opt-out - #98
Merged
Merged
Conversation
… or wedging Only Postgres cascades event deletes to deliveries. On MongoDB a pending delivery whose event was deleted reached the processor without an event and crashed it through an unhandled rejection, on every restart. On Redis the claim script leased the whole batch and then failed to parse the missing event, so attempts climbed and nothing ran or died. DynamoDB dropped such deliveries from the result, so they were leased forever. Each of these stores now marks the delivery dead in the claim, with an "event '...' not found" error. The processor marks dead any delivery a store returns without its event, and catches unexpected errors from a running delivery instead of leaving an unhandled rejection.
…er handler The key function's type allows undefined, but fan-out rejected it and left the event unfanned, so none of its handlers ever ran. undefined now means no key, like null. A priority, key or defer function that throws or returns an invalid value still leaves the event unfanned and retried, but only for the failing handler's retry.maxAttempts fan-out attempts. After that, the event fans out: the failing handler's delivery is created dead with the error as last_error (TxOBNewDelivery gains an optional error that stores honor), and the other handlers run.
The docs said a handler's defer function sees event.defer_until and can opt out of it, but fan-out never called the function when defer_until was set. Call it for every event: its result is used, and returning null or undefined keeps the publisher's defer_until. A static defer is still overridden by publish.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #98 +/- ##
==========================================
- Coverage 96.13% 96.04% -0.10%
==========================================
Files 34 34
Lines 2355 2478 +123
Branches 478 497 +19
==========================================
+ Hits 2264 2380 +116
- Misses 74 81 +7
Partials 17 17 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Resolve the fan-out loop against the schema round-trip change from #97: computeDelivery now receives the parsed event, and events whose stored data fails the schema still get default deliveries. Both 0.6 -> 0.7 migration notes are kept.
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What was wrong
Three ways one bad delivery could stop more than itself, and one documented
deferbehavior that didn't exist:event: undefined.runDeliveryreadevent.correlation_idoutside anytry, and the fire-and-forget promise turned that into an unhandled rejection. Every processor exited with code 1, again on every restart, and the attempt cap never ran.JSON.parse(undefined)threw for the batch. In the PoC all 5 deliveries sat at 9 attempts against a cap of 5; none ran and none died.perKeykey function that returnsundefinedblocked every handler of the event forever (TXOB-CONS-003, Medium). Its type isstring | null | undefined, andkey: (e) => e.data.accountIdwith an optional field returnsundefined. Fan-out rejected it, and the event was never fanned out, so the event's other handlers never ran either.priority, key ordeferfunction that throws, or returns an invalid value, leaves the event unfanned so it is retried. That much is intended and tested. But it was retried forever, with an error log everyleaseMsas the only signal.deferopt-out (TXOB-DOC-002, Low). README andhandlers.tssay a handler'sdeferfunction seesevent.defer_until, "so a handler can opt out of honoring it". Fan-out never called the function oncedefer_untilwas set.What changed
deadduring the claim, fenced by the new lease, withlast_error{ name: "TxOBEventMissingError", message: "event '<id>' not found" }. They don't return it. The processor marks dead any delivery a (custom) store returns without itsevent, and logs it. It also catches anything that escapesrunDelivery, so a store bug can't become an unhandled rejection again. Postgres is unchanged: its foreign key already deletes the delivery. The README retention notes now say to delete events only once none of their deliveries is pending.undefinedkey now means no key, likenull. Type and runtime agree.priority/key/deferfunction still leaves the event unfanned and retried, as the existing tests require. Each fan-out attempt now counts against the failing handler'sretry.maxAttempts. When that's used up, the event fans out: the other handlers get normal deliveries, and the failing handler's delivery is createddeadwith the error aslast_error. That's logged at error level and counted in the delivery telemetry asdead.TxOBNewDeliverygains an optionalerror, which all five stores honor (dead, never claimable).onDeliveryDeadis not called for these, because no transaction is open at fan-out. This is documented.deferfunctions are called for every event. A non-null result is used.null/undefinedkeeps the publisher'sdefer_until. A staticdeferis still overridden bypublish.eventMissingError(eventId)for custom stores.Compatibility
completeFanoutsetsstatus,last_errorandcompleted_aton insert, which are existing columns. Redis'scompleteFanoutscript takes one more argument per delivery. Each processor sends its own script, so mixed versions during a rollout are fine.TxOBNewDelivery.error?andeventMissingErrorare new, andrecordDeliveryis internal. Custom stores must honorTxOBNewDelivery.error, or a capped delivery would be created pending and run without its key. That's called out in MIGRATING, and the contract test "creates deliveries that carry an error dead, and never claims them" checks it.deferfunction that returns a value for an event published withdeferused to be ignored and now wins. A function that wants the publisher to win returnsnullwhenevent.defer_untilis set. The existing unit test'sfromEventhandler already does that, and it still passes. (b) AftermaxAttemptsfan-out attempts, a brokenpriority/key/deferdead-letters that handler instead of blocking the event forever.minor.How it was tested
Real backends in temporary containers: Postgres 17, MongoDB 9 replica set, Redis 8, DynamoDB Local.
pnpm install --frozen-lockfile,pnpm test:prepare,pnpm typecheck,pnpm build(ESM + CJS): pass.undefinedkey.src/reverted, 28 of the new test runs fail. The MongoDB, Redis and DynamoDB delete tests fail; the Postgres one passes because of the cascade. With the fix, all pass.poc-mongo-orphan.mjsnow ends with "still alive after 2s", exit code 0 (it was exit code 1).poc-redis-orphan.mjs: the other 4 events deliver, the orphan has 0 attempts and is dead, and no poll errors are logged.poc-undefined-key.mjs: both handlers run, and the event is fanned out on the first attempt.For the reviewer to decide
deferprecedence. I made the documented opt-out real, rather than deleting the sentence from the docs. That changes behavior fordeferfunctions that return a value for events published withdefer. The feature shipped in 0.6.0 three weeks ago. If you'd rather keep "publish always wins", revert ffb5d60 and I'll fix the docs instead.retry.maxAttempts, rather than a new option. Fan-out attempts happen once perleaseMs(30 s by default), so a broken key dead-letters after about 2.5 minutes by default.onDeliveryDead. The alternative I rejected was re-running the fan-out functions at every claim to route these through the normal dead path, which does run the hook. It would evaluate them on every attempt and couple this PR to the schema round-trip PR.maininto this branch. The fan-out loop now passes the parsed event tocomputeDelivery, and the MIGRATING notes are combined. Full suite after the merge: 655 passed, 9 skipped.Not in this PR