Skip to content

Commit 72b47e7

Browse files
committed
[#1354] Close pytest rerun socket resources during xdist runs
Add local pytest cleanup for ResourceWarning messages emitted when running the test suite with pytest-xdist and pytest-rerunfailures. The rerunfailures plugin creates localhost sockets to coordinate rerun state between the xdist controller and workers, but version 16.1 does not close all of those sockets before Python interpreter shutdown. Update src/conftest.py to close the plugin-owned failures_db socket during pytest_unconfigure(). Also wrap pytest-rerunfailures ServerStatusDB connection handling so accepted server-side sockets are closed when their handler exits. This preserves rerunfailures behavior while preventing unclosed socket warnings during parallel pytest runs. Also keep reportconf.py loading inside a context manager so pytest-html report configuration does not leave an open file handle. Verified with: - pytest -q -n 2 src/architecture/messaging/_UnitTest/test_CMsgTimeWritten.py - pytest -q -n 2 src/simulation/environment/spiceInterface/_UnitTest/test_spiceThreadSafety.py - pytest -n auto from src
1 parent 714192b commit 72b47e7

1 file changed

Lines changed: 44 additions & 0 deletions

File tree

src/conftest.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,39 @@
3535
"The pytest option '--show_plots' has been deprecated for a year and will be removed shortly."
3636
)
3737

38+
39+
def _patch_rerunfailures_socket_cleanup():
40+
"""
41+
Close pytest-rerunfailures server-side sockets when xdist is active.
42+
43+
pytest-rerunfailures 16.1 opens localhost sockets to coordinate reruns
44+
between xdist workers, but accepted server connections are not closed by
45+
the plugin. This narrow patch preserves the plugin handler while ensuring
46+
each accepted connection is closed when the handler exits.
47+
"""
48+
try:
49+
import pytest_rerunfailures
50+
except ImportError:
51+
return
52+
53+
server_status_db = getattr(pytest_rerunfailures, "ServerStatusDB", None)
54+
if server_status_db is None:
55+
return
56+
if getattr(server_status_db, "_bsk_socket_cleanup_patched", False):
57+
return
58+
59+
original_run_connection = server_status_db.run_connection
60+
61+
def run_connection_with_socket_close(self, conn):
62+
with conn:
63+
return original_run_connection(self, conn)
64+
65+
server_status_db.run_connection = run_connection_with_socket_close
66+
server_status_db._bsk_socket_cleanup_patched = True
67+
68+
69+
_patch_rerunfailures_socket_cleanup()
70+
3871
filename = inspect.getframeinfo(inspect.currentframe()).filename
3972
path = os.path.dirname(os.path.abspath(filename))
4073
print(path)
@@ -71,6 +104,17 @@ def pytest_terminal_summary(terminalreporter, exitstatus, config):
71104
terminalreporter.write_line(message, **{terminal_color: True}, bold=True)
72105

73106

107+
def pytest_unconfigure(config):
108+
failures_db = getattr(config, "failures_db", None)
109+
socket_handle = getattr(failures_db, "sock", None)
110+
if socket_handle is None:
111+
return
112+
try:
113+
socket_handle.close()
114+
except OSError:
115+
pass
116+
117+
74118
@pytest.fixture(scope="module")
75119
def show_plots(request):
76120
return request.config.getoption("--show_plots")

0 commit comments

Comments
 (0)