Add test to repeatedly use session storage (#40894)

JohnMcPMS committed Jun 25, 2026 at 09:39 UTC c4b7922a08a296e5b7670d5f1123cb0ef206d3ed
1 file changed +52
test/windows/WslcSdkTests.cpp
+52
@@ -2756,6 +2756,58 @@ class WslcSdkTests
2756 }
2757 }
2758
2759 + WSLC_TEST_METHOD(ResourceReuseAfterCleanup)
2760 + {
2761 + // Each iteration exercises the complete lifecycle: create session → load image → run container
2762 + // → terminate session → release session. Running 10 times on the same storage path verifies
2763 + // that all resources (VHD locks, image store, container overlays, process handles, VM) are
2764 + // fully freed by the time WslcReleaseSession returns, so the next iteration can reuse them
2765 + // without errors.
2766 + std::filesystem::path sessionStorage = m_storagePath / "wslc-resource-reuse-storage";
2767 +
2768 + auto cleanupStorage = wil::scope_exit_log(WI_DIAGNOSTICS_INFO, [&]() {
2769 + std::error_code ec;
2770 + std::filesystem::remove_all(sessionStorage, ec);
2771 + });
2772 +
2773 + WslcSessionSettings sessionSettings;
2774 + VERIFY_SUCCEEDED(WslcInitSessionSettings(L"wslc-resource-reuse", sessionStorage.c_str(), &sessionSettings));
2775 + VERIFY_SUCCEEDED(WslcSetSessionSettingsCpuCount(&sessionSettings, 2));
2776 + VERIFY_SUCCEEDED(WslcSetSessionSettingsMemory(&sessionSettings, 1024));
2777 + VERIFY_SUCCEEDED(WslcSetSessionSettingsTimeout(&sessionSettings, 30 * 1000));
2778 +
2779 + WslcVhdRequirements vhdReqs{};
2780 + vhdReqs.sizeBytes = 2048ull * 1024 * 1024; // 2 GB
2781 + vhdReqs.type = WSLC_VHD_TYPE_DYNAMIC;
2782 + VERIFY_SUCCEEDED(WslcSetSessionSettingsVhd(&sessionSettings, &vhdReqs));
2783 +
2784 + constexpr auto c_imageName = "hello-world:latest";
2785 + constexpr int c_iterationCount = 10;
2786 +
2787 + for (int i = 0; i < c_iterationCount; ++i)
2788 + {
2789 + LogInfo("ResourceReuseAfterCleanup: iteration %d / %d", i + 1, c_iterationCount);
2790 +
2791 + // Create the session, reusing the same storage path every iteration.
2792 + UniqueSession session;
2793 + VERIFY_SUCCEEDED(WslcCreateSession(&sessionSettings, &session, nullptr));
2794 + VERIFY_IS_NOT_NULL(session.get());
2795 +
2796 + // Load the test image into the session.
2797 + VERIFY_SUCCEEDED(WslcLoadSessionImageFromFile(session.get(), GetTestImagePath(c_imageName).c_str(), nullptr, nullptr));
2798 +
2799 + // Run a container using the image's default entrypoint and capture its output.
2800 + auto output = RunContainerAndCapture(session.get(), c_imageName, {});
2801 + VERIFY_IS_TRUE(output.stdoutOutput.find("Hello from Docker!") != std::string::npos);
2802 +
2803 + // Terminate the session, then release it. WslcReleaseSession must return only after all
2804 + // resources are freed so that the next iteration can reopen the same storage without error.
2805 + VERIFY_SUCCEEDED(WslcTerminateSession(session.get()));
2806 + VERIFY_SUCCEEDED(WslcReleaseSession(session.get()));
2807 + session.release(); // explicit calls above
2808 + }
2809 + }
2810 +
2811 WSLC_TEST_METHOD(StopContainerTimeout)
2812 {
2813 WslcProcessSettings procSettings;