Skip to content

Added keep alive setting - #41109

Open
craigloewen-msft wants to merge 3 commits into
masterfrom
user/crloewen/wsl-keep-alive
Open

Added keep alive setting#41109
craigloewen-msft wants to merge 3 commits into
masterfrom
user/crloewen/wsl-keep-alive

Conversation

@craigloewen-msft

@craigloewen-msft craigloewen-msft commented Jul 17, 2026

Copy link
Copy Markdown
Member

Summary of the Pull Request

Added a setting to WSL settings to keep the WSL VM alive at all times.

PR Checklist

Very simple change.
Adds a 'InstanceIdleTimeout' setting and a "WSL keep VM alive" setting to WSL settings.

Detailed Description of the Pull Request / Additional comments

Did this in WSL settings only, no major code changes.

Validation Steps Performed

Validated settings loaded correctly.

Copilot AI review requested due to automatic review settings July 17, 2026 20:00
@craigloewen-msft
craigloewen-msft requested a review from a team as a code owner July 17, 2026 20:00

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.

Pull request overview

This PR adds a new WSL Settings UI toggle to keep the WSL VM running indefinitely by driving the existing wsl2.vmIdleTimeout setting into a negative value (which the service interprets as “do not idle-terminate”).

Changes:

  • Add a “Keep WSL VM alive” toggle to Optional Features, and disable the VM idle timeout expander when keep-alive is active.
  • Extend the Optional Features view model with keep-alive/enablement properties and related change notifications.
  • Add en-US localized strings (header/description and accessibility text) for the new setting.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
src/windows/wslsettings/Views/Settings/OptionalFeaturesPage.xaml Adds keep-alive toggle and disables the idle-timeout expander based on view model state.
src/windows/wslsettings/ViewModels/Settings/OptionalFeaturesViewModel.cs Adds computed properties for keep-alive and VM idle-timeout enablement, and updates change notification flow.
localization/strings/en-US/Resources.resw Adds localized strings for the new keep-alive setting and toggle accessibility metadata.

Comment thread src/windows/wslsettings/Views/Settings/OptionalFeaturesPage.xaml
Comment thread src/windows/wslsettings/ViewModels/Settings/OptionalFeaturesViewModel.cs Outdated
Copilot AI review requested due to automatic review settings July 27, 2026 18:09

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.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 4 comments.

Comments suppressed due to low confidence (1)

src/windows/wslsettings/Views/Settings/OptionalFeaturesPage.xaml:62

  • InstanceIdleTimeout is used to implement “Keep WSL VM alive” by setting a negative timeout, but this header TextBlock formats the raw value via MillisecondsStringConverter. With keep-alive enabled, this will display "-1 milliseconds" (and similarly for other negative values), which is user-unfriendly and doesn’t reflect the toggle semantics.
                <ctControls:SettingsExpander x:Name="InstanceIdleTimeoutExpander" x:Uid="Settings_InstanceIdleTimeout" IsEnabled="{x:Bind ViewModel.InstanceIdleTimeoutEnabled, Mode=OneWay}">
                    <TextBlock Style="{StaticResource TextBlockSettingStyle}" Text="{x:Bind ViewModel.InstanceIdleTimeout, Mode=OneWay, Converter={StaticResource MillisecondsStringConverter}}"/>

Comment thread src/windows/wslsettings/Views/Settings/OptionalFeaturesPage.xaml.cs
Comment thread src/windows/wslsettings/Views/Settings/SettingsApplyHelper.cs
Comment thread src/windows/wslsettings/LibWsl.cs
# Conflicts:
#	localization/strings/en-US/Resources.resw
Copilot AI review requested due to automatic review settings July 27, 2026 21:46
@craigloewen-msft
craigloewen-msft enabled auto-merge (squash) July 27, 2026 21:47

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.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (2)

src/windows/wslsettings/ViewModels/Settings/OptionalFeaturesViewModel.cs:93

  • Toggling “Keep WSL VM alive” off always writes _defaultInstanceIdleTimeout back to InstanceIdleTimeout, which can discard a user’s previously configured distribution idle timeout (e.g., user sets 30000ms → enables keep-alive → disables keep-alive → value becomes default 15000ms). Consider preserving/restoring the last non-negative timeout value so the toggle doesn’t lose user configuration.
    public bool IsOnKeepVMAlive
    {
        get { return _instanceIdleTimeout!.Int32Value < 0; }
        set
        {
            Set(ref _instanceIdleTimeout!, value ? -1 : _defaultInstanceIdleTimeout, nameof(IsOnKeepVMAlive));
            OnPropertyChanged(nameof(InstanceIdleTimeout));
            OnPropertyChanged(nameof(InstanceIdleTimeoutEnabled));
            OnPropertyChanged(nameof(VMIdleTimeoutEnabled));
            InstanceIdleTimeout_ResetEnabled = !Equals(_defaultInstanceIdleTimeout, _instanceIdleTimeout!.Int32Value);
        }

src/windows/wslsettings/Views/Settings/SettingsApplyHelper.cs:180

  • Pending changes formatting will currently show negative idle timeouts as “-1 ms”. Since the service treats values < 0 as “never idle-terminate”, the apply dialog should format negative InstanceIdleTimeout/VMIdleTimeout as a user-friendly localized value (e.g. “Never”) instead of a negative duration.
                    case WslConfigEntry.InitialAutoProxyTimeout:
                    case WslConfigEntry.InstanceIdleTimeout:
                    case WslConfigEntry.VMIdleTimeout:
                        return string.Format("Settings_MillisecondsStringFormat".GetLocalized(), (int)value);
                    default:

Comment on lines +2009 to +2014
<data name="Settings_InstanceIdleTimeout.Header" xml:space="preserve">
<value>Distribution idle timeout</value>
</data>
<data name="Settings_InstanceIdleTimeout.Description" xml:space="preserve">
<value>The number of milliseconds that a distribution is idle, before it is terminated.</value>
</data>
Comment on lines +61 to +62
<ctControls:SettingsExpander x:Name="InstanceIdleTimeoutExpander" x:Uid="Settings_InstanceIdleTimeout" IsEnabled="{x:Bind ViewModel.InstanceIdleTimeoutEnabled, Mode=OneWay}">
<TextBlock Style="{StaticResource TextBlockSettingStyle}" Text="{x:Bind ViewModel.InstanceIdleTimeout, Mode=OneWay, Converter={StaticResource MillisecondsStringConverter}}"/>

@benhillis benhillis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice, the mechanism checks out - _VmIsIdle() only returns true once no running instance has a valid client id, so pinning instanceIdleTimeout to a negative value does keep the VM up. A few things I hit while reading through it.

</ctControls:SettingsExpander.Items>
</ctControls:SettingsExpander>
<ctControls:SettingsExpander x:Name="InstanceIdleTimeoutExpander" x:Uid="Settings_InstanceIdleTimeout" IsEnabled="{x:Bind ViewModel.InstanceIdleTimeoutEnabled, Mode=OneWay}">
<TextBlock Style="{StaticResource TextBlockSettingStyle}" Text="{x:Bind ViewModel.InstanceIdleTimeout, Mode=OneWay, Converter={StaticResource MillisecondsStringConverter}}"/>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

When the toggle is on, InstanceIdleTimeout is -1, and MillisecondsStringConverter only special-cases ulong 0, so this collapsed summary ends up reading literally -1 ms. Same thing shows up in the apply-changes dialog, since SettingsApplyHelper.FormatValue runs the new entry through Settings_MillisecondsStringFormat - so toggling Keep WSL VM alive produces a confirmation line like Distribution idle timeout: -1 ms for a field the user never touched.

Could we have the converter return null (or something like Never) for negative values, and map the toggle to Settings_KeepVMAlive/Header in the apply summary?

get { return _instanceIdleTimeout!.Int32Value < 0; }
set
{
Set(ref _instanceIdleTimeout!, value ? -1 : _defaultInstanceIdleTimeout, nameof(IsOnKeepVMAlive));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Toggling off always snaps back to the built-in default, so someone who had instanceIdleTimeout = 5000 and flips this on and then off again silently loses their 5000. Worth stashing the previous positive value in a field and restoring that, falling back to _defaultInstanceIdleTimeout only when there wasn't one.

<value>Keep WSL VM alive</value>
</data>
<data name="Settings_KeepVMAlive.Description" xml:space="preserve">
<value>Keep the WSL virtual machine, and the distributions running inside it, from being automatically shut down after they have been idle.</value>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Two small wording things here. This is backed by general.instanceIdleTimeout, which also applies to WSL1 instances (see the LxssInstance path in LxssUserSession.cpp), so Keep WSL VM alive undersells what it actually does. It also doesn't start anything - it only stops an already-running VM from being shut down, and wsl --shutdown still works. Might be worth reflecting both in the description so nobody expects the VM to come up on boot.

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.

3 participants