Skip to content

Commit 4d391b2

Browse files
fix(core): close Reaper socket at interpreter exit to prevent ResourceWarning
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>
1 parent 6deaf3b commit 4d391b2

1 file changed

Lines changed: 2 additions & 0 deletions

File tree

core/testcontainers/core/container.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pathlib
44
import sys
55
import tarfile
6+
from atexit import register as atexit_register
67
from os import PathLike
78
from socket import socket
89
from types import TracebackType
@@ -476,5 +477,6 @@ def _create_instance(cls) -> "Reaper":
476477
rs.send(f"label={LABEL_SESSION_ID}={SESSION_ID}\r\n".encode())
477478

478479
Reaper._instance = Reaper()
480+
atexit_register(Reaper.delete_instance)
479481

480482
return Reaper._instance

0 commit comments

Comments
 (0)