Skip to content

Commit 49ba11e

Browse files
committed
Fix leak for wsldevicehostproxy
1 parent eaaa885 commit 49ba11e

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

‎src/windows/common/HcsVirtualMachineBackend.cpp‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -377,12 +377,13 @@ HcsVirtualMachineBackend::~HcsVirtualMachineBackend() noexcept
377377
CloseGuestListenersLocked(m_configuration.Description.Identity);
378378
}
379379

380-
if (system)
380+
// Device hosts must be shut down while the compute system and callback context still exist.
381+
guestDeviceManager.reset();
382+
if (system && !m_exitEvent.is_signaled())
381383
{
382384
LOG_IF_FAILED(wil::ResultFromException([&] { schema::TerminateComputeSystem(system.get()); }));
383385
}
384386
system.reset();
385-
guestDeviceManager.reset();
386387
CleanupAttachedDisks(std::move(attachedDisks));
387388

388389
try
@@ -612,9 +613,10 @@ void HcsVirtualMachineBackend::Terminate()
612613
CloseGuestListenersLocked(m_configuration.Description.Identity);
613614
}
614615

616+
guestDeviceManager.reset();
617+
615618
auto cleanup = wil::scope_exit([&] {
616619
system.reset();
617-
guestDeviceManager.reset();
618620
CleanupAttachedDisks(std::move(attachedDisks));
619621
m_terminatingEvent.SetEvent();
620622
NotifyTerminated(m_configuration.Description.Identity);

‎src/windows/common/hcs.cpp‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,19 @@ void wsl::windows::common::hcs::TerminateComputeSystem(_In_ HCS_SYSTEM ComputeSy
531531
ExecutionContext context(Context::HCS);
532532

533533
const unique_hcs_operation operation = CreateOperation();
534-
THROW_IF_FAILED(::HcsTerminateComputeSystem(ComputeSystem, operation.get(), nullptr));
534+
auto result = ::HcsTerminateComputeSystem(ComputeSystem, operation.get(), nullptr);
535+
if (result == HCS_E_SYSTEM_ALREADY_STOPPED)
536+
{
537+
return;
538+
}
539+
THROW_IF_FAILED(result);
535540

536541
wil::unique_cotaskmem_string resultDocument;
537-
const auto result = ::HcsWaitForOperationResult(operation.get(), INFINITE, &resultDocument);
542+
result = ::HcsWaitForOperationResult(operation.get(), INFINITE, &resultDocument);
543+
if (result == HCS_E_SYSTEM_ALREADY_STOPPED)
544+
{
545+
return;
546+
}
538547
THROW_IF_FAILED_MSG(result, "HcsTerminateComputeSystem failed (error string: %ls)", resultDocument.get());
539548
}
540549

0 commit comments

Comments
 (0)