[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 <sokcevic@google.com>
Reviewed-by: Gavin Mak <gavinmak@google.com>
Tested-by: Josip Sokcevic <sokcevic@google.com>
This commit is contained in:
Josip Sokcevic 2024-10-23 23:15:12 +00:00 committed by LUCI
parent 70a4e643e6
commit ae384f8623

View File

@ -168,8 +168,10 @@ class EventLog:
f.write("\n") f.write("\n")
# An integer id that is unique across this invocation of the program. # An integer id that is unique across this invocation of the program, to be set
_EVENT_ID = multiprocessing.Value("i", 1) # 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(): def _NextEventId():
@ -178,6 +180,12 @@ def _NextEventId():
Returns: Returns:
A unique, to this invocation of the program, integer id. 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(): with _EVENT_ID.get_lock():
val = _EVENT_ID.value val = _EVENT_ID.value
_EVENT_ID.value += 1 _EVENT_ID.value += 1