WSL.Container API for existing containers and sessions
Is your feature request related to a problem? Please describe. While testing the new SDK the program crashed. Next time it failed to create the container because one with that name already existed. Right now it does not appear that there is a way to manage already existing containers.
Describe the solution you'd like
public class Session
{
public static void TerminateSession(string name);
public IReadOnlyList<ContainerInfo> GetContainers();
public Container? OpenContainer(string name); //Open existing container
}
class SessionSettings
{
public bool OpenExisting { get; set; }
public bool ReplaceExisting { get; set; }
}
class ContainerSettings
{
public bool ReplaceIfExist { get; set; }
}
Describe alternatives you've considered
wslc.exe --session MySession system session terminate
wslc.exe --session MySession container list --format json --all
wslc.exe --session MySession stop MyContainerName
wslc.exe --session MySession rm MyContainerName
Additional context Add any other context or screenshots about the feature request here.
NuGet: Microsoft.WSL.Containers 2.9.3 wslc 2.9.3.0
Logs are required for review from WSL team
If this a feature request, please reply with '/feature'. If this is a question, reply with '/question'. Otherwise, please attach logs by following the instructions below, your issue will not be reviewed unless they are added. These logs will help us understand what is going on in your machine.
How to collect WSL logs
Download and execute collect-wsl-logs.ps1 in an administrative powershell prompt:
Invoke-WebRequest -UseBasicParsing "https://raw.githubusercontent.com/microsoft/WSL/master/diagnostics/collect-wsl-logs.ps1" -OutFile collect-wsl-logs.ps1
Set-ExecutionPolicy Bypass -Scope Process -Force
.\collect-wsl-logs.ps1
The script will output the path of the log file once done.
If this is a networking issue, please use .\collect-wsl-logs.ps1 -LogProfile networking instead, following the instructions in Collect WSL logs for networking issues
Once completed please upload the output files to this GitHub issue.
See Collect WSL logs (recommended method).
If you choose to email these logs instead of attaching them to the bug, please send them to [email protected] with the GitHub issue number in the subject, and include a link to your GitHub issue comment in the message body, and reply with '/emailed-logs'.
/feature
Diagnostic information
Found '/feature', adding tag 'feature'
Logs are required for review from WSL team
If this a feature request, please reply with '/feature'. If this is a question, reply with '/question'. Otherwise, please attach logs by following the instructions below, your issue will not be reviewed unless they are added. These logs will help us understand what is going on in your machine.
How to collect WSL logs
Download and execute collect-wsl-logs.ps1 in an administrative powershell prompt:
Invoke-WebRequest -UseBasicParsing "https://raw.githubusercontent.com/microsoft/WSL/master/diagnostics/collect-wsl-logs.ps1" -OutFile collect-wsl-logs.ps1
Set-ExecutionPolicy Bypass -Scope Process -Force
.\collect-wsl-logs.ps1
The script will output the path of the log file once done.
If this is a networking issue, please use .\collect-wsl-logs.ps1 -LogProfile networking instead, following the instructions in Collect WSL logs for networking issues
Once completed please upload the output files to this GitHub issue.
See Collect WSL logs (recommended method).
If you choose to email these logs instead of attaching them to the bug, please send them to [email protected] with the GitHub issue number in the subject, and include a link to your GitHub issue comment in the message body, and reply with '/emailed-logs'.
/feature
Diagnostic information
Found '/feature', adding tag 'feature'
Logs are required for review from WSL team
If this a feature request, please reply with '/feature'. If this is a question, reply with '/question'. Otherwise, please attach logs by following the instructions below, your issue will not be reviewed unless they are added. These logs will help us understand what is going on in your machine.
How to collect WSL logs
Download and execute collect-wsl-logs.ps1 in an administrative powershell prompt:
Invoke-WebRequest -UseBasicParsing "https://raw.githubusercontent.com/microsoft/WSL/master/diagnostics/collect-wsl-logs.ps1" -OutFile collect-wsl-logs.ps1
Set-ExecutionPolicy Bypass -Scope Process -Force
.\collect-wsl-logs.ps1
The script will output the path of the log file once done.
If this is a networking issue, please use .\collect-wsl-logs.ps1 -LogProfile networking instead, following the instructions in Collect WSL logs for networking issues
Once completed please upload the output files to this GitHub issue.
See Collect WSL logs (recommended method).
If you choose to email these logs instead of attaching them to the bug, please send them to [email protected] with the GitHub issue number in the subject, and include a link to your GitHub issue comment in the message body, and reply with '/emailed-logs'.
Merged API requests for Session termination #40997 as requested by @florelis
Commenting question regarding whether to allow opening an existing session.
The wslc.exe command already can operate inside an existing session so the ability would be expected in the API as well.
Commenting here since it was suggested in issue #41087. Would solving this issue also provide access to automatically started processes in the container via API? That is the requiredment described there and it seems like its a gap in the current API.
The API gap here is real and it's not just about recovery from crashes. Right now Microsoft.WSL.Containers exposes CreateContainer/CreateProcess but has no way to enumerate existing containers in a session or attach to one by name after a restart. If your process dies mid-session, the container keeps running but your app has lost the reference — there's no Session.GetContainers() or Session.OpenContainer(name) to get it back.
This also blocks scenarios like:
- A monitoring tool that wants to list all containers across sessions without creating them
- A CLI wrapper (see #41058 for a community wrapper that works around this by shelling out to
wslc.exe) - Any long-running app that needs to survive a process restart
The feature request for DOCKER_HOST / Engine API endpoint (#40976) is the other half of this coin — an API-compatible endpoint would let existing Docker tooling discover wslc containers. Until either lands, tools that need container lifecycle management have to either call wslc.exe from the CLI side or not use wslc at all.
(I track the open API/management gaps at https://wslcontainers.com/requirements/ — #40990 is the most-commented API request there.)
Some findings from the source that may help scope this.
1. Listing a session's containers
The service already implements this: IWSLCSession::ListContainers (wslc.idl#L760) is what wslc container list uses. But the SDK talks to the service only through IWSLCCompatSession (WSLCCompat.idl#L316), which has ListImages but no ListContainers. So an app can't find the containers of a session after a restart.
Our workarounds so far:
- Keeping our own on-disk record of container ids. It misses anything created outside.
- Calling the internal
IWSLCSessionManager::OpenSessionByName→IWSLCSession::ListContainersdirectly. This works, but it shows why a stable entry point matters: between 2.9.9 and 3.0.0,ListContainersmoved from vtable slot 18 to 19 under the same IID, andWSLCContainerEntrychanged layout. We now have to refuse to call it on anything but a verified 3.0.x service.
Adding ListContainers to the compat surface (and Session.GetContainers() / a WslcListContainers export in the SDK) would remove the need for either workaround.
2. Opening a session that is already running
If another process holds a session (or a crashed one left it running, #40997), Session.Start() fails with 0x800700B7 ("Cannot create a file when that rvice already supports joining instead:
- It returns
ERROR_ALREADY_EXISTSonly whenWSLCSessionFlagsOpenExistingis clear (WSLCSessionManager.cpp#L231, flag at WSLCShared.idl#L252). IWSLCCompatSessionManager::CreateSessionalready takesWSLCSessionFlags(WSLCCompat.idl#L363).- The SDK always passes
WSLCSessionFlagsNone(wslcsdk.cpp#L453).
So exposing it looks like an SDK-only change, with no ABI change: for example SessionSettings.OpenExisting (plus Persistent, which would also answer the "survive a restart" part of this issue). Even without it, a dedicated error such as "session is already in use" would be much easier to handle than ERROR_ALREADY_EXISTS.
3. Listing sessions
IWSLCSessionManager::ListSessions (wslc.idl#L900) returns each live session's name, id and creator PID, but the compat manager has no equivalent. With it, tools could show which sessions are running and which process holds one, instead of guessing why Start() failed.
Possible shape
public class Session
{
public IReadOnlyList<ContainerInfo> GetContainers(bool includeStopped = true); // Id, Name, Image, State, Status, CreatedAt, Ports
}
public class SessionSettings
{
public bool OpenExisting { get; set; } // WSLCSessionFlagsOpenExisting
public bool Persistent { get; set; } // WSLCSessionFlagsPersistent
}
public static class WslcService
{
public static IReadOnlyList<SessionInfo> GetSessions(); // Name, Id, CreatorPid
}
Happy to test a prerelease.