Port-forwarding with netsh interface portproxy
is somehow blocking the ports that processes on WSL2 need to use.
I ran a PowerShell script from this blog in order to do port-forwarding between WSL2 and Windows 11.
However, when I start some applications that are supposed to bind the ports, it shows "address already in use" errors.
Error from docker-compose service (MongoDB):
ERROR: for service_name_1 Cannot start service mongo: driver failed programming external connectivity on endpoint service_name_1: Error starting userland proxy: listen tcp4 0.0.0.0:27017: bind: address already in use
Error from FastAPI:
[Errno 98] Address already in use
Strange thing is that these errors occur when there are no WSL processes up on those ports. Running these commands in WSL give me nothing:
lsof -i :27017
sudo netstat -nlp | grep 27017
However, there is one Windows process (svchost) that is using the port:
PS > Get-NetTCPConnection -LocalPort 27017
LocalAddress LocalPort RemoteAddress RemotePort State AppliedSetting OwningProces
s
------------ --------- ------------- ---------- ----- -------------- ------------
0.0.0.0 27017 0.0.0.0 0 Listen 12732
PS > Get-Process -Id 12732
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
------- ------ ----- ----- ------ -- -- -----------
401 45 2824 11268 0.05 12732 0 svchost
When I kill this process, the "address already in use" errors do not appear anymore, but port-forwarding doesn't work propertly still. They were working just fine until today, and I can't figure out what could have possibly gone wrong.
Thanks for your help!
I was running into this same problem from the latest WSL release!
What worked for me is upgrading to a preview version of WSL that fixes this issue. https://github.com/microsoft/WSL/releases/tag/1.2.1
After installing the preview msixbundle, worked for me :)