Skip to content

Commit 3730be8

Browse files
committed
make PYTEST_CURRENT_TEST thread-safe
1 parent 184f5f1 commit 3730be8

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ include/
1616
*.class
1717
*.orig
1818
*~
19-
.hypothesis/
2019

2120
# autogenerated
2221
src/_pytest/_version.py
@@ -51,6 +50,7 @@ coverage.xml
5150
.vscode
5251
__pycache__/
5352
.python-version
53+
.claude
5454

5555
# generated by pip
5656
pip-wheel-metadata/

changelog/13837.improvement.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Pytest setting :ref:`PYTEST_CURRENT_TEST <pytest current test env>` internally is now thread-safe.

src/_pytest/runner.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,13 @@ def _update_current_test_var(
208208
value = value.replace("\x00", "(null)")
209209
os.environ[var_name] = value
210210
else:
211-
os.environ.pop(var_name)
211+
# under multithreading, this may have already been popped by another thread.
212+
# Note that os.environ inherits from MutableMapping and therefore .pop(var_name, None)
213+
# is not atomic or thread-safe, unlike e.g. popping from a builtin dict.
214+
try:
215+
os.environ.pop(var_name)
216+
except KeyError:
217+
pass
212218

213219

214220
def pytest_report_teststatus(report: BaseReport) -> tuple[str, str, str] | None:

0 commit comments

Comments
 (0)