Repository navigation
Keep merge queue polling inside the wait budget #1269
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
c54f092
cd4bf5f
8ae6e30
b586954
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 |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| import fcntl | ||
| import hashlib | ||
| import json | ||
| import math | ||
| import os | ||
| import shlex | ||
| import random | ||
|
|
@@ -1477,15 +1478,40 @@ def _wait_for_checks( | |
| return None | ||
|
|
||
|
|
||
| def _queue_wait( | ||
| deadline: float, poll: Callable[[float], str | None], *, | ||
| bounded: bool, timeout_notice: str = "", | ||
| ) -> str | None: | ||
| if not bounded: # Explicit --wait=0 still observes once. | ||
| return poll(0) | ||
| reported_timeout = False | ||
| try: | ||
| with github.bounded_reads(deadline, token=None, retry=True): | ||
| remaining = github.read_timeout(math.inf) | ||
| try: | ||
| sha = poll(remaining) | ||
| reported_timeout = sha is None | ||
| except (LoopZeroError, github.GhError, _proc.ProcTimeout): | ||
| github.read_timeout(0) | ||
| raise | ||
| except github.ReadDeadline: | ||
| if timeout_notice and not reported_timeout: | ||
| print(timeout_notice) | ||
| return None | ||
| return sha | ||
|
|
||
|
|
||
| def _landed(config: Config, pr: github.PR, closed: list[github.GhError]) -> str | None: | ||
| """Merge SHA if landed; a closed-unmerged read is held as not final (GitHub lags a | ||
| queue merge) and re-raised by the caller only if it persists to the deadline.""" | ||
| """Merge SHA if landed; a closed-unmerged read is held as not final.""" | ||
|
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: The old docstring explained that GitHub lags a queue merge, so a closed-unmerged read is held and only re-raised if it persists to the deadline. With |
||
| github.read_timeout(0) | ||
| try: | ||
| sha = github.merged_sha(config.repo, pr.number, expected_head=pr.head_sha) | ||
| except github.ClosedUnmerged as exc: | ||
| closed[:] = [exc] | ||
| return None | ||
| closed.clear() | ||
| sha = None | ||
| else: | ||
| closed.clear() | ||
| github.read_timeout(0) | ||
| return sha | ||
|
|
||
|
|
||
|
|
@@ -1494,30 +1520,52 @@ def _wait_for_merge(config: Config, pr: github.PR, timeout: float) -> str | None | |
| started = time.monotonic() | ||
| closed: list[github.GhError] = [] | ||
| print(f"PR #{pr.number} is queued for merge into {config.base_branch}; waiting") | ||
| for _ in _poll_until(started, timeout): | ||
| sha = _landed(config, pr, closed) | ||
| if sha: | ||
| return sha | ||
| if not github.merge_pending(config.repo, pr.number): | ||
| with suppress(github.ReadDeadline): | ||
| for _ in _poll_until(started, timeout, bounded=bool(timeout)): | ||
| sha = _landed(config, pr, closed) | ||
| if sha: | ||
| return sha | ||
| if closed: | ||
| continue | ||
| raise CliError(f"PR #{pr.number} left the merge queue unmerged") | ||
| if closed: | ||
| raise closed[0] | ||
| if not github.merge_pending(config.repo, pr.number): | ||
| sha = _landed(config, pr, closed) | ||
| if sha: | ||
| return sha | ||
| if closed: | ||
| continue | ||
| raise CliError(f"PR #{pr.number} left the merge queue unmerged") | ||
| _report_closed_observation(closed) | ||
| return None | ||
|
|
||
|
|
||
| def _report_closed_observation(closed: list[github.GhError]) -> None: | ||
| if closed: | ||
| try: | ||
| github.read_timeout(0) | ||
| except github.ReadDeadline: | ||
| print(f"last queue observation: {closed[0]}") | ||
| else: | ||
| raise closed[0] | ||
|
|
||
|
|
||
| def _merge_and_follow( | ||
| config: Config, pr: github.PR, deadline: float | None, admit_by: float | None, | ||
| ) -> str | None: | ||
| try: | ||
| sha = github.merge(config.repo, pr.number, config.merge_strategy, pr.head_sha, observation_deadline=admit_by) | ||
| except github.ReadDeadline: | ||
| return None | ||
|
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 deadline during direct-merge verification is reported as a merge-queue timeout
|
||
| if sha is None and deadline is not None: | ||
| return _queue_wait(deadline, partial(_wait_for_merge, config, pr), bounded=admit_by is not None) | ||
| return sha | ||
|
|
||
|
|
||
| def _mergify_timeout_detail( | ||
| config: Config, pr: github.PR, queue: str, state: mergify.Membership | None, | ||
| config: Config, pr: github.PR, queue: str, state: mergify.Membership | None, *, query_report: bool, | ||
| ) -> str: | ||
| if state is None: | ||
| try: | ||
| _, _, report = mergify.request_status(config.repo, pr.number, pr.head_sha, queue) | ||
| except github.GhError: | ||
| report = None | ||
| report = None | ||
| if query_report: | ||
| with suppress(github.GhError): | ||
| _, _, report = mergify.request_status(config.repo, pr.number, pr.head_sha, queue) | ||
| return f"waiting for admission to Mergify queue {queue}. " + ( | ||
| f"Mergify report: {report}" if report else "Check the PR for Mergify's admission report." | ||
| ) | ||
|
|
@@ -1587,32 +1635,28 @@ def _wait_for_mergify( | |
| queue = config.mergify_queue | ||
| assert queue is not None | ||
| state = None | ||
| deferred_landing_read = False | ||
| print(f"PR #{pr.number} following Mergify queue {queue}; head {pr.head_sha[:12]}; waiting up to {timeout:g}s") | ||
| for _ in _poll_until(started, timeout): | ||
| sha = _landed(config, pr, closed) | ||
| if sha: | ||
| return sha | ||
| if not mergify.configured(config.repo, config.base_branch, queue): | ||
| raise CliError("Mergify queue configuration became unsafe or unavailable while waiting") | ||
| state = mergify.membership(config.repo, pr.number, queue) | ||
| deferred_landing_read = confirmed and state is not None and not closed | ||
| if not deferred_landing_read and (sha := _landed(config, pr, closed)): | ||
| return sha | ||
| if state is not None and not confirmed: | ||
| mergify.mark_confirmed(config.repo, pr.number, pr.head_sha, queue) | ||
| confirmed = True | ||
| print(f"PR #{pr.number} confirmed in Mergify queue {queue}; waiting") | ||
| elif state is None: | ||
| if sha := _mergify_departure(config, pr, queue, closed, confirmed): | ||
| with suppress(github.ReadDeadline): | ||
| for _ in _poll_until(started, timeout, bounded=bool(timeout)): | ||
| sha = _landed(config, pr, closed) | ||
| if sha: | ||
| return sha | ||
| # A queued, open PR normally gets its next landing read on the next tick. | ||
| if deferred_landing_read and (sha := _landed(config, pr, closed)): | ||
| return sha | ||
| if closed: | ||
| raise closed[0] | ||
| if not mergify.configured(config.repo, config.base_branch, queue): | ||
| raise CliError("Mergify queue configuration became unsafe or unavailable while waiting") | ||
| state = mergify.membership(config.repo, pr.number, queue) | ||
| defer_landing = timeout and confirmed and state is not None and not closed | ||
| if not defer_landing and (sha := _landed(config, pr, closed)): | ||
| return sha | ||
| if state is not None and not confirmed: | ||
| mergify.mark_confirmed(config.repo, pr.number, pr.head_sha, queue) | ||
| confirmed = True | ||
| print(f"PR #{pr.number} confirmed in Mergify queue {queue}; waiting") | ||
| elif state is None: | ||
| if sha := _mergify_departure(config, pr, queue, closed, confirmed): | ||
| return sha | ||
| _report_closed_observation(closed) | ||
| print(f"timed out waiting for Mergify to land PR #{pr.number}") | ||
| print(_mergify_timeout_detail(config, pr, queue, state)) | ||
| print(_mergify_timeout_detail(config, pr, queue, state, query_report=not timeout)) | ||
| return None | ||
|
|
||
|
|
||
|
|
@@ -1653,7 +1697,10 @@ def _merge_with_mergify( | |
| mergify.request(config.repo, pr.number, pr.head_sha, queue) | ||
| requested = True | ||
| if deadline is not None: | ||
| return _wait_for_mergify(config, pr, max(0.0, deadline - time.monotonic()), confirmed) | ||
| return _queue_wait( | ||
| deadline, lambda remaining: _wait_for_mergify(config, pr, remaining, confirmed), | ||
| bounded=admit_by is not None, timeout_notice=f"timed out waiting for Mergify to land PR #{pr.number}", | ||
| ) | ||
| state_name = "confirmed in" if confirmed else "requested from" | ||
| print( | ||
| f"PR #{pr.number} {state_name} Mergify queue {queue}; " | ||
|
|
@@ -1712,12 +1759,10 @@ def cmd_merge(args: argparse.Namespace) -> int: | |
| if admit_by is not None and time.monotonic() > admit_by: | ||
| print(f"timed out before merging PR #{pr.number}") | ||
| return 3 | ||
| sha = github.merge(config.repo, pr.number, config.merge_strategy, pr.head_sha) | ||
| if sha is None and args.wait is not None: | ||
| sha = _wait_for_merge(config, pr, max(0.0, deadline - time.monotonic())) | ||
| if sha is None: | ||
| print(f"timed out waiting for the merge queue to land PR #{pr.number}") | ||
| return 3 | ||
| sha = _merge_and_follow(config, pr, deadline, admit_by) | ||
| if sha is None and deadline is not None: | ||
| print(f"timed out waiting for the merge queue to land PR #{pr.number}") | ||
| return 3 | ||
| if sha is None: | ||
| print( | ||
| f"PR #{pr.number} is queued for merge into {config.base_branch}; " | ||
|
|
||
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: A landing verified just before the deadline can still be dropped when the budget scope closes
_landedcallsread_timeout(0)aftermerged_sha, so a returned SHA was verified before the deadline. That SHA then goes back throughpoll, and the exit ofgithub.bounded_reads(github.py:135) callsread_timeout(0)again. If the deadline passes in that short gap,ReadDeadlineis raised and_queue_waitreturns None. The command then exits 3 even though the landing was verified in time, which goes against the acceptance rule that a landing verified before the deadline proceeds to cleanup.github.merge(..., observation_deadline=...)has the same race, because_verify_merge_requestreturns a SHA insidebounded_reads. The window is only microseconds, so this is unlikely in practice. Smallest fix: in_queue_wait, keepshaand return it when it is truthy even if the scope exit raisesReadDeadline. Make the same change inmerge(), or move the final check so it only runs on the None path.