Skip to content

fix(core): close the reaper socket at exit instead of removing the reaper - #1141

Open
pantafive wants to merge 1 commit into
testcontainers:mainfrom
pantafive:fix/reaper-atexit-close-socket
Open

pantafive wants to merge 1 commit into
testcontainers:mainfrom
pantafive:fix/reaper-atexit-close-socket

Conversation

@pantafive

Copy link
Copy Markdown

Fixes #1140

The atexit handler added in #1017 runs Reaper.delete_instance(), which force-removes the Ryuk container while the interpreter shuts down. Ryuk reaps a session only RYUK_RECONNECTION_TIMEOUT after its connection drops, so a process that exits normally leaves every container of its session running.

The handler now closes only the socket. That still prevents the ResourceWarning from #1017, and Ryuk reaps the session after the timeout. delete_instance() keeps its behaviour for explicit callers and reuses the new close_socket().

     @classmethod
-    def delete_instance(cls) -> None:
+    def close_socket(cls) -> None:
         if Reaper._socket is not None:
             Reaper._socket.close()
             Reaper._socket = None

+    @classmethod
+    def delete_instance(cls) -> None:
+        Reaper.close_socket()
+
 ...
-        atexit.register(Reaper.delete_instance)
+        # Only close the socket: Ryuk reaps the session after RYUK_RECONNECTION_TIMEOUT unless it is removed first.
+        atexit.register(Reaper.close_socket)

test_reaper_removes_session_containers_after_interpreter_exit starts a container in a child interpreter with RYUK_RECONNECTION_TIMEOUT=1s, lets it exit normally and waits up to 30s for the container to disappear. It carries the same skipif(is_mac()) and inside_docker_check marks as test_wait_for_reaper.

Testing, on Colima (Linux VM) with the macOS skip lifted locally:

  • before the fix: TimeoutError: Container 661e2b17… was not removed within 30s (status=exited)
  • after the fix: all of tests/core/test_ryuk.py passes
  • no ResourceWarning at exit under python -X dev
  • pre-commit run --files on the changed files passes, mypy included

…aper

The atexit handler added in testcontainers#1017 called Reaper.delete_instance(), which force-removes the Ryuk container. Ryuk then never reached its reconnection timeout, so the containers of a session that exits normally were never reaped. At exit only the socket is closed now; delete_instance() stays for explicit use.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Signed-off-by: Mikhail Solomenik <56078241+pantafive@users.noreply.github.com>

This branch has not been deployed

No deployments
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.

Bug: Reaper is force-removed at interpreter exit since 4.15.0, session containers are never reaped

1 participant