Skip to content

Commit 2eee683

Browse files
vdusekclaude
andcommitted
fix: Move metadata counter updates after successful API call in RQ single client
In `mark_request_as_handled`, the `handled_request_count` and `pending_request_count` metadata counters were updated before the `_update_request` API call. If the API call failed, the counters would be left in an inconsistent state with no rollback. This moves the counter updates to the `else` block so they only execute after a successful API call. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ea1f52b commit 2eee683

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/apify/storage_clients/_apify/_request_queue_single_client.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,9 @@ async def mark_request_as_handled(self, request: Request) -> ProcessedRequest |
204204
if cached_request := self._requests_cache.get(request_id):
205205
cached_request.handled_at = request.handled_at
206206

207-
if request.handled_at is None:
207+
should_update_counters = request.handled_at is None
208+
if should_update_counters:
208209
request.handled_at = datetime.now(tz=timezone.utc)
209-
self.metadata.handled_request_count += 1
210-
self.metadata.pending_request_count -= 1
211210

212211
try:
213212
# Remember that we handled this request, to optimize local deduplication.
@@ -224,6 +223,10 @@ async def mark_request_as_handled(self, request: Request) -> ProcessedRequest |
224223
logger.exception(f'Error marking request {request.unique_key} as handled.')
225224
return None
226225
else:
226+
# Update counters only after a successful API call to avoid inconsistency on failure.
227+
if should_update_counters:
228+
self.metadata.handled_request_count += 1
229+
self.metadata.pending_request_count -= 1
227230
return processed_request
228231

229232
async def reclaim_request(

0 commit comments

Comments
 (0)