@@ -1626,6 +1626,59 @@ def endpoint(dep=Depends(sync_gen)):
16261626 assert result == {"dep" : "sync_value" }
16271627 assert cleanup_called
16281628
1629+ def test_sync_generator_cleanup_exception_swallowed (self ):
1630+ """Test that exception in generator's finally block is silently swallowed"""
1631+ cleanup_log = []
1632+
1633+ def failing_cleanup_gen ():
1634+ try :
1635+ yield "value"
1636+ finally :
1637+ cleanup_log .append ("cleanup_attempted" )
1638+ raise RuntimeError ("cleanup exploded" )
1639+
1640+ def healthy_gen ():
1641+ try :
1642+ yield "healthy"
1643+ finally :
1644+ cleanup_log .append ("healthy_cleanup" )
1645+
1646+ def endpoint (a = Depends (failing_cleanup_gen ), b = Depends (healthy_gen )):
1647+ return f"{ a } _{ b } "
1648+
1649+ result = self .resolver .resolve_dependencies (endpoint , self .request_data )
1650+ assert result == {"a" : "value" , "b" : "healthy" }
1651+ assert "cleanup_attempted" in cleanup_log
1652+ assert "healthy_cleanup" in cleanup_log
1653+
1654+ @pytest .mark .asyncio
1655+ async def test_async_generator_cleanup_exception_swallowed (self ):
1656+ """Test that async generator cleanup exception is silently swallowed"""
1657+ cleanup_log = []
1658+
1659+ async def failing_cleanup_gen ():
1660+ try :
1661+ yield "value"
1662+ finally :
1663+ cleanup_log .append ("cleanup_attempted" )
1664+ raise RuntimeError ("async cleanup exploded" )
1665+
1666+ async def healthy_gen ():
1667+ try :
1668+ yield "healthy"
1669+ finally :
1670+ cleanup_log .append ("healthy_cleanup" )
1671+
1672+ def endpoint (a = Depends (failing_cleanup_gen ), b = Depends (healthy_gen )):
1673+ return f"{ a } _{ b } "
1674+
1675+ result = await self .resolver .resolve_dependencies_async (
1676+ endpoint , self .request_data
1677+ )
1678+ assert result == {"a" : "value" , "b" : "healthy" }
1679+ assert "cleanup_attempted" in cleanup_log
1680+ assert "healthy_cleanup" in cleanup_log
1681+
16291682 @pytest .mark .asyncio
16301683 async def test_mixed_async_and_sync_generators (self ):
16311684 """Test mixing async and sync generators in async context"""
0 commit comments