Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions Lib/test/test_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import sysconfig
import tempfile
import textwrap
import threading
import time
import unittest
import warnings

Expand All @@ -22,6 +24,7 @@
from test.support import script_helper
from test.support import socket_helper
from test.support import warnings_helper
from test.support.os_helper import EnvironmentVarGuard

TESTFN = os_helper.TESTFN

Expand Down Expand Up @@ -794,6 +797,27 @@ def test_linked_to_musl(self):
for v in linked:
self.assertIsInstance(v, int)

def test_threadsafe_environmentvarguard(self):
Copy link
Member

@picnixz picnixz Apr 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test should be executed more than once. I think, so I would suggest doing something like that (when I executed the script in my terminal, I needed more than one script execution to see the issue)

def test_threadsafe_environmentvarguard(self):
    for _ in range(100):
        self._test_threadsafe_environmentvarguard()

def _test_threadsafe_environmentvarguard(self):
    ...

def worker1(guard):
for i in range(1000):
guard['MY_VAR'] = 'value1'
time.sleep(0.0001) # Small delay to increase chance of thread switching

def worker2(guard):
for i in range(1000):
guard['MY_VAR'] = 'value2'
time.sleep(0.0001)

guard = EnvironmentVarGuard()
t1 = threading.Thread(target=worker1, args=(guard,))
t2 = threading.Thread(target=worker2, args=(guard,))
t1.start()
t2.start()
t1.join()
t2.join()
final_value = os.getenv('MY_VAR')
self.assertEqual(final_value, "value2")


# XXX -follows a list of untested API
# make_legacy_pyc
Expand Down
Loading