Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 382b4e8

Browse files
authored
Defer SIGHUP handlers to reactor. (#8817)
We can get a SIGHUP at any point, including times where we are not in a sane state. By deferring calling the handlers until the next reactor tick we ensure that we don't get unexpected conflicts, e.g. trying to flush logs from the signal handler while the code was in the process of writing a log entry. Fixes #8769.
1 parent 7c43447 commit 382b4e8

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

changelog.d/8817.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix bug where logging could break after a call to SIGHUP.

synapse/app/_base.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from synapse.config.server import ListenerConfig
3333
from synapse.crypto import context_factory
3434
from synapse.logging.context import PreserveLoggingContext
35+
from synapse.metrics.background_process_metrics import wrap_as_background_process
3536
from synapse.util.async_helpers import Linearizer
3637
from synapse.util.daemonize import daemonize_process
3738
from synapse.util.rlimit import change_resource_limit
@@ -244,6 +245,7 @@ def start(hs: "synapse.server.HomeServer", listeners: Iterable[ListenerConfig]):
244245
# Set up the SIGHUP machinery.
245246
if hasattr(signal, "SIGHUP"):
246247

248+
@wrap_as_background_process("sighup")
247249
def handle_sighup(*args, **kwargs):
248250
# Tell systemd our state, if we're using it. This will silently fail if
249251
# we're not using systemd.
@@ -254,7 +256,13 @@ def handle_sighup(*args, **kwargs):
254256

255257
sdnotify(b"READY=1")
256258

257-
signal.signal(signal.SIGHUP, handle_sighup)
259+
# We defer running the sighup handlers until next reactor tick. This
260+
# is so that we're in a sane state, e.g. flushing the logs may fail
261+
# if the sighup happens in the middle of writing a log entry.
262+
def run_sighup(*args, **kwargs):
263+
hs.get_clock().call_later(0, handle_sighup, *args, **kwargs)
264+
265+
signal.signal(signal.SIGHUP, run_sighup)
258266

259267
register_sighup(refresh_certificate, hs)
260268

0 commit comments

Comments
 (0)