Repository navigation
Save the module analysis cache when the process exits - #28376
Open
Alexandre Zollinger Chohfi (azchohfi) wants to merge 3 commits into
Open
Alexandre Zollinger Chohfi (azchohfi) wants to merge 3 commits into
Alexandre Zollinger Chohfi (azchohfi) wants to merge 3 commits into
Conversation
The analysis cache is written by a background task that waits at least 13 seconds after the last change. A pwsh process that runs one command and exits sooner, such as pwsh -Command or -File, never saves it, so every such process analyzes the same modules again when a command is auto-loaded. If a save is still pending when the process exits, write the cache from a ProcessExit handler. A lock keeps the exit handler and the delayed write from running at the same time. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Alexandre Zollinger Chohfi (azchohfi)
requested a review
from a team
as a code owner
November 7, 2026 04:51
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
Temporary cache snapshots register ProcessExit handlers that retain their entire Entries dictionaries until shutdown, allowing repeated merges to accumulate memory in long-running hosts.
1 open issue
What changed in this PR
Addresses #28375 by saving pending module analysis cache updates when PowerShell exits, avoiding repeated module analysis in short-lived processes.
Changes:
- Registers a process-exit handler when the first cache save is queued.
- Serializes pending writes under a shared lock while preserving delayed saves for long-running sessions.
- Adds a regression test checking that a short-lived process creates a nonempty cache file.
| File | Description |
|---|---|
test/powershell/engine/Module/ModuleAnalysisCache.Tests.ps1 |
Tests cache persistence after a short-lived process exits. |
src/System.Management.Automation/engine/Modules/AnalysisCache.cs |
Adds exit-time saving and synchronization with delayed writes. |
Review effort: Balanced
Serialize reads another process's cache into a temporary AnalysisCacheData to merge it, and that instance can queue a save from its delayed Cleanup. The ProcessExit handler captured the instance, so each such temporary instance stayed alive until the process exited. Register one static handler that only saves the process-wide cache. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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.

PR Summary
Fixes #28375.
The module analysis cache is written by a background task that waits at least 13 seconds after the last change, so a pwsh that runs one command and exits sooner (
pwsh -Command,pwsh -File) never saves it, and every such process analyzes the same modules again when a command is auto-loaded.When
QueueSerializationqueues the first save, it now also registers aProcessExithandler that writes the cache if the save is still pending. A lock keeps the handler and the delayed write from running at the same time. The delayed write is unchanged for long-running sessions. Once the cache is up to date nothing is queued, so later processes do not write it again.PR Context
Measured on Windows 11 26100 (i9-10900K), Release builds of master 1aac7f5 with and without this change (the two differ only in System.Management.Automation.dll), default cache location, 40 interleaved runs, median of paired differences with 95% confidence interval:
pwsh -NoProfile -NonInteractive -Command ...Get-ChildItem -Name src | Select-Object -First 5exitThe first run writes the cache (4 KB here); later runs did not rewrite it.
Tests: added
test/powershell/engine/Module/ModuleAnalysisCache.Tests.ps1, which starts pwsh withPSModuleAnalysisCachePathin TestDrive, auto-loads Get-ChildItem and checks that the cache file exists after the process exits. It fails without the change and passes with it. CommandDiscovery, Import-Module, ModulePath, Base-Directory and TabCompletion tests give the same results with and without the change on my machine.I used GitHub Copilot to investigate this and to help write the change and test; I reviewed the code and ran the tests above myself.
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header