Skip to content

Various improvements to interactions with user-provided handles#14486

Draft
OneBlue wants to merge 2 commits intofeature/wsl-for-appsfrom
user/oneblue/cancel-io
Draft

Various improvements to interactions with user-provided handles#14486
OneBlue wants to merge 2 commits intofeature/wsl-for-appsfrom
user/oneblue/cancel-io

Conversation

@OneBlue
Copy link
Collaborator

@OneBlue OneBlue commented Mar 20, 2026

Summary of the Pull Request

This change brings improvements to the way the service deals with user handles.

  1. Keep a list of "in progress" user handles, so we can cancel any stuck IO when the session terminates
  2. Reduce the permissions of duplicated handles to and from user processes

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

Copilot AI review requested due to automatic review settings March 20, 2026 01:15
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 improves WSLA service handling of user-provided handles by (1) attempting to unblock session termination when operations are using user handles, and (2) reducing access rights when duplicating handles between the service and client processes.

Changes:

  • Add UserHandle RAII + session tracking of in-flight user handles, and invoke cancellation during WSLASession::Terminate().
  • Reduce access rights on duplicated handles (both from caller → service and service → caller) to least-needed flags.
  • Add a WSLA test intended to validate cancellation/unblocking behavior for synchronous I/O scenarios.

Reviewed changes

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

Show a summary per file
File Description
test/windows/WSLATests.cpp Adds a new test covering session termination while a user-handle I/O operation is blocked.
src/windows/wslasession/WSLASession.h Introduces UserHandle and per-session tracking of user handles undergoing I/O.
src/windows/wslasession/WSLASession.cpp Implements UserHandle, OpenUserHandle/ReleaseUserHandle, and termination-time cancellation logic; updates call sites to use reduced-access user handle duplication.
src/windows/wslasession/WSLAContainer.cpp Reduces access when duplicating stdin/stdout/stderr/tty handles back to the client; uses OpenUserHandle for export output.
src/windows/common/wslutil.h Extends DuplicateHandleFromCallingProcess to accept an optional desired access mask.
src/windows/common/wslutil.cpp Implements the desired-access behavior for DuplicateHandleFromCallingProcess.

Comment on lines +111 to +119
UserHandle& UserHandle::operator=(UserHandle&& Other)
{
if (this != &Other)
{
m_session = Other.m_session;
m_handle = std::move(Other.m_handle);

Other.m_session = nullptr;
}
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

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

UserHandle move-assignment overwrites an existing m_handle without first removing the old handle from the session’s tracking list. If a UserHandle is ever reassigned while already holding a handle (e.g., via std::optional reassignment), the old HANDLE will be closed by wil::unique_handle but will remain in m_userHandles, leading to stale entries and potentially cancelling/acting on an unrelated handle value later. Update operator= to release/unregister any currently-held handle before taking ownership, or delete move-assignment if it’s not intended to be used.

Copilot uses AI. Check for mistakes.
Comment on lines +1520 to +1531
void WSLASession::CancelUserHandleIO()
{
std::lock_guard lock(m_userHandlesLock);
for (auto handle : m_userHandles)
{
// Cancell all IO on the handle.
// N.B. This only cancels IO happening in this process.
if (!CancelIoEx(handle, nullptr))
{
LOG_LAST_ERROR_IF(GetLastError() != ERROR_NOT_FOUND);
}
}
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

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

CancelUserHandleIO calls CancelIoEx(handle, nullptr), which only cancels I/O issued by the calling thread. In the scenario this PR targets (a worker thread blocked in synchronous ReadFile/WriteFile on a non-overlapped handle), calling CancelIoEx from the termination thread will not unblock that stuck thread. Consider tracking the issuing thread(s) and using CancelSynchronousIo on those thread handles, or restructuring the relay to avoid synchronous blocking I/O on non-overlapped user handles.

Copilot uses AI. Check for mistakes.
std::lock_guard lock(m_userHandlesLock);
for (auto handle : m_userHandles)
{
// Cancell all IO on the handle.
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

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

Typo in comment: "Cancell" → "Cancel".

Suggested change
// Cancell all IO on the handle.
// Cancel all IO on the handle.

Copilot uses AI. Check for mistakes.
Comment on lines +1745 to +1747
// Create a blocked operation that will cause the service to get stuck on WriteFile() call.
// Because the pipe handle that we're passing in doesn't support overlapped IO, the service will get stuck in a
// synchronous WriteFile() call. Validate that terminating the session correctly cancels the IO.
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

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

The comment describes the service getting stuck in a synchronous WriteFile(), but the rest of the test (and the note below) discusses being stuck in ReadFile() on the pipe. This is misleading when debugging failures—please update the comment to reflect the actual blocking call being exercised.

Suggested change
// Create a blocked operation that will cause the service to get stuck on WriteFile() call.
// Because the pipe handle that we're passing in doesn't support overlapped IO, the service will get stuck in a
// synchronous WriteFile() call. Validate that terminating the session correctly cancels the IO.
// Create a blocked operation that will cause the service to get stuck on a ReadFile() call on the pipe.
// Because the pipe handle that we're passing in doesn't support overlapped IO, the service will get stuck in a
// synchronous ReadFile() call. Validate that terminating the session correctly cancels the IO.

Copilot uses AI. Check for mistakes.
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