Skip to content

bug(apps): Windows busy delivery lock escapes as PermissionError #494

Closed

Description

@zengchuang-hw

Refs #490
Refs #493

Problem

On Windows, exporting a managed optimization campaign while its writer holds .session.lock raises a raw PermissionError instead of the documented retryable SnapshotBusy / writer-active error.

infra.artifacts.file_lock calls msvcrt.locking(..., LK_NBLCK, 1). Lock contention raises PermissionError: [Errno 13] Permission denied. apps.delivery.project catches BlockingIOError, so this Windows contention case bypasses the public error translation.

Reproduction and observed evidence

Environment was Windows and CPython 3.12. Run the existing regression with the checkout installed or its src on PYTHONPATH.

python -m pytest -o addopts= -q --tb=short tests/test_result_delivery.py::test_progress_uses_committed_control_and_a_fixed_clock

The test holds optimization._locked(root) and calls delivery.project(root, export_id="busy"). It expects ValueError matching writer is active through the SnapshotBusy subclass. The observed exception originates at src/inferrsi/infra/artifacts.py:28 and escapes through src/inferrsi/apps/delivery/__init__.py:123.

The same test fails on an unmodified archive of base 8aba657da79e8ed9f6e6fbb33437e9d9fdd8f1c7 and on #493. The storage implementation bodies in #493 are unchanged; its direct import migration does not introduce this failure. No assertion or platform coverage was weakened.

Proposed bounded correction and acceptance

  • Normalize genuine lock contention at the appropriate owning boundary so Apps reports its existing retryable writer-active error on Windows and POSIX.
  • Preserve real permission, missing-path and unrelated filesystem errors; do not treat every OSError as a busy writer.
  • Keep the existing nested-lock regression and add focused coverage distinguishing contention from unrelated I/O failures, including lock release/retry.
  • Verify on Windows and POSIX. Existing Linux full-check and the narrower Windows host-compatibility checks do not establish that this full Windows test passes.

This issue tracks the baseline limitation requested during #490's design and implementation follow-up. This correction is separate from the physical directory migration in #493.

Activity

  1. Dong1017 commented on Nov 18, 2026

    @Dong1017
    Collaborator

    Posted by Dong-bot, an automated agent acting on behalf of @Dong1017 (always-on-bot, model claude-sonnet-5-5).

    Assessment: This looks like a valid, well-scoped Windows-only bug. msvcrt.locking(..., LK_NBLCK, 1) reports lock contention as PermissionError (Errno 13). On POSIX the equivalent contention surfaces as BlockingIOError, which apps.delivery.project catches. So the Windows path skips the SnapshotBusy / "writer is active" translation. The issue's claim that this predates #493 is credible. It reproduces on base 8aba657 and the storage bodies in #493 are unchanged. I haven't run anything myself. This is based on the issue text only.

    Where to fix it: I'd normalize at the owning boundary, infra.artifacts.file_lock. That function is where the platform difference originates, and it avoids Apps needing to know about msvcrt semantics. Two options:

    1. file_lock catches the Windows contention error and re-raises BlockingIOError (or a dedicated LockContention subclass of BlockingIOError). Apps then stays unchanged and POSIX behavior is identical.
    2. Widen the except in apps.delivery to include PermissionError. I'd avoid this because it treats genuine permission failures as a busy writer, which the acceptance criteria forbid.

    Option 1 is preferable.

    Risks and gaps to settle before implementing:

    • How to tell contention from a real permission error. A bare PermissionError is ambiguous. On Windows, msvcrt.locking failing under LK_NBLCK reports EACCES (13), and a real denied-open also reports EACCES. The safest split is by call site. Only exceptions raised by the msvcrt.locking call itself, after the lock file has been opened successfully, should be mapped to contention. Errors from open() or the path stay untouched. The issue should state this explicitly.
    • Other msvcrt errors. EDEADLOCK (36) can also occur with the blocking LK_LOCK variants. Confirm it is not reachable here, or map it deliberately.
    • Other callers of file_lock. Check whether other callers (the optimization writer, other apps) depend on seeing PermissionError today. Changing the exception type is a behavior change for them.
    • Lock release and retry. The acceptance criteria mention this. The test should show that after the holder releases, a second acquire succeeds. It should also show that a failed acquire doesn't leave a handle or partial lock behind.
    • Test strategy. The unit coverage for "contention vs. unrelated I/O" should not need real Windows. Monkeypatch msvcrt.locking to raise PermissionError, and separately make open raise PermissionError and FileNotFoundError. Keep one real-lock test that runs on both platforms.
    • CI. The issue says Linux full-check and the narrow Windows host-compat checks don't cover this test. A Windows job that runs tests/test_result_delivery.py, or the whole suite, would close that gap. It could be a follow-up if the Windows runner cost is a concern. Say which.

    Suggested next steps:

    1. Decide on option 1 and state the contention-vs-permission rule (by call site, as above) in the issue.
    2. Implement it in infra/artifacts.py, keep test_progress_uses_committed_control_and_a_fixed_clock as is, and add the focused tests listed above.
    3. Report Windows and POSIX results in the PR. Keep this PR separate from the refactor(apps): group artifact storage under apps.storage #493 directory migration, as the issue already says.

    Not verified: I have not run the regression or inspected the code at artifacts.py:28 or delivery/__init__.py:123. The points above rely on the issue's description.

  2. added
    area:harnessHarness module: profile, A/B loop, quality gates, evidence reports
    kind:bugDefect report: an observable symptom that differs from expected behavior
    on Nov 18, 2026
  3. david6666666 commented on Nov 18, 2026

    @david6666666
    Collaborator

    Could the Windows regression exercise both directions of contention on the same .session.lock? At the merged storage commit, Apps export acquires it through infra.artifacts.file_lock, while Harness optimization uses its separate R2 artifact_primitives.file_lock. An Infra-only change can fix the existing writer-to-export case without testing what the Harness caller sees. Please verify both directions and reacquisition, and state whether error normalization is export-only or shared without reintroducing a Harness-to-Infra import.

  4. zengchuang-hw commented on Nov 19, 2026

    @zengchuang-hw
    ContributorAuthor

    在 #490 第四步集成验收中再次复现同一已有问题。未修改主线 9c7b7b05042f54ca71d13b2897a7505d818d3429 的源码归档,Windows / Python 3.12,执行本 issue 所列同一节点:1 failed,仍为 msvcrt.locking(... LK_NBLCK, 1) 的 PermissionError 逃逸。Apps delivery、infra.artifacts 及该用例相对目录切片均未改动。

    目录切片候选 b67e79d1 的原始集成组为 136 passed、1 failed 并在该节点停止;后续单独排除该已记录失败以检查剩余 delivery / Dashboard / trace 节点,109 passed、2 skipped(本地缺浏览器依赖)、1 deselected。没有改测试断言或把排除结果当作全组通过;#494 仍待独立修复。

  5. amy-why-3459 commented on Nov 20, 2026

    @amy-why-3459
    Collaborator

    The Windows PermissionError from msvcrt.locking(..., LK_NBLCK) bypassing SnapshotBusy looks like a real, pre-existing platform bug. The later reproduction on unmodified main 9c7b7b05 supports keeping this as an independent fix, separate from the #490/#493 directory migration.

    I agree the owning boundary is infra.artifacts.file_lock. Only the contention exception raised by msvcrt.locking after the lock file has been opened successfully should be translated; PermissionError from open() or a missing path must stay a real permission or path error. Please also address the shared-lock point: at the merged storage commit, Apps export acquires .session.lock through infra.artifacts.file_lock, while Harness optimization uses a separate artifact_primitives.file_lock. The PR should say whether normalization is export-only or shared, exercise both directions plus reacquisition, and avoid reintroducing a Harness-to-Infra import. Please also check whether EDEADLOCK is reachable here, and whether any other file_lock caller currently depends on seeing PermissionError.

  6. Dong1017 commented on Nov 22, 2026

    @Dong1017
    Collaborator

    @Dong1017 claim: fix the Windows lock-contention escape in infra.artifacts.file_lock so that contention maps to the documented retryable busy error, with a regression for both contention directions on the same .session.lock as asked above. Please have the PR pool implement it.

    Posted by Claude Code at Dong1017's request, to trial the PR pool on a small, well-scoped bug. zengchuang-hw, as the author, please reply here if you would rather take it yourself, and the task will be withdrawn.

  7. Dong1017 commented on Nov 22, 2026

    @Dong1017
    Collaborator

    Posted by Dong-bot, an automated agent acting on behalf of @Dong1017 (always-on-bot, model claude-sonnet-5-5).

    Queued for the PR pool: the fix for the Windows lock-contention escape in infra.artifacts.file_lock, as Dong1017 asked by hand. It stays queued unless zengchuang-hw says they want to take it, in which case the task is withdrawn.

    Scope given to the pool

    • Translate only the contention error raised by msvcrt.locking(..., LK_NBLCK), after the lock file has opened successfully, into BlockingIOError. Apps then reports SnapshotBusy / "writer is active" without any change to its except.
    • PermissionError or FileNotFoundError from open() or the path, and other OSErrors, must pass through unchanged.
    • Regression for both contention directions on the same .session.lock, plus release, reacquisition and no leaked handle on a failed acquire. Keep the existing nested-lock test unchanged.
    Details
    • Both directions: Harness artifact_primitives.file_lock holds the lock while Apps export tries, and the reverse.
    • Unit tests monkeypatch msvcrt.locking. Add one real-lock test that runs on both platforms.
    • No Harness-to-Infra import. The PR must say whether the normalization is export-only or shared.
    • The PR must also say whether EDEADLOCK is reachable and whether any other file_lock caller relies on seeing PermissionError.
    • Keep this separate from the refactor(apps): group artifact storage under apps.storage #493 directory migration. Report Windows and POSIX results in the PR.
    • I have not run anything or read the code. The pool will plan it and either open a PR or report why it can't.
  8. Dong1017 commented on Nov 22, 2026

    @Dong1017
    Collaborator

    Posted by Dong-bot, an automated agent acting on behalf of @Dong1017 (always-on-bot, model claude-opus-5-5).

    The PR pool agent did not take this (#494: in infra.artifacts.file_lock map only msvcrt.locking contention (not open()/path errors) to BlockingIOError so export reports SnapshotBusy on Windows; test both directions on one .session.lock plus release/retry; no Harness-to-Infra import.):

    #494: nothing to implement, already fixed on base main 7f42e17

    Details

    The scope given to the pool is already merged on main (7f42e17). A PR from this run
    would be empty or would only duplicate existing code and tests. A maintainer needs to
    decide whether to close #494 or change what it asks for.

    What the base already has

    • No infra.artifacts module. src/inferrsi/infra/ no longer exists.
      tests/test_artifact_helper_copies.py says "task: retire the infra/ and model/ packages into their owning modules #498 slice 3b added the Apps copy and
      slice 5 deleted the Infra one". Apps export now locks through inferrsi.apps.artifacts.file_lock
      (src/inferrsi/apps/delivery/__init__.py:12, :127).
    • Only msvcrt.locking contention is translated, in both copies.
      src/inferrsi/apps/artifacts.py:31-37 and src/inferrsi/harness/artifact_primitives.py:25-31
      wrap only the msvcrt.locking(fd, LK_NBLCK, 1) call, which runs after path.open("a+b")
      succeeded. Only PermissionError with errno == EACCES becomes
      BlockingIOError(EAGAIN, ...) with the original as __cause__. open() and path errors
      and any other errno pass through unchanged. apps.delivery.project already maps
      BlockingIOError to SnapshotBusy ("writer is active"), so Apps needs no change.
    • The fix is shared, with no Harness-to-Apps or Harness-to-Infra import. The two copies
      have identical source, enforced by test_every_copy_has_the_same_source. The AST check in
      tests/test_skill_evaluation.py::test_harness_artifact_primitives_match_apps_bytes_and_semantics
      asserts that harness/artifact_primitives.py imports no inferrsi module.
    • The tests the issue asks for already exist:
      • tests/test_artifact_helper_copies.py::test_windows_only_translates_locking_contention
        is parametrized over both copies and over EACCES/EPERM/EBADF. It uses a fake msvcrt
        and covers translation only for EACCES, pass-through for the others, then release and
        reacquire on the same .session.lock with the exact locking call sequence.
      • tests/test_artifact_helper_copies.py::test_lock_preserves_errors_opening_the_file
        checks that FileNotFoundError for a missing directory and PermissionError from
        open() pass through unchanged (same object).
      • tests/test_session_lock_across_processes.py::test_a_held_lock_excludes_the_other_copy_in_another_process
        uses real processes on the same .session.lock in both directions (harness→apps and
        apps→harness). It expects BlockingIOError while the lock is held and a successful
        reacquire after release.
      • tests/test_session_lock_across_processes.py::test_an_apps_export_of_a_valid_session_is_refused_while_a_harness_process_holds_it
        with tests/session_lock_probe.py covers a real Harness optimization._locked holder
        against Apps export_bundle: "writer is active", nothing published, then a published
        progress export after release.
      • The issue's original node
        tests/test_result_delivery.py::test_progress_uses_committed_control_and_a_fixed_clock
        is unchanged.
    • Answers to the review questions, from the code:
      • EDEADLOCK is not reachable. Only LK_NBLCK is used, and the CRT reports EDEADLOCK
        only after the retry loop of LK_LOCK/LK_RLCK. If it ever appeared, it would pass
        through because only EACCES is translated.
      • No file_lock caller depends on seeing PermissionError. The callers are
        harness/optimization.py and harness/agent_sessions.py (_locked on .session.lock),
        apps/workflows/benchmark_compare.py:620 (.compare.lock) and apps/delivery.
        harness/optimization_session.py:295-296 already labels BlockingIOError as
        "Campaign lock is busy". The PermissionError handlers in apps/delivery/snapshot.py
        cover reads of source files, not the lock.

    What I ran (Linux, CPython in a venv; no Windows host available)

    SETUPTOOLS_SCM_PRETEND_VERSION_FOR_INFERRSI=0.0.0 pip install -e ".[dev]". The pretend
    version was needed because this checkout has no git binary.

    python -m pytest -o addopts= -q --tb=short -p no:cacheprovider \
      tests/test_artifact_helper_copies.py tests/test_session_lock_across_processes.py \
      "tests/test_result_delivery.py::test_progress_uses_committed_control_and_a_fixed_clock" \
      tests/test_skill_evaluation.py::test_harness_artifact_primitives_match_apps_bytes_and_semantics
    

    Result: 23 passed in 3.67s. Without git I could not find the commit that added the
    EACCES translation.

    What would unblock it (maintainer decision)

    1. Close bug(apps): Windows busy delivery lock escapes as PermissionError #494 as fixed on main after someone with a Windows host runs the command above
      on 7f42e17 and posts the result. This pool cannot run Windows, and the issue's last
      acceptance item ("Verify on Windows and POSIX") needs it. The earlier Windows failures
      reported in the issue were on 8aba657 and 9c7b7b05, which predate this code.
    2. Or restate the remaining gap if there is one, for example a Windows CI job that runs
      tests/test_result_delivery.py and tests/test_session_lock_across_processes.py. That
      change would touch .github/, which is outside this pool's allowed scope, and AGENTS.md
      forbids adding CI lanes, so it needs explicit human direction.
  9. zengchuang-hw commented on Nov 22, 2026

    @zengchuang-hw
    ContributorAuthor

    Completed: retryable Windows lock contention

    Closing under the requester's instruction to close completed issues, after checking the merged implementation and completing the previously requested native Windows verification.

    flowchart LR
        A["Harness or Apps holds session lock"] --> B["Other owner attempts lock"]
        B --> C["Contention becomes retryable busy"]
        C --> D["Release and reacquire succeeds"]
        B --> E["Other I/O errors remain unchanged"]
    
    Loading
    • Delivered: #546 merged as 4bc192767946e75de04d83d5b5c51a079cc6311e. Only PermissionError(EACCES) from msvcrt.locking(..., LK_NBLCK, ...) after a successful open is translated. Both owner-local helper copies preserve parity; Apps retains its existing SnapshotBusy translation. No Harness-to-Apps/Infra import was introduced.
    • Verified: on clean main b781edc2908969cc45b628adcaf62f57f2a28ad7, native Windows / Python 3.12, 23 lock/delivery tests passed, 0 skipped. This covers both cross-process contention directions on the same .session.lock, release/retry, managed export refusal followed by successful publication, helper-source parity, EACCES translation and preservation of unrelated errors. The original nested-lock regression passed unchanged. The same run also passed 33 project-explanation tests: 56 total, no failures/errors/skips. Existing POSIX results are recorded in fix(apps): report Windows session lock contention as retryable #546; its final-head Ready CI passed. Lock-helper/probe sources and focused cross-process tests are unchanged between the merged fix and checked main.
    • Limits: this accepts the bounded lock-contention correction. It does not close task: Apps 应用层详细设计与迁移计划(CLI / Python / Plugin) #371's remaining production-port integration or add a Windows CI requirement. Earlier Windows failures remain historical records.
    Verification and review details

    The 23-test group consists of tests/test_artifact_helper_copies.py (18), tests/test_session_lock_across_processes.py (3), the original tests/test_result_delivery.py::test_progress_uses_committed_control_and_a_fixed_clock (1), and tests/test_skill_evaluation.py::test_harness_artifact_primitives_match_apps_bytes_and_semantics (1).

    The merged PR explains EDEADLOCK versus nonblocking acquisition: only LK_NBLCK is used; other error codes remain unchanged. Open/path/release errors are not relabeled as contention. Both Apps and Harness helpers are fixed, so this is shared behavior, not an export-only workaround. The later PR-pool report that no additional implementation was needed is consistent with the merged fix; native Windows verification is now recorded here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:harnessHarness module: profile, A/B loop, quality gates, evidence reportskind:bugDefect report: an observable symptom that differs from expected behavior

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions