From ae384f8623aeb36809541a5e07900a77a0960d5f Mon Sep 17 00:00:00 2001 From: Josip Sokcevic Date: Wed, 23 Oct 2024 23:15:12 +0000 Subject: [PATCH] [event_log] Stop leaking semaphore resources With the global state and fork, we are left with uncleaned resources. Isolate mulitprocessing.Value in a function so we stop the leak. Bug: 353656374 Change-Id: If50bb544bda12b72f00c02bc1d2c0d19de000b88 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/440261 Commit-Queue: Josip Sokcevic Reviewed-by: Gavin Mak Tested-by: Josip Sokcevic --- event_log.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/event_log.py b/event_log.py index ef01394a..9ef52247 100644 --- a/event_log.py +++ b/event_log.py @@ -168,8 +168,10 @@ class EventLog: f.write("\n") -# An integer id that is unique across this invocation of the program. -_EVENT_ID = multiprocessing.Value("i", 1) +# An integer id that is unique across this invocation of the program, to be set +# by the first Add event. We can't set it here since it results in leaked +# resources (see: https://issues.gerritcodereview.com/353656374). +_EVENT_ID = None def _NextEventId(): @@ -178,6 +180,12 @@ def _NextEventId(): Returns: A unique, to this invocation of the program, integer id. """ + global _EVENT_ID + if _EVENT_ID is None: + # There is a small chance of race condition - two parallel processes + # setting up _EVENT_ID. However, we expect TASK_COMMAND to happen before + # mp kicks in. + _EVENT_ID = multiprocessing.Value("i", 1) with _EVENT_ID.get_lock(): val = _EVENT_ID.value _EVENT_ID.value += 1