Skip to content

Commit 04e9afc

Browse files
febus982claude
andcommitted
Add test for SessionHandler.__del__ exception handling
Verifies that exceptions from scoped_session.remove() are caught and logged during cleanup, preventing errors during garbage collection. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 662add1 commit 04e9afc

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

tests/session_handler/test_session_lifecycle.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ def test_sync_session_is_removed_on_cleanup(sa_manager):
3232
mocked_remove.assert_called_once()
3333

3434

35+
def test_sync_session_cleanup_handles_exception(sa_manager):
36+
"""Test that __del__ gracefully handles exceptions from scoped_session.remove()."""
37+
sh = SessionHandler(sa_manager.get_bind("sync"))
38+
39+
with patch.object(
40+
sh.scoped_session,
41+
"remove",
42+
side_effect=Exception("Connection already closed"),
43+
) as mocked_remove:
44+
# This should not raise - the exception should be caught and logged
45+
sh.__del__()
46+
47+
mocked_remove.assert_called_once()
48+
49+
3550
@pytest.mark.parametrize("read_only_flag", [True, False])
3651
async def test_commit_is_called_only_if_not_read_only(
3752
read_only_flag,

0 commit comments

Comments
 (0)