mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-05-01 11:07:41 +03:00
The before_run never actually ran because: You're right to suspect an issue with before_run not being called and a potential deadlock. The way you've implemented the run_wrapper using __getattribute__ for the run method of BaseProcess is a common pitfall in Python's multiprocessing, especially when combined with how multiprocessing.Process works internally. Here's a breakdown of why before_run isn't being called and why you might be experiencing a deadlock: The Problem: __getattribute__ and Process Serialization When you create a multiprocessing.Process object and call start(), the multiprocessing module needs to serialize the process object (or at least enough of it to re-create the process in the new interpreter). It then pickles this serialized object and sends it to the newly spawned process. The issue with your __getattribute__ implementation for run is that: run is retrieved during serialization: When multiprocessing tries to pickle your Process object to send to the new process, it will likely access the run attribute. This triggers your __getattribute__ wrapper, which then tries to bind run_wrapper to self. run_wrapper is bound to the parent process's self: The run_wrapper closure, when created in the parent process, captures the self (the Process instance) from the parent's memory space. Deserialization creates a new object: In the child process, a new Process object is created by deserializing the pickled data. However, the run_wrapper method that was pickled still holds a reference to the self from the parent process. This is a subtle but critical distinction. The child's run is not your wrapped run: When the child process starts, it internally calls its own run method. Because of the serialization and deserialization process, the run method that's ultimately executed in the child process is the original multiprocessing.Process.run or the Process.run if you had directly overridden it. Your __getattribute__ magic, which wraps run, isn't correctly applied to the Process object within the child's context.
56 lines
1.5 KiB
Python
56 lines
1.5 KiB
Python
import faulthandler
|
|
import logging
|
|
import multiprocessing as mp
|
|
import signal
|
|
import sys
|
|
import threading
|
|
from functools import wraps
|
|
from logging.handlers import QueueHandler
|
|
from typing import Any, Callable, Optional
|
|
|
|
import frigate.log
|
|
|
|
|
|
class BaseProcess(mp.Process):
|
|
def __init__(
|
|
self,
|
|
*,
|
|
name: Optional[str] = None,
|
|
target: Optional[Callable] = None,
|
|
args: tuple = (),
|
|
kwargs: dict = {},
|
|
daemon: Optional[bool] = None,
|
|
):
|
|
super().__init__(
|
|
name=name, target=target, args=args, kwargs=kwargs, daemon=daemon
|
|
)
|
|
|
|
def start(self, *args, **kwargs):
|
|
self.before_start()
|
|
super().start(*args, **kwargs)
|
|
self.after_start()
|
|
|
|
def before_start(self) -> None:
|
|
pass
|
|
|
|
def after_start(self) -> None:
|
|
pass
|
|
|
|
|
|
class Process(BaseProcess):
|
|
logger: logging.Logger
|
|
|
|
@property
|
|
def stop_event(self) -> threading.Event:
|
|
# Lazily create the stop_event. This allows the signal handler to tell if anyone is
|
|
# monitoring the stop event, and to raise a SystemExit if not.
|
|
if "stop_event" not in self.__dict__:
|
|
self.__dict__["stop_event"] = threading.Event()
|
|
return self.__dict__["stop_event"]
|
|
|
|
def before_start(self) -> None:
|
|
self.__log_queue = frigate.log.log_listener.queue
|
|
self.logger = logging.getLogger(self.name)
|
|
logging.basicConfig(handlers=[], force=True)
|
|
logging.getLogger().addHandler(QueueHandler(self.__log_queue))
|