Repository navigation
Description
- I determined this is the correct repository in which to report this bug.
Summary of the issue
Since 3.72.0, with opentelemetry-instrumentation-grpc enabled, almost every Spanner read logs:
ERROR opentelemetry.context: Failed to detach context
ValueError: <Token var=<ContextVar name='current_context' ...> at 0x...> was created in a different Context
3.71.0 does not.
The cause is the background stream draining from #18320. When PartialResultSet.last arrives, _drain_stream passes the gRPC response iterator to the spanner-stream-drainer-N threads. With gRPC instrumentation, that iterator is the instrumentation's _intercept_server_stream generator, which holds with tracer.start_as_current_span(...) across yield from (open-telemetry/opentelemetry-python-contrib#2357). The drainer thread finishes the generator, so the span's context token is detached in the wrong context.
This has two effects:
- An ERROR log for each drained stream.
- The calling thread's context is never restored. The ended
ExecuteStreamingSqlspan stays current there, so later spans get the wrong parent.
API client name and version
google-cloud-spanner 3.72.0
Reproduction steps: code
This calls _drain_stream with a generator shaped like the gRPC instrumentation's _intercept_server_stream, so it needs no Spanner instance.
import logging
import time
from google.cloud.spanner_v1._helpers import _drain_stream
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider
logging.basicConfig()
trace.set_tracer_provider(TracerProvider())
tracer = trace.get_tracer("repro")
def instrumented_server_stream():
with tracer.start_as_current_span("/google.spanner.v1.Spanner/ExecuteStreamingSql"):
yield from ["partial_result_set", "partial_result_set_with_last", "trailing_metadata"]
stream = instrumented_server_stream()
next(stream)
next(stream)
_drain_stream(stream)
time.sleep(1)
print("current span in the calling thread after the drain:", trace.get_current_span().name)Reproduction steps: supporting files
None.
Reproduction steps: actual results
ERROR:opentelemetry.context:Failed to detach context
Traceback (most recent call last):
File ".../opentelemetry/context/__init__.py", line 127, in detach
_RUNTIME_CONTEXT.detach(token)
File ".../opentelemetry/context/contextvars_context.py", line 40, in detach
self._current_context.reset(token)
ValueError: <Token var=<ContextVar name='current_context' default={} at 0x10d675df0> at 0x10f4da200> was created in a different Context
current span in the calling thread after the drain: /google.spanner.v1.Spanner/ExecuteStreamingSql
Reproduction steps: expected results
No ERROR log, and the calling thread's current span is restored.
OS & version + platform
Linux, macOS
Python environment
Python 3.14
Python dependencies
- google-cloud-spanner 3.72.0
- grpcio 1.85.1
- opentelemetry-api / opentelemetry-sdk 1.45.2
- opentelemetry-instrumentation-grpc 0.66b2
Activity
ohmayr commentedon Nov 30, 2026
Thank you for reporting this issue. We have triaged it and routed it to the Spanner team for review.