WSL icon indicating copy to clipboard operation
WSL copied to clipboard

WSL.Container API for existing containers and sessions

Open hultqvist opened this issue 4 months ago • 12 comments

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

hultqvist avatar Jul 03 '26 18:07 hultqvist

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'.

github-actions[bot] avatar Jul 03 '26 18:07 github-actions[bot]

/feature

hultqvist avatar Jul 03 '26 18:07 hultqvist

Diagnostic information
Found '/feature', adding tag 'feature'

github-actions[bot] avatar Jul 03 '26 18:07 github-actions[bot]

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'.

github-actions[bot] avatar Jul 03 '26 18:07 github-actions[bot]

/feature

hultqvist avatar Jul 03 '26 18:07 hultqvist

Diagnostic information
Found '/feature', adding tag 'feature'

github-actions[bot] avatar Jul 03 '26 18:07 github-actions[bot]

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'.

github-actions[bot] avatar Jul 08 '26 04:07 github-actions[bot]

Merged API requests for Session termination #40997 as requested by @florelis

hultqvist avatar Jul 08 '26 05:07 hultqvist

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.

hultqvist avatar Jul 08 '26 05:07 hultqvist

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.

Sonix avatar Aug 03 '26 09:08 Sonix

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.)

ylwl1997 avatar Aug 04 '26 03:08 ylwl1997

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::ListContainers directly. This works, but it shows why a stable entry point matters: between 2.9.9 and 3.0.0, ListContainers moved from vtable slot 18 to 19 under the same IID, and WSLCContainerEntry changed 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:

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.

aberus avatar Sep 30 '26 09:09 aberus