-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Fix port conflict error message and cleanup on failure #40102
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feature/wsl-for-apps
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -1274,12 +1274,10 @@ std::unique_ptr<WSLCContainerImpl> WSLCContainerImpl::Open( | |||||
| { | ||||||
| auto allocation = virtualMachine.TryAllocatePort(e.VmPort, e.Family, e.Protocol); | ||||||
|
|
||||||
| THROW_HR_IF_MSG( | ||||||
| THROW_HR_WITH_USER_ERROR_IF( | ||||||
| HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS), | ||||||
| !allocation, | ||||||
| "Port %hu is in use, cannot open container %hs", | ||||||
| e.VmPort, | ||||||
| dockerContainer.Id.c_str()); | ||||||
| std::format(L"Port {} is already in use, cannot open container {}", e.VmPort, dockerContainer.Id), | ||||||
| !allocation); | ||||||
|
Comment on lines
+1277
to
+1280
|
||||||
|
|
||||||
| inserted.VmMapping.AssignVmPort(allocation); | ||||||
| } | ||||||
|
|
@@ -1443,20 +1441,31 @@ void WSLCContainerImpl::MapPorts() | |||||
| auto allocatedPort = | ||||||
| m_virtualMachine.TryAllocatePort(e.ContainerPort, e.VmMapping.BindAddress.si_family, e.VmMapping.Protocol); | ||||||
|
|
||||||
| THROW_HR_IF_MSG( | ||||||
| THROW_HR_WITH_USER_ERROR_IF( | ||||||
| HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS), | ||||||
| !allocatedPort, | ||||||
| "Port %hu is in use, cannot start container %hs", | ||||||
| e.ContainerPort, | ||||||
| m_id.c_str()); | ||||||
| std::format(L"Port {} is already in use, cannot start container {}", e.ContainerPort, m_id), | ||||||
| !allocatedPort); | ||||||
|
Comment on lines
+1444
to
+1447
|
||||||
|
|
||||||
| e.VmMapping.AssignVmPort(allocatedPort); | ||||||
|
|
||||||
| allocatedPorts.emplace(e.ContainerPort, allocatedPort); | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| m_virtualMachine.MapPort(e.VmMapping); | ||||||
| try | ||||||
| { | ||||||
| m_virtualMachine.MapPort(e.VmMapping); | ||||||
| } | ||||||
| catch (const wil::ResultException& ex) | ||||||
| { | ||||||
| if (ex.GetErrorCode() == HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS)) | ||||||
| { | ||||||
| THROW_HR_WITH_USER_ERROR( | ||||||
| HRESULT_FROM_WIN32(ERROR_ALREADY_EXISTS), | ||||||
| std::format(L"Port {} is already in use, cannot start container {}", e.ContainerPort, m_id)); | ||||||
|
||||||
| std::format(L"Port {} is already in use, cannot start container {}", e.ContainerPort, m_id)); | |
| std::format(L"Port {} is already in use, cannot start container {}", e.VmMapping.HostPort(), m_id)); |
Copilot
AI
Apr 4, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MapPort() failures caused by the OS bind/listen failing will come back as HRESULT_FROM_WIN32(WSAEADDRINUSE) (and possibly WSAEACCES), not ERROR_ALREADY_EXISTS (which is used for duplicate mappings inside wslrelay). As written, the new user error message won’t trigger for the common “port already bound by another process” scenario. Handle WSAEADDRINUSE (and optionally WSAEACCES) here and surface the port-conflict user error for those HRESULTs too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change alters observable behavior (container should be deleted when Start() fails). The repo has WSLC end-to-end tests for
wslc container run(e.g. test/windows/wslc/e2e/WSLCE2EContainerRunTests.cpp), but this scenario isn’t covered. Add an e2e test that holds a host port open, runswslc container run -p <port>:..., asserts the expected port-in-use user message, and verifies the container is not left behind.