From 10716e8ec148e75cc73303c98b8e2cb5daf8a137 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aleix=20Conchillo=20Flaqu=C3=A9?= Date: Wed, 29 Jan 2025 23:06:12 -0800 Subject: [PATCH] utils: protect obj_id() and obj_count() with a lock --- src/pipecat/utils/utils.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/pipecat/utils/utils.py b/src/pipecat/utils/utils.py index 4cdb73050..0f4801f35 100644 --- a/src/pipecat/utils/utils.py +++ b/src/pipecat/utils/utils.py @@ -6,9 +6,12 @@ import collections import itertools +import threading _COUNTS = collections.defaultdict(itertools.count) +_COUNTS_LOCK = threading.Lock() _ID = itertools.count() +_ID_LOCK = threading.Lock() def obj_id() -> int: @@ -21,7 +24,8 @@ def obj_id() -> int: >>> obj_id() 2 """ - return next(_ID) + with _ID_LOCK: + return next(_ID) def obj_count(obj) -> int: @@ -35,4 +39,5 @@ def obj_count(obj) -> int: >>> obj_count(new_type()) 0 """ - return next(_COUNTS[obj.__class__.__name__]) + with _COUNTS_LOCK: + return next(_COUNTS[obj.__class__.__name__])