Skip to content

Commit f77ec0a

Browse files
committed
add lock to warning check in onetrace_enabled context manager
1 parent 56a5ce7 commit f77ec0a

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

dpctl/utils/_onetrace_context.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
import threading
1718
from contextlib import contextmanager
1819
from os import environ, getenv
1920
from platform import system as sys_platform
@@ -25,6 +26,7 @@
2526

2627
_UNCHECKED = sys_platform() == "Linux"
2728
del sys_platform
29+
_unchecked_lock = threading.Lock()
2830

2931

3032
@contextmanager
@@ -68,21 +70,25 @@ def onetrace_enabled():
6870
"""
6971
global _UNCHECKED
7072

71-
if _UNCHECKED:
72-
_UNCHECKED = False
73-
if not (
74-
getenv("PTI_ENABLE", None) == "1"
75-
and "onetrace_tool" in getenv("LD_PRELOAD", "")
76-
):
77-
import warnings
78-
79-
warnings.warn(
80-
"It looks like Python interpreter was not started using "
81-
"`onetrace` utility. Using `onetrace_enabled` may have "
82-
"no effect. See `onetrace_enabled.__doc__` for usage.",
83-
RuntimeWarning,
84-
stacklevel=2,
73+
with _unchecked_lock:
74+
if _UNCHECKED:
75+
_UNCHECKED = False
76+
needs_warning = not (
77+
getenv("PTI_ENABLE", None) == "1"
78+
and "onetrace_tool" in getenv("LD_PRELOAD", "")
8579
)
80+
else:
81+
needs_warning = False
82+
if needs_warning:
83+
import warnings
84+
85+
warnings.warn(
86+
"It looks like Python interpreter was not started using "
87+
"`onetrace` utility. Using `onetrace_enabled` may have "
88+
"no effect. See `onetrace_enabled.__doc__` for usage.",
89+
RuntimeWarning,
90+
stacklevel=2,
91+
)
8692

8793
_env_var_name = "PTI_ENABLE_COLLECTION"
8894
saved = getenv(_env_var_name, None)

0 commit comments

Comments
 (0)