2024-09-24 15:07:47 +03:00
|
|
|
import argparse
|
2021-02-17 16:23:32 +03:00
|
|
|
import faulthandler
|
Use Fork-Server As Spawn Method (#18682)
* Set runtime
* Use count correctly
* Don't assume camera sizes
* Use separate zmq proxy for object detection
* Correct order
* Use forkserver
* Only store PID instead of entire process reference
* Cleanup
* Catch correct errors
* Fix typing
* Remove before_run from process util
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.
* Cleanup
* Logging bugfix (#18465)
* use mp Manager to handle logging queues
A Python bug (https://github.com/python/cpython/issues/91555) was preventing logs from the embeddings maintainer process from printing. The bug is fixed in Python 3.14, but a viable workaround is to use the multiprocessing Manager, which better manages mp queues and causes the logging to work correctly.
* consolidate
* fix typing
* Fix typing
* Use global log queue
* Move to using process for logging
* Convert camera tracking to process
* Add more processes
* Finalize process
* Cleanup
* Cleanup typing
* Formatting
* Remove daemon
---------
Co-authored-by: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com>
2025-06-12 21:12:34 +03:00
|
|
|
import multiprocessing as mp
|
2024-09-24 15:07:47 +03:00
|
|
|
import signal
|
|
|
|
|
import sys
|
2023-05-29 13:31:17 +03:00
|
|
|
import threading
|
2024-12-18 23:10:32 +03:00
|
|
|
from typing import Union
|
2023-05-29 13:31:17 +03:00
|
|
|
|
2024-12-18 23:10:32 +03:00
|
|
|
import ruamel.yaml
|
2024-09-24 15:07:47 +03:00
|
|
|
from pydantic import ValidationError
|
2021-02-17 16:23:32 +03:00
|
|
|
|
2024-06-23 16:13:02 +03:00
|
|
|
from frigate.app import FrigateApp
|
2024-09-24 15:07:47 +03:00
|
|
|
from frigate.config import FrigateConfig
|
2024-09-27 15:53:23 +03:00
|
|
|
from frigate.log import setup_logging
|
2024-12-18 23:10:32 +03:00
|
|
|
from frigate.util.config import find_config_file
|
2023-05-29 13:31:17 +03:00
|
|
|
|
2020-11-04 15:31:25 +03:00
|
|
|
|
2024-09-17 16:26:25 +03:00
|
|
|
def main() -> None:
|
Use Fork-Server As Spawn Method (#18682)
* Set runtime
* Use count correctly
* Don't assume camera sizes
* Use separate zmq proxy for object detection
* Correct order
* Use forkserver
* Only store PID instead of entire process reference
* Cleanup
* Catch correct errors
* Fix typing
* Remove before_run from process util
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.
* Cleanup
* Logging bugfix (#18465)
* use mp Manager to handle logging queues
A Python bug (https://github.com/python/cpython/issues/91555) was preventing logs from the embeddings maintainer process from printing. The bug is fixed in Python 3.14, but a viable workaround is to use the multiprocessing Manager, which better manages mp queues and causes the logging to work correctly.
* consolidate
* fix typing
* Fix typing
* Use global log queue
* Move to using process for logging
* Convert camera tracking to process
* Add more processes
* Finalize process
* Cleanup
* Cleanup typing
* Formatting
* Remove daemon
---------
Co-authored-by: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com>
2025-06-12 21:12:34 +03:00
|
|
|
manager = mp.Manager()
|
2024-09-17 16:26:25 +03:00
|
|
|
faulthandler.enable()
|
2020-11-01 17:06:15 +03:00
|
|
|
|
2024-09-27 15:53:23 +03:00
|
|
|
# Setup the logging thread
|
Use Fork-Server As Spawn Method (#18682)
* Set runtime
* Use count correctly
* Don't assume camera sizes
* Use separate zmq proxy for object detection
* Correct order
* Use forkserver
* Only store PID instead of entire process reference
* Cleanup
* Catch correct errors
* Fix typing
* Remove before_run from process util
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.
* Cleanup
* Logging bugfix (#18465)
* use mp Manager to handle logging queues
A Python bug (https://github.com/python/cpython/issues/91555) was preventing logs from the embeddings maintainer process from printing. The bug is fixed in Python 3.14, but a viable workaround is to use the multiprocessing Manager, which better manages mp queues and causes the logging to work correctly.
* consolidate
* fix typing
* Fix typing
* Use global log queue
* Move to using process for logging
* Convert camera tracking to process
* Add more processes
* Finalize process
* Cleanup
* Cleanup typing
* Formatting
* Remove daemon
---------
Co-authored-by: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com>
2025-06-12 21:12:34 +03:00
|
|
|
setup_logging(manager)
|
2020-11-04 06:26:39 +03:00
|
|
|
|
2024-09-17 16:26:25 +03:00
|
|
|
threading.current_thread().name = "frigate"
|
2025-06-24 20:41:11 +03:00
|
|
|
stop_event = mp.Event()
|
|
|
|
|
|
|
|
|
|
# send stop event on SIGINT
|
|
|
|
|
signal.signal(signal.SIGINT, lambda sig, frame: stop_event.set())
|
2024-09-17 16:26:25 +03:00
|
|
|
|
2024-09-24 15:07:47 +03:00
|
|
|
# Make sure we exit cleanly on SIGTERM.
|
|
|
|
|
signal.signal(signal.SIGTERM, lambda sig, frame: sys.exit())
|
|
|
|
|
|
|
|
|
|
# Parse the cli arguments.
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
|
prog="Frigate",
|
|
|
|
|
description="An NVR with realtime local object detection for IP cameras.",
|
|
|
|
|
)
|
|
|
|
|
parser.add_argument("--validate-config", action="store_true")
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
# Load the configuration.
|
|
|
|
|
try:
|
2024-09-28 22:21:42 +03:00
|
|
|
config = FrigateConfig.load(install=True)
|
2024-09-24 15:07:47 +03:00
|
|
|
except ValidationError as e:
|
|
|
|
|
print("*************************************************************")
|
|
|
|
|
print("*************************************************************")
|
|
|
|
|
print("*** Your config file is not valid! ***")
|
|
|
|
|
print("*** Please check the docs at ***")
|
|
|
|
|
print("*** https://docs.frigate.video/configuration/ ***")
|
|
|
|
|
print("*************************************************************")
|
|
|
|
|
print("*************************************************************")
|
|
|
|
|
print("*** Config Validation Errors ***")
|
2024-12-18 23:10:32 +03:00
|
|
|
print("*************************************************************\n")
|
|
|
|
|
# Attempt to get the original config file for line number tracking
|
|
|
|
|
config_path = find_config_file()
|
|
|
|
|
with open(config_path, "r") as f:
|
|
|
|
|
yaml_config = ruamel.yaml.YAML()
|
|
|
|
|
yaml_config.preserve_quotes = True
|
|
|
|
|
full_config = yaml_config.load(f)
|
|
|
|
|
|
2024-09-24 15:07:47 +03:00
|
|
|
for error in e.errors():
|
2024-12-18 23:10:32 +03:00
|
|
|
error_path = error["loc"]
|
|
|
|
|
|
|
|
|
|
current = full_config
|
|
|
|
|
line_number = "Unknown"
|
|
|
|
|
last_line_number = "Unknown"
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
for i, part in enumerate(error_path):
|
|
|
|
|
key: Union[int, str] = (
|
|
|
|
|
int(part) if isinstance(part, str) and part.isdigit() else part
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if isinstance(current, ruamel.yaml.comments.CommentedMap):
|
|
|
|
|
current = current[key]
|
|
|
|
|
elif isinstance(current, list):
|
|
|
|
|
if isinstance(key, int):
|
|
|
|
|
current = current[key]
|
|
|
|
|
|
|
|
|
|
if hasattr(current, "lc"):
|
|
|
|
|
last_line_number = current.lc.line
|
|
|
|
|
|
|
|
|
|
if i == len(error_path) - 1:
|
|
|
|
|
if hasattr(current, "lc"):
|
|
|
|
|
line_number = current.lc.line
|
|
|
|
|
else:
|
|
|
|
|
line_number = last_line_number
|
|
|
|
|
|
|
|
|
|
except Exception as traverse_error:
|
|
|
|
|
print(f"Could not determine exact line number: {traverse_error}")
|
|
|
|
|
|
2025-01-08 19:25:09 +03:00
|
|
|
if current != full_config:
|
|
|
|
|
print(f"Line # : {line_number}")
|
|
|
|
|
print(f"Key : {' -> '.join(map(str, error_path))}")
|
2025-01-10 18:39:24 +03:00
|
|
|
print(f"Value : {error.get('input', '-')}")
|
2024-12-18 23:10:32 +03:00
|
|
|
print(f"Message : {error.get('msg', error.get('type', 'Unknown'))}\n")
|
|
|
|
|
|
2024-09-24 15:07:47 +03:00
|
|
|
print("*************************************************************")
|
|
|
|
|
print("*** End Config Validation Errors ***")
|
|
|
|
|
print("*************************************************************")
|
2025-05-24 19:47:15 +03:00
|
|
|
|
|
|
|
|
# attempt to start Frigate in recovery mode
|
|
|
|
|
try:
|
|
|
|
|
config = FrigateConfig.load(install=True, safe_load=True)
|
|
|
|
|
print("Starting Frigate in safe mode.")
|
|
|
|
|
except ValidationError:
|
|
|
|
|
print("Unable to start Frigate in safe mode.")
|
|
|
|
|
sys.exit(1)
|
2024-09-24 15:07:47 +03:00
|
|
|
if args.validate_config:
|
|
|
|
|
print("*************************************************************")
|
|
|
|
|
print("*** Your config file is valid. ***")
|
|
|
|
|
print("*************************************************************")
|
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
2024-09-17 16:26:25 +03:00
|
|
|
# Run the main application.
|
2025-06-24 20:41:11 +03:00
|
|
|
FrigateApp(config, manager, stop_event).start()
|
2020-11-01 18:56:33 +03:00
|
|
|
|
2024-09-17 16:26:25 +03:00
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2025-06-24 20:41:11 +03:00
|
|
|
mp.set_forkserver_preload(
|
|
|
|
|
[
|
|
|
|
|
# Standard library and core dependencies
|
|
|
|
|
"sqlite3",
|
|
|
|
|
# Third-party libraries commonly used in Frigate
|
|
|
|
|
"numpy",
|
|
|
|
|
"cv2",
|
|
|
|
|
"peewee",
|
|
|
|
|
"zmq",
|
|
|
|
|
"ruamel.yaml",
|
|
|
|
|
# Frigate core modules
|
|
|
|
|
"frigate.camera.maintainer",
|
|
|
|
|
]
|
|
|
|
|
)
|
Use Fork-Server As Spawn Method (#18682)
* Set runtime
* Use count correctly
* Don't assume camera sizes
* Use separate zmq proxy for object detection
* Correct order
* Use forkserver
* Only store PID instead of entire process reference
* Cleanup
* Catch correct errors
* Fix typing
* Remove before_run from process util
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.
* Cleanup
* Logging bugfix (#18465)
* use mp Manager to handle logging queues
A Python bug (https://github.com/python/cpython/issues/91555) was preventing logs from the embeddings maintainer process from printing. The bug is fixed in Python 3.14, but a viable workaround is to use the multiprocessing Manager, which better manages mp queues and causes the logging to work correctly.
* consolidate
* fix typing
* Fix typing
* Use global log queue
* Move to using process for logging
* Convert camera tracking to process
* Add more processes
* Finalize process
* Cleanup
* Cleanup typing
* Formatting
* Remove daemon
---------
Co-authored-by: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com>
2025-06-12 21:12:34 +03:00
|
|
|
mp.set_start_method("forkserver", force=True)
|
2024-09-17 16:26:25 +03:00
|
|
|
main()
|