From 6da022414f61280fe4058a9f65f6d9f5d20add75 Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Thu, 26 Mar 2026 17:11:23 +0100 Subject: [PATCH 1/2] fix: Isolate cleanup steps in Actor `__aexit__` to prevent cascading failures Co-Authored-By: Claude Opus 4.6 (1M context) --- src/apify/_actor.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/apify/_actor.py b/src/apify/_actor.py index 9fc7ff1a..81a25828 100644 --- a/src/apify/_actor.py +++ b/src/apify/_actor.py @@ -249,11 +249,20 @@ async def finalize() -> None: if self._event_listeners_timeout: await self.event_manager.wait_for_all_listeners_to_complete(timeout=self._event_listeners_timeout) - await self.event_manager.__aexit__(None, None, None) - await self._charging_manager_implementation.__aexit__(None, None, None) + for cleanup_step in [ + self.event_manager.__aexit__, + self._charging_manager_implementation.__aexit__, + ]: + try: + await cleanup_step(None, None, None) + except Exception: + self.log.exception(f'Cleanup step failed: {cleanup_step}') # Persist Actor state - await self._save_actor_state() + try: + await self._save_actor_state() + except Exception: + self.log.exception('Failed to save Actor state') try: await asyncio.wait_for(finalize(), self._cleanup_timeout.total_seconds()) From 34d383650c141610ceb8b743c0d8d0d764190c6d Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Thu, 26 Mar 2026 17:16:49 +0100 Subject: [PATCH 2/2] fix: Unroll cleanup loop to satisfy PERF203 lint rule Co-Authored-By: Claude Opus 4.6 (1M context) --- src/apify/_actor.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/apify/_actor.py b/src/apify/_actor.py index 81a25828..f4a3b0d2 100644 --- a/src/apify/_actor.py +++ b/src/apify/_actor.py @@ -249,14 +249,15 @@ async def finalize() -> None: if self._event_listeners_timeout: await self.event_manager.wait_for_all_listeners_to_complete(timeout=self._event_listeners_timeout) - for cleanup_step in [ - self.event_manager.__aexit__, - self._charging_manager_implementation.__aexit__, - ]: - try: - await cleanup_step(None, None, None) - except Exception: - self.log.exception(f'Cleanup step failed: {cleanup_step}') + try: + await self.event_manager.__aexit__(None, None, None) + except Exception: + self.log.exception('Failed to exit event manager') + + try: + await self._charging_manager_implementation.__aexit__(None, None, None) + except Exception: + self.log.exception('Failed to exit charging manager') # Persist Actor state try: