Repository navigation
Serialize Mergify queue requests and reconcile transient failures #1951
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -401,7 +401,11 @@ def markers(repo: str, number: int, head: str, queue: str) -> tuple[bool, bool]: | |
|
|
||
| def request(repo: str, number: int, head: str, queue: str) -> None: | ||
| body = f"@mergifyio queue\n\n{_marker(REQUEST, head, queue)}" # Mergify deprecates naming the queue | ||
| github._api(f"repos/{repo}/issues/{number}/comments", {"body": body}) | ||
| try: | ||
| github._api(f"repos/{repo}/issues/{number}/comments", {"body": body}) | ||
| except (github.GhError, github.ProcTimeout, OSError) as exc: | ||
| if getattr(exc, "confirmed_rate_limit", False) or not markers(repo, number, head, queue)[0]: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: A failed reconciliation read hides the original POST error If the POST fails without a confirmed rate limit (a 5xx, ProcTimeout or OSError) and the follow-up |
||
| raise | ||
|
|
||
|
|
||
| def mark_confirmed(repo: str, number: int, head: str, queue: str) -> None: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3919,6 +3919,51 @@ def test_mergify_merge_requests_once_for_behind_head( | |
| else: | ||
| assert "queue configuration is unverified" in err | ||
|
|
||
| @pytest.mark.parametrize(("accepted", "transient"), [(True, False), (False, False), (True, True)]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: No test covers releasing the lock before queue polling The new concurrency test only runs |
||
| def test_overlapping_mergify_command_after_lost_reply(wt, gh, monkeypatch, accepted, transient): | ||
| head = arm_mergify_pr(wt, gh) | ||
| pr = github.PR(7, URL, head, "main", False, "OPEN", "MERGEABLE") | ||
| settings = config.load_trusted(wt) | ||
| monkeypatch.setattr(cli, "_context", lambda *_args, **_kwargs: (wt, settings)) | ||
| monkeypatch.setattr(cli, "_pr_readiness", lambda *_args, **_kwargs: (pr, github.Readiness(True, ()))) | ||
| monkeypatch.setattr(mergify, "membership", lambda *_: None) | ||
| monkeypatch.setattr(github, "merged_sha", lambda *_args, **_kwargs: None) | ||
| endpoint = f"repos/{REPO}/issues/7/comments" | ||
| gh.respond(f"api {endpoint}", []) | ||
| gh.respond(f"api {endpoint} --method POST", {}) | ||
| writing, overlap = Event(), Event() | ||
| original_api, flock = github._api, cli.fcntl.flock | ||
|
|
||
| def lock(fd, operation): | ||
| if writing.is_set(): | ||
| overlap.set() | ||
| return flock(fd, operation) | ||
|
|
||
| def transport(path, payload, **kwargs): | ||
| if path != endpoint: | ||
| return original_api(path, payload, **kwargs) | ||
| if not writing.is_set(): | ||
| writing.set() | ||
| assert overlap.wait(10), "second merge did not reach admission" | ||
| if accepted: | ||
| original_api(path, payload, **kwargs) | ||
| gh.respond(f"api {path}", [{"user": {"login": LOGIN}, "body": payload["body"]}]) | ||
| if transient: | ||
| raise github.GhUnavailable(("GitHub API request",), "HTTP 502") | ||
| raise github.ProcTimeout("lost Mergify command reply") | ||
| return original_api(path, payload, **kwargs) | ||
|
|
||
| monkeypatch.setattr(cli.fcntl, "flock", lock) | ||
| monkeypatch.setattr(github, "_api", transport) | ||
| with ThreadPoolExecutor(max_workers=2) as pool: | ||
| first = pool.submit(cli.main, ["merge"]) | ||
| assert writing.wait(10), "first merge did not reach the command POST" | ||
| second = pool.submit(cli.main, ["merge"]) | ||
| assert first.result(timeout=15) == (0 if accepted else 1) | ||
| assert second.result(timeout=15) == 0 | ||
| assert len([c for c in gh.calls if "--input" in c and c["argv"][1] == endpoint]) == 1 | ||
|
|
||
|
|
||
|
|
||
| @pytest.mark.parametrize("command", ["ready", "status"]) | ||
| @pytest.mark.parametrize("merge_state", ["CLEAN", "BEHIND"]) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: The admission lock covers slow work, waits a fixed 60s and ignores admit_by
lock_local_statewaits a fixed 60 seconds and then raises TimeoutError('task state is busy'). Merge now holds this lock for the whole admission phase: GitHub reads,_mergify_departure, and_prequeue_size_budget. The size budget clones, fetches and runs up to two scripts, each with GIT_TIMEOUT=60s.cmd_checkalso holds the same lock for an entire check run. As a result, a secondmerge --waitcan fail with 'busy' instead of following the queue the first invocation already joined. A merge started during a check also fails after 60s, where it previously went ahead. The 60s wait also ignoresadmit_by, so a positive--waitcan run past its admission budget. This stays safe (no duplicate POST), but users have to retry. Consider one of these: run_prequeue_size_budgetbefore taking the lock, then recheck markers under the lock; or make the lock wait respectadmit_by/deadlinewhen one is set.