Skip to content

spanner: background stream draining (3.72.0) breaks OpenTelemetry context with gRPC instrumentation ("Failed to detach context") #19155

Todo

Description

@zoola969
  • 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:

  1. An ERROR log for each drained stream.
  2. The calling thread's context is never restored. The ended ExecuteStreamingSql span 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

  1. added
    triage me
    on Nov 20, 2026
  2. added
    api: spanner
    priority: p2
    and removed
    triage me
    on Nov 30, 2026
  3. ohmayr commented on Nov 30, 2026

    @ohmayr
    Contributor

    Thank you for reporting this issue. We have triaged it and routed it to the Spanner team for review.

  4. added
    type: bug
    on Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: spannerpriority: p2type: bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions