Skip to content

Commit f98d672

Browse files
committed
Make ConcurrentMultiSpanProcessor fork safe
1 parent 102fec2 commit f98d672

File tree

1 file changed

+11
-2
lines changed
  • opentelemetry-sdk/src/opentelemetry/sdk/trace

1 file changed

+11
-2
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import concurrent.futures
1919
import json
2020
import logging
21+
import os
2122
import threading
2223
import traceback
2324
import typing
@@ -238,8 +239,16 @@ def __init__(self, num_threads: int = 2):
238239
# iterating through it on "on_start" and "on_end".
239240
self._span_processors = () # type: Tuple[SpanProcessor, ...]
240241
self._lock = threading.Lock()
241-
self._executor = concurrent.futures.ThreadPoolExecutor(
242-
max_workers=num_threads
242+
self._num_threads = num_threads
243+
self._executor = self._build_executor()
244+
if hasattr(os, "register_at_fork"):
245+
# Only the main thread is kept in forked processed, the executor
246+
# needs to be re-instantiated to get a fresh pool of threads:
247+
os.register_at_fork(after_in_child=self._build_executor)
248+
249+
def _build_executor(self) -> concurrent.futures.ThreadPoolExecutor:
250+
return concurrent.futures.ThreadPoolExecutor(
251+
max_workers=self._num_threads
243252
)
244253

245254
def add_span_processor(self, span_processor: SpanProcessor) -> None:

0 commit comments

Comments
 (0)