Remove a reference cycle in background process (#16314)

This commit is contained in:
Erik Johnston 2023-09-13 16:17:06 +01:00 committed by GitHub
parent 7afb5e0410
commit 032cf84f52
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

1
changelog.d/16314.misc Normal file
View File

@ -0,0 +1 @@
Remove a reference cycle for in background processes.

View File

@ -322,13 +322,21 @@ class BackgroundProcessLoggingContext(LoggingContext):
if instance_id is None: if instance_id is None:
instance_id = id(self) instance_id = id(self)
super().__init__("%s-%s" % (name, instance_id)) 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: def start(self, rusage: "Optional[resource.struct_rusage]") -> None:
"""Log context has started running (again).""" """Log context has started running (again)."""
super().start(rusage) 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 # 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 # procs. (Note that "start" here means we've become active, as opposed
# to starting for the first time.) # to starting for the first time.)
@ -345,6 +353,14 @@ class BackgroundProcessLoggingContext(LoggingContext):
super().__exit__(type, value, traceback) 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 # The background process has finished. We explicitly remove and manually
# update the metrics here so that if nothing is scraping metrics the set # update the metrics here so that if nothing is scraping metrics the set
# doesn't infinitely grow. # doesn't infinitely grow.
@ -352,3 +368,6 @@ class BackgroundProcessLoggingContext(LoggingContext):
_background_processes_active_since_last_scrape.discard(self._proc) _background_processes_active_since_last_scrape.discard(self._proc)
self._proc.update_metrics() self._proc.update_metrics()
# Set proc to None to break the reference cycle.
self._proc = None