Skip to content

Commit 7fdffd9

Browse files
sparrowtNoah Ulster
andauthored
fix: avoid unnecessary env updates to reduce chances of segfault (#956)
* fix: __terminal_color shouldn't modify os.environ unless it has to See #955 for a detailed explanation of why this can cause thread safety issues resulting in a segfault when another thread calls `getenv` * fix: pytest_assertrepr_compare should be a no-op for other types If neither operand is a `SnapshotAssertion` then the rest of the code is not going to do anything, so bail early rather than unnecessarily setting up `__terminal_color` etc. which can cause issues c.f. #955 * chore: run linter --------- Co-authored-by: Noah Ulster <noah.u@roserocket.com>
1 parent b7f5a9d commit 7fdffd9

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

src/syrupy/__init__.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import argparse
2+
import contextlib
23
import sys
34
from functools import lru_cache
45
from gettext import gettext
@@ -105,11 +106,14 @@ def pytest_addoption(parser: "pytest.Parser") -> None:
105106

106107

107108
def __terminal_color(config: "pytest.Config") -> "ContextManager[None]":
108-
env = {}
109109
if config.option.no_colors:
110-
env[DISABLE_COLOR_ENV_VAR] = "true"
111-
112-
return env_context(**env)
110+
env = {
111+
DISABLE_COLOR_ENV_VAR: "true",
112+
}
113+
return env_context(**env)
114+
else:
115+
# No-op to avoid unnecessary env updates
116+
return contextlib.nullcontext()
113117

114118

115119
def pytest_assertrepr_compare(
@@ -119,6 +123,12 @@ def pytest_assertrepr_compare(
119123
Return explanation for comparisons in failing assert expressions.
120124
https://docs.pytest.org/en/latest/reference.html#_pytest.hookspec.pytest_assertrepr_compare
121125
"""
126+
if not isinstance(left, SnapshotAssertion) and not isinstance(
127+
right, SnapshotAssertion
128+
):
129+
# Shortcut to minimise overhead in the case of other unrelated assertions
130+
return None
131+
122132
with __terminal_color(config):
123133
received_name = received_style("[+ received]")
124134

0 commit comments

Comments
 (0)