Repository navigation
feat(plugin-gateway): add dispatch wrappers and destroy hooks - #285
Conversation
addDispatchWrapper runs code around the processing of each dispatch received from Discord, so a tracing span or an AsyncLocalStorage store covers the cache writes and the listeners. addDestroyHook runs at the end of every destroy(), once the shards are disconnected and the dispatches processed, to flush what is left before the process exits. Refs #261
|
Architecture Data flow No data-flow sequence changed in this PR. Review ✨ Nothing flagged · reviewed Tip Run 🪧 More tips
Thanks for using PR Lens! It's free for OSS. If it helped, a star helps other teams find it. |
There was a problem hiding this comment.
✅ No new issues found.
Reviewed changes
Reviewed the full diff of the single commit, then ran tests/hooks.test.ts (8 passing), oxlint and oxfmt --check on the branch.
addDispatchWrapper— wrappers live in an immutable array snapshot read once per dispatch, nest first-outermost aroundhandleDispatch, and sit inside the dispatch timeout andreportError, so failures still reacherror. A synchronous throw from a wrapper is caught by the sametry.addDestroyHook— hooks run in afinallyat the end ofdisconnect(), aftergateway.destroy, the session flush andidle(), so they also run when disconnecting fails and the original rejection still propagates. A throwing hook goes toerror(or the logger) and the next hooks still run. Concurrentdestroy()calls join onedisconnect(), so hooks run once per destroy.#keepSessions— moving the assignment into thetrykeeps the reset in the innerfinally, so behavior is unchanged.- Docs and release — README section,
minorchangeset and the AI disclosure line are in place.
claude-sonnet-5-5 | 𝕏
| } finally { | ||
| this.#keepSessions = false; | ||
| // Even when disconnecting failed: the process is likely to exit next, and what is left should still go out. | ||
| await this.#runDestroyHooks(); |
There was a problem hiding this comment.
If gateway.destroy() rejects while a dispatch is still running, disconnect() skips idle() and immediately runs the destroy hooks. A hook that flushes traces can finish before that dispatch ends, leaving its final spans unexported when the process exits. Wait for queued dispatches before running the hooks on the failure path too, while preserving the original disconnect error.
Prompt To Fix With AI
This is a comment left during a code review.
Path: packages/plugin-gateway/src/GatewayClient.ts
Line: 679-681
Comment:
**Shutdown hooks run too early**
If `gateway.destroy()` rejects while a dispatch is still running, `disconnect()` skips `idle()` and immediately runs the destroy hooks. A hook that flushes traces can finish before that dispatch ends, leaving its final spans unexported when the process exits. Wait for queued dispatches before running the hooks on the failure path too, while preserving the original disconnect error.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.
🔗 Linked issue
Refs #261
🧭 Context
Second half of #261, as a stack, bottom to top:
plugin-gatewaydispatch wrappers and destroy hooksplugin-sharderexit and destroy hooksplugin-opentelemetrydispatch spans and theflushTraces()wiring, which resolves request: a plugin that traces the bot with OpenTelemetry #261plugin-opentelemetrycannot give a dispatch a span of its own today. Therawanddispatchevents don't wrap the async work, so the cache and database calls of a dispatch have no parent. Nor can it flush the traces when the gateway shuts down.📚 Description
GatewayClient#addDispatchWrapper(wrapper)runs a wrapper around the processing of each dispatch received from Discord. The wrapper gets{ payload, shardId, partition }and arunfunction to call once. Wrappers nest in the order they were added, and the dispatch timeout and error reporting stay outside all of them, so a failing dispatch is still reported througherror.GatewayClient#addDestroyHook(hook)runs a hook at the end of everydestroy(), after the shards are disconnected and the queued dispatches are processed. Hooks also run when disconnecting fails. A hook that throws is reported througherrorand the next hooks still run.Both return a function that removes what was added. Nothing changes for a client that adds neither.
As Pullfrog's plan noted, the READY reconcile and
replayDispatchdon't go throughrunDispatch, so they aren't wrapped. The README says so.Key changes
GatewayClient:addDispatchWrapper,addDestroyHook, and theWrappedDispatch,DispatchWrapperandGatewayClientDestroyHooktypes.tests/hooks.test.ts: nesting order, anAsyncLocalStoragestore seen by listeners, failure reporting, removal, and hook order and failures ondestroy.minorchangeset.Type of Change
Pre-flight Checklist
pnpm changeset(orpnpm changeset add --emptyif no release is needed). See .changeset/README.md.Fix the failed-shutdown ordering before merging so destroy hooks wait for pending dispatches.
Reviews (1) · Last reviewed commit: "feat(plugin-gateway): add dispatch wrapp..." · Reviewed by Greptile