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

Remove a reference cycle in background process #16314

Merged
merged 3 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/16314.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove a reference cycle for in background processes.
21 changes: 20 additions & 1 deletion synapse/metrics/background_process_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,21 @@ def __init__(self, name: str, instance_id: Optional[Union[int, str]] = None):
if instance_id is None:
instance_id = id(self)
super().__init__("%s-%s" % (name, instance_id))
self._proc = _BackgroundProcess(name, self)
self._proc: Optional[_BackgroundProcess] = _BackgroundProcess(name, self)

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

super().start(rusage)

if self._proc is None:
logger.error(
"Background process re-entered without a proc: %s",
self.name,
stack_info=True,
)
return

# We've become active again so we make sure we're in the list of active
# procs. (Note that "start" here means we've become active, as opposed
# to starting for the first time.)
Expand All @@ -345,10 +353,21 @@ def __exit__(

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

if self._proc is None:
logger.error(
"Background process exited without a proc: %s",
self.name,
stack_info=True,
)
return

# The background process has finished. We explicitly remove and manually
# update the metrics here so that if nothing is scraping metrics the set
# doesn't infinitely grow.
with _bg_metrics_lock:
_background_processes_active_since_last_scrape.discard(self._proc)

self._proc.update_metrics()

# Set proc to None to break the reference cycle.
self._proc = None
Loading