GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Save the module analysis cache when the process exits by azchohfi · Pull Request #28376 · PowerShell/PowerShell

Conversation

@azchohfi

@azchohfi Alexandre Zollinger Chohfi (azchohfi) commented Nov 7, 2026

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@azchohfi
Save the module analysis cache when the process exits
05bd712
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 <[email protected]>
@azchohfi
Alexandre Zollinger Chohfi (azchohfi) requested a review from a team as a code owner November 7, 2026 04:51
Copilot AI balanced review requested due to automatic review settings November 7, 2026 04:51
@azure-pipelines

azure-pipelines Bot commented Nov 7, 2026

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Copilot started reviewing on behalf of Alexandre Zollinger Chohfi (azchohfi) November 7, 2026 04:52
View session
Copilot AI reviewed Nov 7, 2026
View reviewed changes

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

🟡 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
  • Medium severity Temporary cache snapshots are retained until process exit · New
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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/System.Management.Automation/engine/Modules/AnalysisCache.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@azchohfi
Only save the process-wide analysis cache at exit
5589415
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 <[email protected]>
@kilasuit 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
@azchohfi Alexandre Zollinger Chohfi (azchohfi) mentioned this pull request Nov 9, 2026
The module analysis cache is never saved by short-lived pwsh processes #28375
Open
@azchohfi
Merge branch 'master' into o35-analysis-cache-flush
f53a947
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

Copilot code review Copilot
Copilot review effort
Applies to this pull request for everyone.Learn more about Copilot code review.
Copilot left review comments

At least 1 approving review is required to merge this pull request.

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

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

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

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

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@azchohfi @kilasuit
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.