FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Save the module analysis cache when the process exits by azchohfi · Pull Request #28376 · PowerShell/PowerShell · GitHub

Repository navigation

Save the module analysis cache when the process exits - #28376

Open
Alexandre Zollinger Chohfi (azchohfi) wants to merge 3 commits into
PowerShell:masterfrom
azchohfi:o35-analysis-cache-flush
Open

Alexandre Zollinger Chohfi (azchohfi) wants to merge 3 commits into
PowerShell:masterfrom
azchohfi:o35-analysis-cache-flush

Conversation

Copy link
Copy Markdown

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 QueueSerialization queues the first save, it now also registers a ProcessExit handler 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 ... before after difference
Get-ChildItem -Name src | Select-Object -First 5 548 ms 447 ms -98.4 ms (-108.8 to -89.5)
exit 348 ms 346 ms -3.0 ms (-7.9 to 3.8)

The 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 with PSModuleAnalysisCachePath in 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

  • PR has a meaningful title
  • Summarized changes
  • Make sure all .h, .cpp, .cs, .ps1 and .psm1 files have the correct copyright header
  • This PR is ready to merge.
  • Breaking changes
    • None
  • User-facing changes
    • Not Applicable
  • Testing - New and feature
    • Make sure you've added a new test if existing tests do not effectively test the code changed

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>
Copilot AI balanced review requested due to automatic review settings November 7, 2026 04:51

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

🟡 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>
Ryan Yates (kilasuit) added WG-Engine core PowerShell engine, interpreter, and runtime WG-NeedsReview Needs a review by the labeled Working Group labels Nov 8, 2026
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

Labels

WG-Engine core PowerShell engine, interpreter, and runtime WG-NeedsReview Needs a review by the labeled Working Group

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The module analysis cache is never saved by short-lived pwsh processes

3 participants


Back | FazBrowse Home | New Git URL