Skip to content

test: run session teardown in bookmark file-input restore test (#2482) - #2490

Open
drewe7192 wants to merge 1 commit into
posit-dev:mainfrom
drewe7192:fix/bookmark-file-input-leak-2482
Open

test: run session teardown in bookmark file-input restore test (#2482)#2490
drewe7192 wants to merge 1 commit into
posit-dev:mainfrom
drewe7192:fix/bookmark-file-input-leak-2482

Conversation

@drewe7192

Copy link
Copy Markdown

Closes #2482

Description

In tests/pytest/test_test_mode.py, test_file_restore_handler_registers_snapshot_preprocess exercises the shiny.file bookmark restore handler. This handler instantiates a tempfile.TemporaryDirectory and registers cleanup via session.on_ended(lambda: tempdir_root.cleanup()).

Because the test previously exited without ending the session lifecycle, the cleanup callback never fired during the test execution. The directory was left to Python's cyclic garbage collector, intermittently emitting an unraisable ResourceWarning: Implicitly cleaning up <TemporaryDirectory ...> during test teardown.

Solution

  • Marked test_file_restore_handler_registers_snapshot_preprocess with @pytest.mark.asyncio and converted it to an async def.
  • Wrapped the assertion block in a try...finally block that calls await session._run_session_ended_tasks(), ensuring the registered on_ended cleanup hook executes deterministically at test completion.

Testing

  • Verified that pytest tests/pytest/test_test_mode.py -k test_file_restore_handler_registers_snapshot_preprocess passes cleanly without unhandled warnings.

In `test_file_restore_handler_registers_snapshot_preprocess`, the `shiny.file` bookmark restore handler creates a `tempfile.TemporaryDirectory` and registers its cleanup via `session.on_ended()`. Because the test never explicitly completed the session lifecycle, the cleanup callback never fired, leaving the directory to be reclaimed at GC time and emitting a `ResourceWarning`.

Convert the test to async and invoke `await session._run_session_ended_tasks()` in a `finally` block so the directory is cleaned up deterministically upon test completion.

Fixes posit-dev#2482
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.

Bookmark file-input restore leaks a TemporaryDirectory when a session never ends

1 participant