Skip to content

Commit

Permalink
[event_log] Stop leaking semaphore resources
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Gavin Mak <[email protected]>
Tested-by: Josip Sokcevic <[email protected]>
  • Loading branch information
sokcevicG authored and LUCI committed Oct 24, 2024
1 parent 70a4e64 commit ae384f8
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions event_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,10 @@ def Write(self, filename):
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():
Expand All @@ -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
Expand Down

0 comments on commit ae384f8

Please sign in to comment.