fix: eagerly update local head on forefront ops in shared RequestQueue#887
Draft
fix: eagerly update local head on forefront ops in shared RequestQueue#887
Conversation
The shared `RequestQueue` client relied on `list_and_lock_head` to reflect forefront updates from `update_request(forefront=true)` and `batch_add_requests(forefront=true)`, but the API has propagation delay between those operations. The next fetch could return a different request, causing flaky test failures (#808). Mirror the single-mode client by eagerly inserting forefront request IDs at the front of the local `_queue_head` cache instead of relying on an API roundtrip.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #887 +/- ##
===========================================
- Coverage 86.94% 75.74% -11.21%
===========================================
Files 48 48
Lines 2942 2944 +2
===========================================
- Hits 2558 2230 -328
- Misses 384 714 +330
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The shared
RequestQueueclient relied onlist_and_lock_headto reflect forefront updates fromupdate_request(forefront=true)andbatch_add_requests(forefront=true). Because the Apify API has a propagation delay between those operations andlist_and_lock_head(#808), the next fetch could return a stale request — the failing CI fortest_request_reclaim_with_forefront[shared]returnedhttps://example.com/2instead of the just-reclaimed/1.This mirrors the single-mode client (
_request_queue_single_client.py:262) by eagerly updating the local_queue_headdeque on forefront operations:reclaim_request(forefront=True)—appendleftthe reclaimed request_id (withcontextlib.suppress(ValueError)dedupe). Removed the now-redundant_should_check_for_forefront_requests = Trueflag setting.add_batch_of_requests(forefront=True)—appendleftnewly-added (non-unprocessed) request IDs in reverse order so the first batched item ends up at the front. Also passedhydrated_request=requestto_cache_requestso subsequent fetches don't need an extra API roundtrip._should_check_for_forefront_requestsentirely (and its leftover-buffer logic in_list_head). With eager local updates the cache is authoritative for our own forefront ops; natural drain (len(_queue_head) ≤ 1) handles the refresh for picking up other clients' changes.The motivation for the change is to fix this flaky test in CI: https://github.com/apify/apify-sdk-python/actions/runs/25316848848/job/74216228877