Fix logic error in poll

This commit is contained in:
Blue 2025-12-04 13:00:04 -08:00
parent 5091458e0f
commit c3b561c35c
3 changed files with 20 additions and 11 deletions

View File

@ -21,10 +21,11 @@ using wsl::windows::service::wsla::WSLAContainer;
constexpr const char* nerdctlPath = "/usr/bin/nerdctl";
// Constants for required default arguments for "nerdctl run..."
static std::vector<std::string> defaultNerdctlRunArgs{//"--pull=never", // TODO: Uncomment once PullImage() is implemented.
"--net=host", // TODO: default for now, change later
"--ulimit",
"nofile=65536:65536"};
static std::vector<std::string> defaultNerdctlRunArgs{
//"--pull=never", // TODO: Uncomment once PullImage() is implemented.
"--net=host", // TODO: default for now, change later
"--ulimit",
"nofile=65536:65536"};
WSLAContainer::WSLAContainer(WSLAVirtualMachine* parentVM, ServiceRunningProcess&& containerProcess, const char* name, const char* image) :
m_parentVM(parentVM), m_containerProcess(std::move(containerProcess)), m_name(name), m_image(image)
@ -33,11 +34,17 @@ WSLAContainer::WSLAContainer(WSLAVirtualMachine* parentVM, ServiceRunningProcess
// TODO: Find a better way to wait for the container to be fully started.
auto status = GetNerdctlStatus();
while (status != "running" && m_containerProcess.State() == WslaContainerStateRunning)
while (status != "running")
{
if (status == "exited" || m_containerProcess.State() != WslaProcessStateRunning)
{
m_state = WslaContainerStateExited;
return;
}
// TODO: empty string is returned while the container image is still downloading.
// Remove this logic once the image pull is separated from container creation.
if (status != "created" && status != "")
if (status.has_value() && status != "created")
{
THROW_HR_MSG(
E_UNEXPECTED, "Unexpected nerdctl status '%hs', for container '%hs'", status.value_or("<empty>").c_str(), m_name.c_str());
@ -235,12 +242,14 @@ std::optional<std::string> WSLAContainer::GetNerdctlStatus()
// TODO: Find a way to validate that the container is indeed not found, and not some other error.
return {};
}
auto& status = result.Output[0];
auto& status = result.Output[1];
while (!status.empty() && status.back() == '\n')
{
status.pop_back();
}
return status;
// N.B. nerdctl inspect can return with exit code 0 and no output. Return an empty optional if that happens.
return status.empty() ? std::optional<std::string>{} : status;
}

View File

@ -226,7 +226,7 @@ try
auto it = m_containers.find(containerOptions->Name);
RETURN_HR_IF(HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS), it != m_containers.end());
// Validate that name & images are within legnth limits.
// Validate that name & images are within length limits.
RETURN_HR_IF(E_INVALIDARG, strlen(containerOptions->Name) > WSLA_MAX_CONTAINER_NAME_LENGTH);
RETURN_HR_IF(E_INVALIDARG, strlen(containerOptions->Image) > WSLA_MAX_IMAGE_NAME_LENGTH);
@ -248,7 +248,7 @@ try
auto it = m_containers.find(Name);
RETURN_HR_IF_MSG(HRESULT_FROM_WIN32(ERROR_NOT_FOUND), it == m_containers.end(), "Container not found: '%hs'", Name);
it->second.CopyTo(__uuidof(IWSLAContainer), (void**)Container);
THROW_IF_FAILED(it->second.CopyTo(__uuidof(IWSLAContainer), (void**)Container));
return S_OK;
}
CATCH_RETURN();

View File

@ -1247,7 +1247,7 @@ class WSLATests
auto container = launcher.Launch(*session);
VERIFY_ARE_EQUAL(container.State(), WslaContainerStateRunning);
// Validate that a container with the same name can be started
// Validate that a container with the same name cannot be started
VERIFY_ARE_EQUAL(
WSLAContainerLauncher("debian:latest", "test-unique-name", "echo", {"OK"}).LaunchNoThrow(*session).first,
HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS));