Repository navigation
Reuse passing checks and guard concurrent runs at the same head #1557
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
1bf2e16
b5e1016
e2cbc3a
2c53eca
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 |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ | |
|
|
||
| import argparse | ||
| import dataclasses | ||
| import errno | ||
| import fcntl | ||
| import hashlib | ||
| import importlib.metadata | ||
|
|
@@ -591,15 +592,44 @@ def cmd_start(args: argparse.Namespace) -> int: | |
| return 0 | ||
|
|
||
|
|
||
| @contextmanager | ||
| def _lock_check(wt: Path, head: str) -> Iterator[None]: | ||
| git_dir = Path(_git(wt, "rev-parse", "--absolute-git-dir")) | ||
| # Lock existing worktree metadata, which Git leaves in place when HEAD moves. | ||
| path = git_dir / "commondir" | ||
| if not path.is_file(): | ||
| path = git_dir / "config" | ||
| with path.open("r+b", buffering=0) as lock: | ||
| try: | ||
| fcntl.lockf(lock, fcntl.LOCK_EX | fcntl.LOCK_NB, 1, int(head[:15], 16)) | ||
| except OSError as exc: | ||
|
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. important: Preserve exclusion between checks in the same process
|
||
| if exc.errno not in (errno.EACCES, errno.EAGAIN): | ||
| raise | ||
| raise CliError(f"check already running in this worktree at {head[:12]}") from exc | ||
| yield | ||
|
|
||
|
|
||
| def cmd_check(args: argparse.Namespace) -> int: | ||
| wt, config = _context(args, pin_checks=True) | ||
| if not config.checks: | ||
| raise config_mod.ConfigError("checks.commands must list at least one command to run checks") | ||
| head = worktree.head(wt) | ||
| with _lock_check(wt, head): | ||
| report = _current_check_report(wt, config, head) | ||
| if (report is not None and sandbox.receipt_inputs_match(config, wt, report) | ||
| and not worktree.is_dirty(wt) and worktree.head(wt) == head): | ||
| _refresh_pr_checks(wt, config) | ||
| print(f"PASS (reused {CHECKS_FILE} for {head[:12]} without running stages)") | ||
| return 0 | ||
| return _run_check(args, wt, config, head) | ||
|
|
||
|
|
||
| def _run_check(args: argparse.Namespace, wt: Path, config: Config, head: str) -> int: | ||
| try: | ||
| report = sandbox.run_checks(config, wt, progress=lambda command: print( | ||
| f"running: {command}", flush=True), | ||
| status=lambda message: print(message, flush=True), | ||
| checkpoint=lambda partial: _save_report(wt, partial), | ||
| checkpoint=lambda partial: _save_report(wt, partial), expected_head=head, | ||
| workflow=args.config or f"origin/{config.base_branch}:workflow.toml") | ||
| except sandbox.SandboxUnavailable as exc: | ||
| print(f"sandbox unavailable: {exc}", file=sys.stderr) | ||
|
|
||
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.
important: Use a stable lock file in the main worktree
In the main worktree, the lock is held on the inode of
.git/config. A concurrentgit config --localwrite replaces that file by rename without changing HEAD. A second check then opens the new inode and acquires the same byte-range lock while the first check is still running. Use a dedicated lock file under the worktree Git directory, keep it in place across runs, and cover a config write between the two acquisitions.