#43522·grpc

[Python] grpc.aio server deadlocks under concurrent load when grpcio-observability's OpenTelemetryPlugin is registered

Author: RubiniuMCreated Sep 24, 2026Updated Sep 29, 2026
Labelslang/Python

What version of gRPC and what language are you using?

Python. grpcio / grpcio-observability / grpcio-health-checking 1.84.0. Also reproduced on 1.83.1 and 1.83.0, so this is not a recent regression. opentelemetry-sdk 1.44.0.

What operating system (Linux, Windows,...) and version?

Linux (Debian trixie, aarch64), in a container limited to 1 CPU and 512 MB.

What runtime / compiler are you using (e.g. python version or version of gcc)

CPython 3.13.14.

What did you do?

Registered OpenTelemetryPlugin globally, started a grpc.aio server with only the standard health servicer on it, and called Health/Check from four concurrent coroutines. Full script attached below; it needs nothing but grpcio, grpcio-health-checking, grpcio-observability and opentelemetry-sdk.

python
import asyncio
import time

import grpc
from grpc_health.v1 import health, health_pb2, health_pb2_grpc
from grpc_observability import OpenTelemetryPlugin
from opentelemetry.sdk.metrics import MeterProvider

CONCURRENCY = 4
CALLS = 20000


async def main() -> None:
    OpenTelemetryPlugin(meter_provider=MeterProvider()).register_global()

    server = grpc.aio.server()
    servicer = health.aio.HealthServicer()
    health_pb2_grpc.add_HealthServicer_to_server(servicer, server)
    await servicer.set("", health_pb2.HealthCheckResponse.SERVING)
    port = server.add_insecure_port("127.0.0.1:0")
    await server.start()

    done = 0
    started = time.perf_counter()

    async def caller() -> None:
        nonlocal done
        async with grpc.aio.insecure_channel(f"127.0.0.1:{port}") as channel:
            stub = health_pb2_grpc.HealthStub(channel)
            while done < CALLS:
                await stub.Check(health_pb2.HealthCheckRequest())
                done += 1
                if done % 100 == 0:
                    print(f"{done} calls", flush=True)

    await asyncio.gather(*(caller() for _ in range(CONCURRENCY)))
    print(f"{done} calls in {time.perf_counter() - started:.0f}s", flush=True)


asyncio.run(main())

What did you expect to see?

The 20000 calls complete. Commenting out the register_global() line gives exactly that: 20000 calls in 4s.

What did you see instead?

Output stops after a few hundred calls and the process never makes progress again:

100 calls
200 calls
300 calls
<nothing, forever>

The process is not busy — it is parked. Container CPU drops to ~1%, memory is flat, and every thread is asleep. Nothing recovers it; only SIGKILL ends it.

In our service this is fatal in production: the gRPC health endpoint stops answering too, so the Kubernetes liveness probe kills the pod, and it happens again as soon as traffic returns. Concurrency of 4 was enough on a 1-CPU pod.

Anything else we should know about your project / environment?

Where it deadlocks. A faulthandler.dump_traceback(all_threads=True) taken while frozen shows the asyncio loop thread stopped inside a C callback, holding the GIL:

Current thread (most recent call first):
  File "/usr/local/lib/python3.13/asyncio/events.py", line 89 in _run
  File "/usr/local/lib/python3.13/asyncio/base_events.py", line 2060 in _run_once
  File "/usr/local/lib/python3.13/asyncio/base_events.py", line 683 in run_forever

Every other thread is idle in futex_wait_queue, including the prometheus exporter thread, which is what makes the whole process unresponsive rather than just slow.

Reading the sources, the two ends of the cycle look like this:

  • grpc_observability/_cyobservability.pyx, _flush_census_data() takes g_census_data_buffer_mutex and then, still holding it, runs Python: it converts labels and measurements and calls exporter.export_stats_data(...). Running Python requires the GIL.
  • grpc_observability/observability_util.cc, AddCensusDataToBuffer() takes the same mutex from the RPC path.

So one thread can hold the GIL and wait for the mutex while the other holds the mutex and waits for the GIL. _export_census_data() does release the GIL around AwaitNextBatchLocked, but the flush that follows it does not.

It is timing-dependent, which matches a lock cycle rather than a leak. GRPC_PYTHON_CENSUS_EXPORT_BATCH_INTERVAL_SECS shortens the window in which the mutex is held together with the GIL, and every step raises the concurrency the process survives without ever removing the failure (measured on a real service, 30000 calls per run): the default 0.5 dies at 4 concurrent; 0.05 survives 8 and dies at 16; 0.01 survives 16 and dies at 32; 0.005 survives 32. GRPC_PYTHON_CENSUS_MAX_EXPORT_BUFFER_SIZE does not help, since the discard path takes the mutex first.

Synchronous servers seem unaffected. The same plugin, the same container, a grpc.server(ThreadPoolExecutor(...)) and the same health service: 8, 32 and 64 client threads for 60s each gave 134k, 132k and 138k calls with no stall. That fits the theory — the cycle needs a thread that holds the GIL while blocking on the mutex, which is what the single aio event-loop thread does.

Possibly related: #38500 (hang on exit unless deregister_global() is called) reports a hang in native code from the same plugin, though on shutdown rather than under load.

Workaround we shipped: dropped the plugin and now record grpc.server.call.{started,duration,sent_total_compressed_message_size,rcvd_total_compressed_message_size} from a plain grpc.aio.ServerInterceptor. With the plugin gone the same service handles 30000 calls at 64 concurrent in 12s with no stall.

repro.py