Skip to content

fix(backend): preserve request context in indexer worker and SSE timers - #1138

Open
Akpolo wants to merge 1 commit into
LabsCrypt:mainfrom
Akpolo:fix/issue-1039-logger-context
Open

fix(backend): preserve request context in indexer worker and SSE timers#1138
Akpolo wants to merge 1 commit into
LabsCrypt:mainfrom
Akpolo:fix/issue-1039-logger-context

Conversation

@Akpolo

@Akpolo Akpolo commented Jul 29, 2026

Copy link
Copy Markdown

Summary

logger.ts uses an AsyncLocalStorage-based requestContext to attach a requestId to every log line, but that context is only ever populated by requestIdMiddleware for HTTP requests. Background timers in soroban-event-worker.ts and sse.service.ts run outside of any HTTP request, so their log lines never carried a correlation id.

  • soroban-event-worker.ts: the setTimeout-driven poll() loop now runs inside requestContext.run() with a stable workerId generated once per worker instance, so every poll cycle (and everything it awaits, including the RPC/DB calls in fetchAndProcessEvents) logs with a consistent id.
  • sse.service.ts: the setInterval heartbeat callback now runs inside requestContext.run() with a stable heartbeatWorkerId, so heartbeat-triggered log lines (e.g. slow-client drops) carry a consistent id too.

The AsyncLocalStorage implementation in logger.ts itself is unchanged, as noted out of scope in the issue. The manual triggerPoll() path (invoked from an admin HTTP route) already runs within its caller's request context and is left untouched.

Test plan

  • npx tsc --noEmit in backend/ — no new errors introduced (pre-existing errors on main unrelated to this change, verified by diffing against baseline)
  • npx vitest run in backend/ — same 15 pre-existing failures as on main (require a live Postgres DB not available locally); no new failures
  • npx 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 on main

Closes #1039

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Backend] logger.ts request-scoped context is lost inside setTimeout/setInterval callbacks in the indexer worker

1 participant