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

Commit 032cf84

Browse files
authored
Remove a reference cycle in background process (#16314)
1 parent 7afb5e0 commit 032cf84

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

changelog.d/16314.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Remove a reference cycle for in background processes.

synapse/metrics/background_process_metrics.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,13 +322,21 @@ def __init__(self, name: str, instance_id: Optional[Union[int, str]] = None):
322322
if instance_id is None:
323323
instance_id = id(self)
324324
super().__init__("%s-%s" % (name, instance_id))
325-
self._proc = _BackgroundProcess(name, self)
325+
self._proc: Optional[_BackgroundProcess] = _BackgroundProcess(name, self)
326326

327327
def start(self, rusage: "Optional[resource.struct_rusage]") -> None:
328328
"""Log context has started running (again)."""
329329

330330
super().start(rusage)
331331

332+
if self._proc is None:
333+
logger.error(
334+
"Background process re-entered without a proc: %s",
335+
self.name,
336+
stack_info=True,
337+
)
338+
return
339+
332340
# We've become active again so we make sure we're in the list of active
333341
# procs. (Note that "start" here means we've become active, as opposed
334342
# to starting for the first time.)
@@ -345,10 +353,21 @@ def __exit__(
345353

346354
super().__exit__(type, value, traceback)
347355

356+
if self._proc is None:
357+
logger.error(
358+
"Background process exited without a proc: %s",
359+
self.name,
360+
stack_info=True,
361+
)
362+
return
363+
348364
# The background process has finished. We explicitly remove and manually
349365
# update the metrics here so that if nothing is scraping metrics the set
350366
# doesn't infinitely grow.
351367
with _bg_metrics_lock:
352368
_background_processes_active_since_last_scrape.discard(self._proc)
353369

354370
self._proc.update_metrics()
371+
372+
# Set proc to None to break the reference cycle.
373+
self._proc = None

0 commit comments

Comments
 (0)