Hypercorn/Unicorn Support

The OpenTelemetry SDK is not fork-safe, meaning you cannot use the same instance of the SDK in multiple processes. Specifically, the BatchSpanProcessor, PeriodicExportingMetricReader, and BatchLogProcessor must not be copied from a parent process to a child process. Instead, the parent process and child process must create their own instances. If instances of these processors/readers are copied from a parent to a child process, they will not function correctly.

These components spawn background threads to send data to the backend and maintain state protected by a lock, which is not fork-safe (see Python issue 6721). As a result, they face the same limitations as any code using locks or mutexes.

A common workaround is to create new instances of these processors/readers in each process using the register_at_fork hook from the os module. However, Unicorn/Hypercorn use the spawn method to start worker processes, which does not invoke the register_at_fork hook. Consequently, this workaround is not applicable to Unicorn/Hypercorn.

The recommended solution is to use gunicorn with the worker class uvicorn.workers.UvicornWorker since it supports the register_at_fork hook.

Was this page helpful?