microsoft / microsoft/WSL

Socket Connect hangs until timeout runs out in mirrored mode.

Open
#10,855 55 comments 18 reactions 1 assignee View on GitHub

@CatalinFetoiu is already working on this.

Since Apr 9, 2025.

network
Dominant language
C++
Stars
33.7k
Forks
1.8k
Avg merge
3d 17h
Merged PRs (30d)
116

Description

Windows Version

Microsoft Windows [Version 10.0.22631.2715]

WSL Version

2.0.9.0 and 2.0.14 tested

Are you using WSL 1 or WSL 2?
  • WSL 2
  • WSL 1
Kernel Version

5.15.133.1-1

Distro Version

Ubuntu 22.04

Other Software

No response

Repro Steps

Call this script in mirrored mode as normal user or sudo (try both):

import socket
import time
import os

def is_port_in_use(port):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        # s.settimeout(2)  # set timeout directly

        print(f"Socket type: {s.type}")
        print(f"Socket family: {s.family}")
        print(f"Socket timeout: {s.gettimeout()} seconds")
        # Get and print socket options
        print(f"SO_REUSEADDR option: {s.getsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR)}")
        print(f"SO_KEEPALIVE option: {s.getsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE)}")
        print(f"TCP_NODELAY option: {s.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY)}")
        print("checking: ", port)
        print("Timeout: ",s.gettimeout())
        return s.connect_ex(("localhost", port)) == 0
    
    
print("Uid: ", os.getuid())
start_time = time.time()
print("Is Port in use:", is_port_in_use(6006))
print("--- Runtime: %s seconds ---" % (time.time() - start_time))
Expected Behavior

It directly checks if the socket is in use or not.

Actual Behavior

Sometimes it takes over 132 seconds to return True or False. In not mirrored mode it always directly returns. I came across this issue using ros, where the roscore takes about 2 minutes to start, or starting tensorboard takes around 2 minutes aswell, so i build this script to reproduce the error.

If it directly returns true or false in mirrored mode, i start the script as sudo and then the problem occurs again.
What is interesting, is that if i set the socket timeout to a specific second, then it will return after that. Its like its blocked for the default timeout.

Diagnostic Logs

Without networking mode mirrored:

❯ python3 test_connect.py
Uid: 1000
Socket type: 1
Socket family: 2
Socket timeout: None seconds
SO_REUSEADDR option: 0
SO_KEEPALIVE option: 0
TCP_NODELAY option: 0
checking: 6006
Timeout: None
Is Port in use: False
--- Runtime: 0.0001404285430908203 seconds ---

With networking mode mirrored:

❯ python3 test_connect.py
Uid: 1000
Socket type: 1
Socket family: 2
Socket timeout: None seconds
SO_REUSEADDR option: 0
SO_KEEPALIVE option: 0
TCP_NODELAY option: 0
checking: 6006
Timeout: None
Is Port in use: False
--- Runtime: 132.54164338111877 seconds ---

Sometimes it works as fast as without mirrored. But sometimes not, even tho it results in the port is not in use.
If it works directly, starting the script with sudo will result again in this timeout.

With networking mode mirrored and setting timeout to a second with:
s.settimeout(1) before the connect_ex call

❯ python3 test_connect.py
Uid: 1000
Socket type: 1
Socket family: 2
Socket timeout: 1.0 seconds
SO_REUSEADDR option: 0
SO_KEEPALIVE option: 0
TCP_NODELAY option: 0
checking: 6006
Timeout: None
Is Port in use: False
--- Runtime: 1.001239538192749 seconds ---

Or with settimeout to 2 seconds:

❯ python3 test_connect.py
Uid: 1000
Socket type: 1
Socket family: 2
Socket timeout: 2.0 seconds
SO_REUSEADDR option: 0
SO_KEEPALIVE option: 0
TCP_NODELAY option: 0
checking: 6006
Timeout: 2.0
Is Port in use: False
--- Runtime: 2.0022153854370117 seconds ---

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.