Skip to content

Commit 0f2a399

Browse files
committed
add lock to warning check in onetrace_enabled context manager
1 parent a0b4af1 commit 0f2a399

1 file changed

Lines changed: 20 additions & 14 deletions

File tree

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
@@ -62,21 +64,25 @@ def onetrace_enabled():
6264
"""
6365
global _UNCHECKED
6466

65-
if _UNCHECKED:
66-
_UNCHECKED = False
67-
if not (
68-
getenv("PTI_ENABLE", None) == "1"
69-
and "onetrace_tool" in getenv("LD_PRELOAD", "")
70-
):
71-
import warnings
72-
73-
warnings.warn(
74-
"It looks like Python interpreter was not started using "
75-
"`onetrace` utility. Using `onetrace_enabled` may have "
76-
"no effect. See `onetrace_enabled.__doc__` for usage.",
77-
RuntimeWarning,
78-
stacklevel=2,
67+
with _unchecked_lock:
68+
if _UNCHECKED:
69+
_UNCHECKED = False
70+
needs_warning = not (
71+
getenv("PTI_ENABLE", None) == "1"
72+
and "onetrace_tool" in getenv("LD_PRELOAD", "")
7973
)
74+
else:
75+
needs_warning = False
76+
if needs_warning:
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,
85+
)
8086

8187
_env_var_name = "PTI_ENABLE_COLLECTION"
8288
saved = getenv(_env_var_name, None)

0 commit comments

Comments
 (0)