﻿# Asynchronous context leakage: a tool span can inherit the wrong request

A practical investigation of asynchronous context leakage: a tool span can inherit the wrong request, with explicit observations, control cases and evidence limits.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/field-observe-async-context-tool-span-misattribution/
Language: en

## The diagnostic situation

A Node.js gateway handles concurrent inference requests and schedules tool callbacks through a shared event emitter. A callback creates its span using whichever context is active. The trace tree remains valid, but a tool action is attached to another request. A single-request smoke test passes because it never exercises the interleaving that causes the attribution defect.

## Why the observation changes meaning

OpenTelemetry context attachment associates context with an execution unit and provides restoration through detach. Node.js documentation explains that enterWith persists through the remainder of synchronous execution and following asynchronous calls, including later event handlers in the described pattern. A context store is not automatically scoped to a business request simply because the stored value contains its identifier.

## Define the instrumentation contract

Define where request context begins, where it is restored and how delayed callbacks obtain the intended context. Prefer the documented scoped execution mechanism for the selected integration rather than a shared mutable current-request variable. Treat explicit tool operation identity as a cross-check, not a replacement for valid parent context. No request content is needed in the context-isolation fixture.

## A bounded verification procedure

Construct synthetic requests with distinct safe markers and interleave their callback scheduling and completion. Include nested callbacks, an exception and a callback that runs after the response observation ends. Export the resulting span relationships and compare them with the predetermined ownership table. A valid parent identifier is insufficient if it belongs to the wrong synthetic request in that table.

## Evidence required for acceptance

Acceptance evidence includes the scheduling sequence, context mechanism, runtime and instrumentation versions, and exported ownership map. Verify restoration on both ordinary and exception paths. Record absent context separately from wrong context: automatically choosing a nearby active trace can hide the difference. Repeat after changing callback libraries because custom thenables, worker pools or event bridges can alter propagation behavior.

## Failure cases and limits

Correct trace context does not authorize the tool action or establish that its business input belongs to the same tenant. Those controls require independent application checks. This protocol tests diagnostic attribution within the selected asynchronous path. It does not guarantee universal context propagation across processes or every library, and it should not justify moving sensitive payloads into baggage to make debugging easier.

## Primary references and related reading

Validate context isolation under interleaving; a plausible trace tree can still associate tool work with the wrong request. [OpenTelemetry context specification](https://opentelemetry.io/docs/specs/otel/context/); [Node.js asynchronous context tracking](https://nodejs.org/api/async_context.html).

The proposed investigation of async context tool span misattribution is not an executed deployment benchmark. Related reading: [Request IDs and trace context across gateway, API and engine](/journal/provider-request-id-trace-context/); [Ray Serve FastAPI factories: build nonserializable instrumentation inside each replica](/journal/fg2-ray-serve-fastapi-factory-replica-local-instrumentation/).
