Skip to content

Commit d343d0e

Browse files
authored
fix(memory-storage): Avoid duplicate processed requests in memory request queue client (#1941)
## Description - `MemoryRequestQueueClient.add_batch_of_requests` appended a `ProcessedRequest` twice for requests that were already present in the queue but not yet handled — once in the `was_already_present` branch and again at the unconditional append after the if/else block, which that branch did not skip. - This violated the contract that `AddRequestsResponse.processed_requests` contains exactly one entry per input request and inflated results for any caller iterating the list. - Removed the inner append so the shared append at the end handles both branches, matching the structure of the file system client. Added a regression test.
1 parent 9ce5a43 commit d343d0e

2 files changed

Lines changed: 17 additions & 8 deletions

File tree

‎src/crawlee/storage_clients/_memory/_request_queue_client.py‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,6 @@ async def add_batch_of_requests(
190190
# Add updated request back to queue.
191191
self._pending_requests.appendleft(request)
192192

193-
processed_requests.append(
194-
ProcessedRequest(
195-
unique_key=request.unique_key,
196-
was_already_present=True,
197-
was_already_handled=False,
198-
)
199-
)
200-
201193
# Add the new request to the queue.
202194
else:
203195
if forefront:

‎tests/unit/storage_clients/_memory/test_memory_rq_client.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ async def test_memory_specific_purge_behavior() -> None:
4343
assert await rq_client2.is_empty() is True
4444

4545

46+
async def test_add_existing_pending_request_returns_single_processed_request(
47+
rq_client: MemoryRequestQueueClient,
48+
) -> None:
49+
"""Test that re-adding a pending (not handled) request yields exactly one `ProcessedRequest` entry."""
50+
request = Request.from_url('https://example.com')
51+
await rq_client.add_batch_of_requests([request])
52+
53+
# Re-add the same request while it is still pending (not handled, not in progress).
54+
response = await rq_client.add_batch_of_requests([request])
55+
56+
assert len(response.processed_requests) == 1
57+
processed_request = response.processed_requests[0]
58+
assert processed_request.unique_key == request.unique_key
59+
assert processed_request.was_already_present is True
60+
assert processed_request.was_already_handled is False
61+
62+
4663
async def test_memory_metadata_updates(rq_client: MemoryRequestQueueClient) -> None:
4764
"""Test that metadata timestamps are updated correctly in memory storage."""
4865
# Record initial timestamps

0 commit comments

Comments
 (0)