@@ -96,6 +96,28 @@ class HcsVirtualMachineBackendTests
9696{
9797 WSL_TEST_CLASS (HcsVirtualMachineBackendTests)
9898
99+ TEST_CLASS_SETUP (TestClassSetup)
100+ {
101+ WSADATA data{};
102+ THROW_IF_WIN32_ERROR (WSAStartup (MAKEWORD (2 , 2 ), &data));
103+ return true ;
104+ }
105+
106+ TEST_CLASS_CLEANUP (TestClassCleanup)
107+ {
108+ const auto module = GetModuleHandleW (L" wsldevicehostproxystub.dll" );
109+ if (module )
110+ {
111+ const auto canUnload = reinterpret_cast <HRESULT (STDAPICALLTYPE *)()>(GetProcAddress (module , " DllCanUnloadNow" ));
112+ VERIFY_IS_NOT_NULL (canUnload);
113+ LogInfo (" Device-host proxy unload status before COM cleanup: 0x%08x" , canUnload ());
114+ }
115+ CoFreeUnusedLibrariesEx (0 , 0 );
116+ VERIFY_IS_NULL (GetModuleHandleW (L" wsldevicehostproxystub.dll" ));
117+ VERIFY_ARE_EQUAL (0 , WSACleanup ());
118+ return true ;
119+ }
120+
99121 TEST_METHOD (FileSystemRequestsDefaultToVirtioFs)
100122 {
101123 const VmFileSystemDeviceRequest device;
@@ -260,7 +282,7 @@ class HcsVirtualMachineBackendTests
260282 CreateVhd (invalidPath);
261283
262284 auto backend = HcsVirtualMachineBackend::Create (CreateRunnableRequest ());
263- backend-> Start ( );
285+ auto guest = StartGuest (*backend );
264286 const auto automatic = backend->AttachDisk (CreateDiskRequest (automaticPath));
265287 const auto exact = backend->AttachDisk (CreateDiskRequest (exactPath, 2 ));
266288 const auto replacement = backend->AttachDisk (CreateDiskRequest (replacementPath));
@@ -311,7 +333,7 @@ class HcsVirtualMachineBackendTests
311333 CreateVhd (secondPath);
312334
313335 auto firstBackend = HcsVirtualMachineBackend::Create (CreateRunnableRequest ());
314- firstBackend-> Start ( );
336+ auto firstGuest = StartGuest (*firstBackend );
315337 const auto first = firstBackend->AttachDisk (CreateDiskRequest (firstPath, 253 ));
316338
317339 VERIFY_ARE_EQUAL (UINT32 {253 }, first.GuestAddress .Lun );
@@ -322,7 +344,7 @@ class HcsVirtualMachineBackendTests
322344
323345 // Disk IDs are local to a VM and can be reused after another VM has terminated.
324346 auto secondBackend = HcsVirtualMachineBackend::Create (CreateRunnableRequest ());
325- secondBackend-> Start ( );
347+ auto secondGuest = StartGuest (*secondBackend );
326348 const auto second = secondBackend->AttachDisk (CreateDiskRequest (secondPath));
327349
328350 VERIFY_ARE_EQUAL (first.Id .Value , second.Id .Value );
@@ -354,7 +376,7 @@ class HcsVirtualMachineBackendTests
354376 {
355377 SKIP_TEST_ARM64 ();
356378 auto backend = HcsVirtualMachineBackend::Create (CreateRunnableRequest ());
357- backend-> Start ( );
379+ auto guest = StartGuest (*backend );
358380
359381 const auto request = CreateNetworkRequest ();
360382 const auto first = backend->AddNetworkAdapter (request);
@@ -372,6 +394,31 @@ class HcsVirtualMachineBackendTests
372394 backend->Terminate ();
373395 }
374396
397+ TEST_METHOD (KeepsOtherVmDeviceHostsAlive)
398+ {
399+ SKIP_TEST_ARM64 ();
400+ const auto directory = CreateTestDirectory ();
401+ auto cleanup = wil::scope_exit_log (WI_DIAGNOSTICS_INFO , [&] { std::filesystem::remove_all (directory); });
402+
403+ auto first = HcsVirtualMachineBackend::Create (CreateRunnableRequest ());
404+ auto firstGuest = StartGuest (*first);
405+ first->CreateFileSystemDevice ({VmVirtioFsDevice{L" first" , VmVirtioFsLayout::Aggregate}});
406+
407+ auto second = HcsVirtualMachineBackend::Create (CreateRunnableRequest ());
408+ auto secondGuest = StartGuest (*second);
409+ const auto device = second->CreateFileSystemDevice ({VmVirtioFsDevice{L" second" , VmVirtioFsLayout::Aggregate}});
410+
411+ first.reset ();
412+
413+ VmFileSystemShareRequest request;
414+ request.HostPath = directory;
415+ request.Options = VmVirtioFsShareOptions{};
416+ const auto share = second->AddFileSystemShare (device.Id , request);
417+ VERIFY_ARE_EQUAL (device.Id .Value , share.Device .Value );
418+ second->RemoveFileSystemShare (share.Id );
419+ second->Terminate ();
420+ }
421+
375422 TEST_METHOD (NotifiesTerminationCallbacksWithoutHoldingBackendLock)
376423 {
377424 SKIP_TEST_ARM64 ();
@@ -447,7 +494,7 @@ class HcsVirtualMachineBackendTests
447494 auto cleanup = wil::scope_exit_log (WI_DIAGNOSTICS_INFO , [&] { std::filesystem::remove_all (directory); });
448495
449496 auto backend = HcsVirtualMachineBackend::Create (CreateRunnableRequest ());
450- backend-> Start ( );
497+ auto guest = StartGuest (*backend );
451498
452499 // Elevated and unelevated callers share one VM, so each elevation level gets its own device
453500 // just as WslCoreVm::AddDrvFsShare uses a separate Plan 9 port and virtio-fs tag for each.
0 commit comments