fix(backend): preserve request context in indexer worker and SSE timers - #1138
Open
Akpolo wants to merge 1 commit into
Open
fix(backend): preserve request context in indexer worker and SSE timers#1138Akpolo wants to merge 1 commit into
Akpolo wants to merge 1 commit into
Conversation
logger.ts's AsyncLocalStorage-based requestContext only gets populated by requestIdMiddleware for HTTP requests, so log lines emitted from the soroban-event-worker's setTimeout poll loop and the SSE service's setInterval heartbeat never carried a correlation id, making it hard to trace background activity. Wrap both callbacks in requestContext.run() with a stable, worker-scoped id generated once per worker instance so every poll cycle and heartbeat tick (and everything they await) logs with a consistent id. Closes LabsCrypt#1039
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.
Summary
logger.tsuses anAsyncLocalStorage-basedrequestContextto attach arequestIdto every log line, but that context is only ever populated byrequestIdMiddlewarefor HTTP requests. Background timers insoroban-event-worker.tsandsse.service.tsrun outside of any HTTP request, so their log lines never carried a correlation id.soroban-event-worker.ts: thesetTimeout-drivenpoll()loop now runs insiderequestContext.run()with a stableworkerIdgenerated once per worker instance, so every poll cycle (and everything it awaits, including the RPC/DB calls infetchAndProcessEvents) logs with a consistent id.sse.service.ts: thesetIntervalheartbeat callback now runs insiderequestContext.run()with a stableheartbeatWorkerId, so heartbeat-triggered log lines (e.g. slow-client drops) carry a consistent id too.The
AsyncLocalStorageimplementation inlogger.tsitself is unchanged, as noted out of scope in the issue. The manualtriggerPoll()path (invoked from an admin HTTP route) already runs within its caller's request context and is left untouched.Test plan
npx tsc --noEmitinbackend/— no new errors introduced (pre-existing errors onmainunrelated to this change, verified by diffing against baseline)npx vitest runinbackend/— same 15 pre-existing failures as onmain(require a live Postgres DB not available locally); no new failuresnpx vitest run tests/sse.service.test.ts tests/sse.controller.test.ts tests/soroban-worker.helpers.test.ts tests/soroban-event-worker.test.ts— all pass except one pre-existing bigint/number assertion mismatch also present onmainCloses #1039