mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-02-15 07:35:27 +03:00
Reopen stdout/err on process fork
This helps avoid deadlocks (https://github.com/python/cpython/issues/91776).
This commit is contained in:
parent
f407776e9a
commit
0a11ed3781
@ -2,6 +2,7 @@ import atexit
|
||||
import logging
|
||||
import multiprocessing as mp
|
||||
import os
|
||||
import sys
|
||||
import threading
|
||||
from collections import deque
|
||||
from contextlib import AbstractContextManager, ContextDecorator
|
||||
@ -68,6 +69,19 @@ class log_thread(AbstractContextManager, ContextDecorator):
|
||||
self._stop_thread()
|
||||
|
||||
|
||||
# When a multiprocessing.Process exits, python tries to flush stdout and stderr. However, if the
|
||||
# process is created after a thread (for example a logging thread) is created and the process fork
|
||||
# happens while an internal lock is held, the stdout/err flush can cause a deadlock.
|
||||
#
|
||||
# https://github.com/python/cpython/issues/91776
|
||||
def reopen_std_streams():
|
||||
sys.stdout = os.fdopen(1, "w")
|
||||
sys.stderr = os.fdopen(2, "w")
|
||||
|
||||
|
||||
os.register_at_fork(after_in_child=reopen_std_streams)
|
||||
|
||||
|
||||
# based on https://codereview.stackexchange.com/a/17959
|
||||
class LogPipe(threading.Thread):
|
||||
def __init__(self, log_name: str):
|
||||
|
||||
Loading…
Reference in New Issue
Block a user