Skip to content

Allow retrieving state and init process info after container auto remove#14429

Open
kvega005 wants to merge 21 commits intomicrosoft:feature/wsl-for-appsfrom
kvega005:user/kevinve/autoRemoveReadStdOut
Open

Allow retrieving state and init process info after container auto remove#14429
kvega005 wants to merge 21 commits intomicrosoft:feature/wsl-for-appsfrom
kvega005:user/kevinve/autoRemoveReadStdOut

Conversation

@kvega005
Copy link

@kvega005 kvega005 commented Mar 13, 2026

Summary of the Pull Request

Allow clients to continue reading stdout/stderr and querying state from containers after they've been deleted, by caching the read-only data in the com wrapper.

Changes

  1. COM wrapper caches read-only properties
  • WSLAContainerImpl calls CacheState and passes values to com wrapper before disconnecting.
  • GetState(), GetInitProcess(), GetId(), and GetName() check the cached values first, so they continue to work after the WSLAContainerImpl is disconnected.
  1. Deleted containers purged from session (WSLASession.cpp)
  • OpenContainer and ListContainers purge deleted containers via std::erase_if so they are no longer visible to lookups, while existing COM references remain usable via cached data.
    3 .Transition before ReleaseResources
  • DeleteExclusiveLockHeld now calls Transition(Deleted) before ReleaseResources() so that OnDisconnectLockHeld caches the final Deleted state.

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

Validation Steps Performed

Updated tests (WSLATests.cpp)

  • Existing auto-remove tests now assert container.State() == WslaContainerStateDeleted instead of expecting RPC_E_DISCONNECTED.
  • New test ContainerAutoRemoveReadStdout: Verifies that after an auto-removed container exits, the client can still read State(), get the init process, and capture its stdout ("Hello World\n"), and that the container is properly hidden from OpenContainer/ListContainers.

Copy link
Contributor

Copilot AI left a comment

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 changes WSLA container lifetime/visibility semantics so clients can keep querying container state/process output even after an --rm container is auto-deleted, by deferring when COM objects get disconnected/removed from the session’s container list.

Changes:

  • Adjust container deletion/disconnection behavior in WSLAContainerImpl / WSLAContainer to keep COM calls working after auto-remove.
  • Stop eagerly purging deleted containers from WSLASession and instead filter them out during open/list operations.
  • Update/add Windows tests to validate post-auto-remove state/process/stdout behavior.

Reviewed changes

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

File Description
test/windows/WSLATests.cpp Updates auto-remove expectations and adds a new test that reads stdout/state after auto-delete.
src/windows/wslasession/WSLASession.cpp Replaces “purge deleted containers” with “filter deleted containers” in Open/List.
src/windows/wslasession/WSLAContainer.h Adds ~WSLAContainer() declaration.
src/windows/wslasession/WSLAContainer.cpp Moves COM wrapper disconnect/reset to impl destructor; adds COM wrapper destructor callback; makes delete idempotent.
Comments suppressed due to low confidence (1)

src/windows/wslasession/WSLAContainer.cpp:1308

  • ReleaseResources() no longer drops the implementation’s strong reference to the COM wrapper (m_comWrapper), which means the wrapper’s refcount can’t reach 0 while the impl remains in m_containers. As a result, the new WSLAContainer destructor-based cleanup (m_onDeleted) won’t run for auto-removed containers, and deleted containers can accumulate in-session until Terminate() clears m_containers. If the intent is to erase the impl when the last external COM reference is released, the impl needs to stop owning the COM wrapper at/after deletion (e.g., Reset without Disconnect for auto-remove), while still ensuring wrappers get disconnected safely on session termination.
__requires_exclusive_lock_held(m_lock) void WSLAContainerImpl::ReleaseResources()
{
    WSL_LOG("ReleaseContainerResources", TraceLoggingValue(m_id.c_str(), "ID"));

    // Unmount volumes.
    UnmountVolumes(m_mountedVolumes, m_virtualMachine);
    m_mountedVolumes.clear();

    // Unmap and release ports.

Copilot AI review requested due to automatic review settings March 19, 2026 17:32
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Enables clients to keep querying state/init process and reading stdout/stderr for auto-removed containers by deferring COM wrapper disconnection and caching key read-only properties on disconnect.

Changes:

  • Added disconnect-time caching in WSLAContainer and a new COMImplClass hook (OnDisconnectLockHeld) to preserve access after disconnect.
  • Adjusted container delete behavior sequencing and updated/added tests for auto-remove + post-delete access patterns.
  • Updated tests to validate deleted-state behavior and container hiding from session lookups/lists.

Reviewed changes

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

File Description
test/windows/WSLATests.cpp Updates existing auto-remove assertions and adds a new test for reading stdout/init process after auto-remove.
src/windows/wslasession/WSLAContainer.h Adds disconnect hook override, caching fields, and friend access for caching implementation details.
src/windows/wslasession/WSLAContainer.cpp Implements cached getters and disconnect-time caching of id/name/state/init-process.
src/windows/common/COMImplClass.h Adds OnDisconnectLockHeld virtual hook invoked during Disconnect() under lock.

Copilot AI review requested due to automatic review settings March 20, 2026 17:54
Copy link
Contributor

Copilot AI left a comment

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 enables clients to continue querying read-only container metadata (state/id/name/init process) and reading stdout/stderr after a container has been auto-removed, by caching the needed data in the COM wrapper prior to disconnecting the implementation.

Changes:

  • Cache container read-only properties into WSLAContainer before WSLAContainerImpl disconnects, and serve GetState/GetInitProcess/GetId/GetName from cached values post-disconnect.
  • Adjust deletion flow to transition to Deleted before resource release, so the cached state reflects the final lifecycle state.
  • Update/add tests to validate post-auto-remove state and stdout capture, and ensure deleted containers are hidden from session lookups/listing.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
test/windows/WSLATests.cpp Updates existing auto-remove expectations and adds a new test verifying state/init process/stdout are readable after auto-remove and container is hidden from session queries.
src/windows/wslasession/WSLAContainer.h Adds cached fields and a CacheState helper for snapshotting read-only properties pre-disconnect.
src/windows/wslasession/WSLAContainer.cpp Implements caching + cached getters, adjusts deletion transition ordering, and snapshots cached data during COM wrapper disconnect.
Comments suppressed due to low confidence (1)

src/windows/wslasession/WSLAContainer.cpp:1406

  • DisconnectComWrapper() is annotated as requiring m_lock exclusively, and it now additionally acquires m_processesLock. If any other code path acquires these locks in the opposite order (m_processesLock then m_lock), this introduces a concrete deadlock risk. To fix, enforce a single lock-ordering contract (document and follow it everywhere), or avoid taking m_processesLock here by making m_initProcess access safe under m_lock (e.g., move ownership/reads under m_lock), or snapshot the required process pointer in a location that already holds locks in the established order.
{
    if (m_comWrapper)
    {
        // Cache read-only properties in the COM wrapper before disconnecting,
        // so callers can still query state/process after the impl is gone.
        {
            std::lock_guard processesLock{m_processesLock};
            m_comWrapper->CacheState(m_id, m_name, m_state, m_initProcess);
        }

        m_comWrapper->Disconnect();
        m_comWrapper.Reset();
    }
}

Copilot AI review requested due to automatic review settings March 20, 2026 18:50
Copy link
Contributor

Copilot AI left a comment

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

Copilot AI review requested due to automatic review settings March 23, 2026 19:05
Copy link
Contributor

Copilot AI left a comment

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

@kvega005 kvega005 marked this pull request as ready for review March 25, 2026 15:56
@kvega005 kvega005 requested a review from a team as a code owner March 25, 2026 15:56
Copilot AI review requested due to automatic review settings March 25, 2026 15:56
Copy link
Contributor

Copilot AI left a comment

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

Copilot AI review requested due to automatic review settings March 25, 2026 18:26
Copy link
Contributor

Copilot AI left a comment

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

Comments suppressed due to low confidence (1)

src/windows/wslasession/WSLAContainer.cpp:1

  • WSLAContainer::GetInitProcess dereferences Process without a null check (*Process = nullptr). This can AV if a caller passes nullptr. Add RETURN_HR_IF_NULL(E_POINTER, Process); before writing through the pointer (similar to GetState / GetName).
/*++

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.

2 participants