Skip to content

fix(core): close Reaper socket at interpreter exit to prevent ResourceWarning#1017

Merged
alexanderankin merged 1 commit into
testcontainers:mainfrom
edgarrmondragon:fix/reaper-socket-resource-warning
May 22, 2026
Merged

fix(core): close Reaper socket at interpreter exit to prevent ResourceWarning#1017
alexanderankin merged 1 commit into
testcontainers:mainfrom
edgarrmondragon:fix/reaper-socket-resource-warning

Conversation

@edgarrmondragon
Copy link
Copy Markdown
Contributor

@edgarrmondragon edgarrmondragon commented May 21, 2026

The Ryuk keep-alive socket stored in Reaper._socket was never explicitly
closed, so CPython's GC finalized it during interpreter shutdown and emitted
a ResourceWarning ("unclosed <socket.socket ...>") that is impossible to
suppress via warnings filters because it fires after pytest exits.

================================================ slowest 10 durations =================================================
12.25s setup    tests/test_integration.py::TestIntegration::test_ensure_tables_creates_schema_if_not_exists
0.26s call     tests/test_integration.py::TestIntegration::test_ensure_tables_creates_schema_if_not_exists
0.25s teardown tests/test_integration.py::TestIntegration::test_ensure_tables_creates_schema_if_not_exists
0.21s call     tests/test_state.py::test_acquire_lock_max_retries_exceeded

(6 durations < 0.005s hidden.  Use -vv to show these durations.)
=========================================== 23 passed, 1 warning in 13.04s ============================================
Exception ignored in: <socket.socket fd=22, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 55812)>
ResourceWarning: unclosed <socket.socket fd=22, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 55812)>

Registering Reaper.delete_instance with atexit ensures the socket is closed
cleanly before the GC sweep, eliminating the warning without changing the
socket's lifetime during the test session.

@codecov
Copy link
Copy Markdown

codecov Bot commented May 21, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.01%. Comparing base (6deaf3b) to head (33fae08).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1017      +/-   ##
==========================================
+ Coverage   82.82%   83.01%   +0.19%     
==========================================
  Files          16       16              
  Lines        1723     1725       +2     
  Branches      189      189              
==========================================
+ Hits         1427     1432       +5     
+ Misses        240      236       -4     
- Partials       56       57       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

…eWarning

The Ryuk keep-alive socket stored in `Reaper._socket` was never explicitly
closed, so CPython's GC finalized it during interpreter shutdown and emitted
a ResourceWarning ("unclosed <socket.socket ...>") that is impossible to
suppress via warnings filters because it fires after pytest exits.

Registering `Reaper.delete_instance` with `atexit` ensures the socket is closed
cleanly before the GC sweep, eliminating the warning without changing the
socket's lifetime during the test session.

Signed-off-by: Edgar Ramírez Mondragón <edgarrm358@gmail.com>
@edgarrmondragon edgarrmondragon force-pushed the fix/reaper-socket-resource-warning branch from 4d391b2 to 33fae08 Compare May 21, 2026 23:24
@alexanderankin alexanderankin merged commit d3a1517 into testcontainers:main May 22, 2026
8 checks passed
@edgarrmondragon edgarrmondragon deleted the fix/reaper-socket-resource-warning branch May 22, 2026 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants