Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b07758f

Browse files
committed
Update _at_fork_reinit impl
1 parent a003bea commit b07758f

File tree

1 file changed

+17
-3
lines changed
  • opentelemetry-sdk/src/opentelemetry/sdk/trace/export

1 file changed

+17
-3
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
import sys
1818
import threading
1919
import typing
20+
import os
2021
from enum import Enum
21-
from os import environ, linesep, register_at_fork
22+
from os import environ, linesep
2223
from typing import Optional
2324

2425
from opentelemetry.context import (
@@ -197,7 +198,9 @@ def __init__(
197198
None
198199
] * self.max_export_batch_size # type: typing.List[typing.Optional[Span]]
199200
self.worker_thread.start()
200-
register_at_fork(after_in_child=self._at_fork_reinit)
201+
# Only available in *nix since py37.
202+
if hasattr(os, "register_at_fork"):
203+
os.register_at_fork(after_in_child=self._at_fork_reinit)
201204

202205
def on_start(
203206
self, span: Span, parent_context: typing.Optional[Context] = None
@@ -222,12 +225,23 @@ def on_end(self, span: ReadableSpan) -> None:
222225
self.condition.notify()
223226

224227
def _at_fork_reinit(self):
225-
self.condition._at_fork_reinit()
228+
# worker_thread is local to a process, only the thread that issued fork continues
229+
# to exist. A new worker thread must be started in child process.
226230
self.worker_thread = threading.Thread(
227231
name="OtelBatchSpanProcessor", target=self.worker, daemon=True
228232
)
229233
self.worker_thread.start()
230234

235+
# could be in an inconsistent state after fork, reinitialise by calling `_at_fork_reinit`
236+
# (creates a new lock internally https://github.com/python/cpython/blob/main/Python/thread_pthread.h#L727)
237+
# if exists, otherwise create a new one.
238+
if hasattr(self.condition, "_at_fork_reinit"):
239+
self.condition._at_fork_reinit()
240+
else:
241+
self.condition = threading.Condition(threading.Lock())
242+
243+
self.queue.clear()
244+
231245
def worker(self):
232246
timeout = self.schedule_delay_millis / 1e3
233247
flush_request = None # type: typing.Optional[_FlushRequest]

0 commit comments

Comments
 (0)