Added keep alive setting - #41109
Conversation
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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
InstanceIdleTimeoutis used to implement “Keep WSL VM alive” by setting a negative timeout, but this header TextBlock formats the raw value viaMillisecondsStringConverter. 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}}"/>
# Conflicts: # localization/strings/en-US/Resources.resw
There was a problem hiding this comment.
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
_defaultInstanceIdleTimeoutback toInstanceIdleTimeout, 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/VMIdleTimeoutas 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:
| <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> |
| <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
left a comment
There was a problem hiding this comment.
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}}"/> |
There was a problem hiding this comment.
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)); |
There was a problem hiding this comment.
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> |
There was a problem hiding this comment.
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.
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.