|
72 | 72 | from synapse.events.presence_router import load_legacy_presence_router
|
73 | 73 | from synapse.handlers.auth import load_legacy_password_auth_providers
|
74 | 74 | from synapse.http.site import SynapseSite
|
75 |
| -from synapse.logging.context import PreserveLoggingContext |
| 75 | +from synapse.logging.context import LoggingContext, PreserveLoggingContext |
76 | 76 | from synapse.logging.opentracing import init_tracer
|
77 | 77 | from synapse.metrics import install_gc_manager, register_threadpool
|
78 | 78 | from synapse.metrics.background_process_metrics import run_as_background_process
|
@@ -183,25 +183,23 @@ def run() -> None:
|
183 | 183 | if gc_thresholds:
|
184 | 184 | gc.set_threshold(*gc_thresholds)
|
185 | 185 | install_gc_manager()
|
186 |
| - run_command() |
187 | 186 |
|
188 |
| - # make sure that we run the reactor with the sentinel log context, |
189 |
| - # otherwise other PreserveLoggingContext instances will get confused |
190 |
| - # and complain when they see the logcontext arbitrarily swapping |
191 |
| - # between the sentinel and `run` logcontexts. |
192 |
| - # |
193 |
| - # We also need to drop the logcontext before forking if we're daemonizing, |
194 |
| - # otherwise the cputime metrics get confused about the per-thread resource usage |
195 |
| - # appearing to go backwards. |
196 |
| - with PreserveLoggingContext(): |
197 |
| - if daemonize: |
198 |
| - assert pid_file is not None |
| 187 | + # Reset the logging context when we start the reactor (whenever we yield control |
| 188 | + # to the reactor, the `sentinel` logging context needs to be set so we don't |
| 189 | + # leak the current logging context and erroneously apply it to the next task the |
| 190 | + # reactor event loop picks up) |
| 191 | + with PreserveLoggingContext(): |
| 192 | + run_command() |
| 193 | + |
| 194 | + if daemonize: |
| 195 | + assert pid_file is not None |
| 196 | + |
| 197 | + if print_pidfile: |
| 198 | + print(pid_file) |
199 | 199 |
|
200 |
| - if print_pidfile: |
201 |
| - print(pid_file) |
| 200 | + daemonize_process(pid_file, logger) |
202 | 201 |
|
203 |
| - daemonize_process(pid_file, logger) |
204 |
| - run() |
| 202 | + run() |
205 | 203 |
|
206 | 204 |
|
207 | 205 | def quit_with_error(error_string: str) -> NoReturn:
|
@@ -601,10 +599,12 @@ def run_sighup(*args: Any, **kwargs: Any) -> None:
|
601 | 599 | hs.get_datastores().main.db_pool.start_profiling()
|
602 | 600 | hs.get_pusherpool().start()
|
603 | 601 |
|
| 602 | + def log_shutdown() -> None: |
| 603 | + with LoggingContext("log_shutdown"): |
| 604 | + logger.info("Shutting down...") |
| 605 | + |
604 | 606 | # Log when we start the shut down process.
|
605 |
| - hs.get_reactor().addSystemEventTrigger( |
606 |
| - "before", "shutdown", logger.info, "Shutting down..." |
607 |
| - ) |
| 607 | + hs.get_reactor().addSystemEventTrigger("before", "shutdown", log_shutdown) |
608 | 608 |
|
609 | 609 | setup_sentry(hs)
|
610 | 610 | setup_sdnotify(hs)
|
|
0 commit comments