Skip to content

Add support for wslc container restart command#41005

Draft
beena352 wants to merge 2 commits into
microsoft:masterfrom
beena352:user/beenachauhan/container-restart
Draft

Add support for wslc container restart command#41005
beena352 wants to merge 2 commits into
microsoft:masterfrom
beena352:user/beenachauhan/container-restart

Conversation

@beena352

@beena352 beena352 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Summary of the Pull Request

Adds wslc container restart which stops and then starts a container, matching Docker CLI semantics. Supports --signal and --time options (defaults: SIGTERM, 5s timeout).

PR Checklist

  • Closes: Link to issue #xxx
  • Communication: I've discussed this with core contributors already. If work hasn't been agreed, this work might be rejected
  • Tests: Added/updated if needed and all pass
  • Localization: All end user facing strings can be localized
  • Dev docs: Added/updated if needed
  • Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

Behavior:

  • Running container: sends signal, waits for timeout, then starts.
  • Stopped/Exited container: skips stop phase, starts directly.
  • Created (never-started) container: starts directly.
  • --rm container: refused with ERROR_INVALID_STATE — Stop would trigger auto-delete via OnStopped, destroying the container.
  • Not-found container: returns WSLC_E_CONTAINER_NOT_FOUND.

Implementation path: ContainerRestartCommandContainerTasks::RestartContainersContainerService::RestartIWSLCContainer::RestartWSLCContainerImpl::Restart.

N.B. Lock design: WSLCContainerImpl::Restart snapshots state/flags under a shared lock, then releases before calling Stop and Start (which each take the lock exclusively). This is intentional - m_lock is a non-recursive wil::srwlock. If the container self-exits between the snapshot and Stop, Stop no-ops on the Exited state.

Follow-up: To achieve full Docker parity for --rm containers (Docker allows restart on --rm by suppressing auto-remove during the restart window), a follow-up PR will add a member flag m_restartInProgress guarded by wil::scope_exit and checked in OnStopped. This closes the race where a natural container exit between Stop and Start would trigger auto-delete. Shipping the --rm guard (refuse) now is the safe default; the member-flag approach needs its own review and dedicated tests for the self-exit race.

Validation Steps Performed

Unit tests (test/windows/WSLCTests.cppContainerRestart):

  • Restart running container → stops then starts, state returns to Running
  • Restart exited container → starts, state returns to Running
  • Restart created container → starts, state returns to Running
  • Restart --rm container → refused with ERROR_INVALID_STATE and exact localized message
  • Restart on already-deleted COM object → fails with RPC_E_DISCONNECTED (object disconnected after delete)

E2E tests (test/windows/wslc/e2e/WSLCE2EContainerRestartTests.cpp):

  • --help output
  • Restart running container (by ID)
  • Restart stopped container
  • Restart by name
  • Container not found
  • Only targeted container restarted (multi-container)
  • --rm container refused with exact error + HRESULT
  • Invalid signal (0, 32)
  • Invalid timeout (non-numeric)

Manual validation:

  • wslc container run -d --name test debian:latest sleep infinity
  • wslc container restart test -t 0
  • wslc container ls # confirms running
  • wslc container stop test -t 0
  • wslc container restart test -t 0
  • wslc container ls # confirms running again

Copilot AI review requested due to automatic review settings July 6, 2026 18:14

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 wslc container restart subcommand that mirrors Docker CLI semantics by stopping (with configurable --signal/--time) and then starting a container, with end-to-end wiring from CLI → service layer → COM interface → session implementation, plus unit/E2E coverage and localized strings.

Changes:

  • Add a new CLI command wslc container restart and route it through ContainerTasks and ContainerService.
  • Extend the internal IWSLCContainer COM interface with Restart(...) and implement restart logic in WSLCContainerImpl.
  • Add unit and E2E tests, update container subcommand help expectations, and add localization strings for help + user-facing errors.

Reviewed changes

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

Show a summary per file
File Description
test/windows/WSLCTests.cpp Adds unit tests for container restart behaviors and error cases.
test/windows/wslc/e2e/WSLCE2EContainerTests.cpp Updates container command help to include the new restart subcommand.
test/windows/wslc/e2e/WSLCE2EContainerRestartTests.cpp New E2E tests validating container restart CLI behavior and help output.
src/windows/wslcsession/WSLCContainer.h Adds restart APIs on WSLCContainerImpl and COM WSLCContainer.
src/windows/wslcsession/WSLCContainer.cpp Implements restart logic and exposes it via COM with warning callback context.
src/windows/wslc/tasks/ContainerTasks.h Declares RestartContainers task.
src/windows/wslc/tasks/ContainerTasks.cpp Implements CLI task parsing/loop for restarting one or more containers.
src/windows/wslc/services/ContainerService.h Adds ContainerService::Restart declaration.
src/windows/wslc/services/ContainerService.cpp Implements Restart by opening the container and calling COM Restart.
src/windows/wslc/commands/ContainerRestartCommand.cpp New command implementation for argument wiring + execution pipeline.
src/windows/wslc/commands/ContainerCommand.h Declares the ContainerRestartCommand type.
src/windows/wslc/commands/ContainerCommand.cpp Registers restart as a container subcommand.
src/windows/service/inc/wslc.idl Extends IWSLCContainer with the Restart method.
localization/strings/en-US/Resources.resw Adds localized strings for restart command descriptions and --rm refusal message.

Comment thread localization/strings/en-US/Resources.resw Outdated
Comment thread test/windows/WSLCTests.cpp

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 14 out of 14 changed files in this pull request and generated no new comments.

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 14 out of 14 changed files in this pull request and generated 2 comments.

Comment thread src/windows/wslc/commands/ContainerRestartCommand.cpp
Comment thread test/windows/wslc/e2e/WSLCE2EContainerRestartTests.cpp
@beena352 beena352 marked this pull request as ready for review July 6, 2026 18:57
@beena352 beena352 requested a review from a team as a code owner July 6, 2026 18:57

@OneBlue OneBlue left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Unfortunately I'm not sure that Restart() can be easily implemented with our existing locking story.

We have a work item to rethink the container state machine & locking logic to allow concurrent Stop() & Kill() calls, I think we're unfortunately going to have for that change before we can implement restart

}

// Refuse --rm before stopping: Stop -> OnStopped would delete the container.
THROW_HR_WITH_USER_ERROR_IF(

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I don't think this is the behavior we want: Docker supports restarting containers when --rm is set.

snapshotFlags = m_containerFlags;
}

// Refuse --rm before stopping: Stop -> OnStopped would delete the container.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Locking wise, I don't think that we can drop the lock here unfortunately, otherwise another stop / start operation could arrive before we complete, which would leak to weird states

@beena352

beena352 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor Author

Unfortunately I'm not sure that Restart() can be easily implemented with our existing locking story.

We have a work item to rethink the container state machine & locking logic to allow concurrent Stop() & Kill() calls, I think we're unfortunately going to have for that change before we can implement restart

Marking this PR as Draft and blocked by Deliverable 62987921 (“[WSL] [Containers] Rethink the WSLCContainer state machine”).
I’ll resume/rework restart once that state-machine/locking change lands, including Docker-parity  --rm  behavior.

@beena352 beena352 marked this pull request as draft July 6, 2026 22:08
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