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

Commit cb8d568

Browse files
richvdhhawkowl
authored andcommitted
Fix 'utime went backwards' errors on daemonization. (#5609)
* Fix 'utime went backwards' errors on daemonization. Fixes #5608 * remove spurious debug
1 parent 463d5a8 commit cb8d568

3 files changed

Lines changed: 44 additions & 31 deletions

File tree

changelog.d/5609.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix 'utime went backwards' errors on daemonization.

synapse/app/_base.py

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -93,33 +93,36 @@ def start_reactor(
9393
install_dns_limiter(reactor)
9494

9595
def run():
96-
# make sure that we run the reactor with the sentinel log context,
97-
# otherwise other PreserveLoggingContext instances will get confused
98-
# and complain when they see the logcontext arbitrarily swapping
99-
# between the sentinel and `run` logcontexts.
100-
with PreserveLoggingContext():
101-
logger.info("Running")
102-
103-
change_resource_limit(soft_file_limit)
104-
if gc_thresholds:
105-
gc.set_threshold(*gc_thresholds)
106-
reactor.run()
107-
108-
if daemonize:
109-
if print_pidfile:
110-
print(pid_file)
111-
112-
daemon = Daemonize(
113-
app=appname,
114-
pid=pid_file,
115-
action=run,
116-
auto_close_fds=False,
117-
verbose=True,
118-
logger=logger,
119-
)
120-
daemon.start()
121-
else:
122-
run()
96+
logger.info("Running")
97+
change_resource_limit(soft_file_limit)
98+
if gc_thresholds:
99+
gc.set_threshold(*gc_thresholds)
100+
reactor.run()
101+
102+
# make sure that we run the reactor with the sentinel log context,
103+
# otherwise other PreserveLoggingContext instances will get confused
104+
# and complain when they see the logcontext arbitrarily swapping
105+
# between the sentinel and `run` logcontexts.
106+
#
107+
# We also need to drop the logcontext before forking if we're daemonizing,
108+
# otherwise the cputime metrics get confused about the per-thread resource usage
109+
# appearing to go backwards.
110+
with PreserveLoggingContext():
111+
if daemonize:
112+
if print_pidfile:
113+
print(pid_file)
114+
115+
daemon = Daemonize(
116+
app=appname,
117+
pid=pid_file,
118+
action=run,
119+
auto_close_fds=False,
120+
verbose=True,
121+
logger=logger,
122+
)
123+
daemon.start()
124+
else:
125+
run()
123126

124127

125128
def quit_with_error(error_string):

synapse/util/logcontext.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ def get_thread_resource_usage():
5252
return None
5353

5454

55+
# get an id for the current thread.
56+
#
57+
# threading.get_ident doesn't actually return an OS-level tid, and annoyingly,
58+
# on Linux it actually returns the same value either side of a fork() call. However
59+
# we only fork in one place, so it's not worth the hoop-jumping to get a real tid.
60+
#
61+
get_thread_id = threading.get_ident
62+
63+
5564
class ContextResourceUsage(object):
5665
"""Object for tracking the resources used by a log context
5766
@@ -225,7 +234,7 @@ def __init__(self, name=None, parent_context=None, request=None):
225234
# became active.
226235
self.usage_start = None
227236

228-
self.main_thread = threading.current_thread()
237+
self.main_thread = get_thread_id()
229238
self.request = None
230239
self.tag = ""
231240
self.alive = True
@@ -318,7 +327,7 @@ def copy_to(self, record):
318327
record.request = self.request
319328

320329
def start(self):
321-
if threading.current_thread() is not self.main_thread:
330+
if get_thread_id() != self.main_thread:
322331
logger.warning("Started logcontext %s on different thread", self)
323332
return
324333

@@ -328,7 +337,7 @@ def start(self):
328337
self.usage_start = get_thread_resource_usage()
329338

330339
def stop(self):
331-
if threading.current_thread() is not self.main_thread:
340+
if get_thread_id() != self.main_thread:
332341
logger.warning("Stopped logcontext %s on different thread", self)
333342
return
334343

@@ -355,7 +364,7 @@ def get_resource_usage(self):
355364

356365
# If we are on the correct thread and we're currently running then we
357366
# can include resource usage so far.
358-
is_main_thread = threading.current_thread() is self.main_thread
367+
is_main_thread = get_thread_id() == self.main_thread
359368
if self.alive and self.usage_start and is_main_thread:
360369
utime_delta, stime_delta = self._get_cputime()
361370
res.ru_utime += utime_delta

0 commit comments

Comments
 (0)