WSL icon indicating copy to clipboard operation
WSL copied to clipboard

Mirrored networking: session leader's vsock bind() can still fail with EINTR, `wsl.exe -e` then hangs 30 s with Wsl/Service/0x8007274c (follow-up to #14541)

Open davidcristian opened this issue 1 month ago • 5 comments

Windows Version

Microsoft Windows [Version 10.0.26300.9711]

WSL Version

3.0.3.0

Are you using WSL 1, WSL 2, or WSLC?

  • [x] WSL 2
  • [ ] WSL 1
  • [ ] WSLC

Kernel Version

6.18.54.1

Distro Version

Ubuntu 24.04.5 LTS

Other Software

WSL: systemd=true Docker Desktop 4.96.3 with WSL integration for Ubuntu. .wslconfig: networkingMode=mirrored.

Repro Steps

1. Syscall level. On a distro running with networkingMode=mirrored, build and run the reproducer below. It does what the session leader does: a SIGCHLD handler with SA_SIGINFO and no SA_RESTART, a child that exits at a random moment, then socket(AF_VSOCK) + bind(VMADDR_PORT_ANY) + listen() as in UtilListenVsockAnyPort(). Optional background processes bind() TCP ports in a loop, roughly what systemd services and Docker do at startup.

gcc -O2 -o repro repro.c
./repro plain 3000 0      # no background load
./repro plain 1000 4      # 4 background bind() loops
Mode Load Iterations bind() failed with EINTR
current behavior (no retry, no SA_RESTART) none 3000 105 (3.5%)
current behavior 4 procs 1000 1000 (100%)
TEMP_FAILURE_RETRY around bind/listen none / 4 procs 3000 / 1000 0 / 0
SA_RESTART on the handler none / 4 procs 3000 / 1000 0 / 0

2. End to end. With ./repro load 300 4 running in the distro, start a long-lived wsl.exe -d Ubuntu -e sleep 3600 on a console, then four overlapping wsl.exe -d Ubuntu -e true on the same console so they share its session leader:

Start-Process wsl.exe '-d Ubuntu -e sleep 3600' -NoNewWindow
$p = 1..4 | % { Start-Process wsl.exe '-d Ubuntu -e true' -NoNewWindow -PassThru }
$p | % { $_.WaitForExit(); '{0} after {1:n1}s' -f $_.ExitCode, ($_.ExitTime - $_.StartTime).TotalSeconds }

Result on 2.7.3: one command succeeded in 0.1 s, one failed after 30.1 s with Wsl/Service/0x8007274c, and two never returned until I killed them 13 minutes later. dmesg shows the same UtilBindVsockAnyPort:322: bind failed 4 / InitCreateProcessUtilityVm failed pair. While those two were stuck, new wsl.exe -d Ubuntu sessions from other consoles also hung, while -d docker-desktop kept working. Ubuntu only recovered after I killed the remaining sleep 3600 client on that console.

On the master build, four-at-a-time waves didn't reproduce it, but a continuous stream did. I launched wsl.exe -e true every 30 ms with up to 8 in flight on one console, with 8 background bind() loops. That failed by command 11–12 in 3 of 3 runs with Wsl/Service/WSAETIMEDOUT.

repro.c
// Reproducer for spurious EINTR from bind()/listen() under WSL mirrored networking.
//
// Mimics the WSL session leader: a SIGCHLD handler installed without SA_RESTART,
// a child exiting at a random moment, and a vsock bind(VMADDR_PORT_ANY) + listen()
// like UtilListenVsockAnyPort(). Background processes bind() TCP sockets in a loop
// to keep the port tracker's seccomp supervisor busy, the way systemd services and
// Docker do at startup.
//
// Usage: repro <plain|restart|retry> [iterations] [load-procs]
//        repro load <seconds> [load-procs]
//   plain   - SA_SIGINFO only, no retry (current WSL behavior)
//   restart - SA_SIGINFO | SA_RESTART
//   retry   - SA_SIGINFO, bind()/listen() wrapped in TEMP_FAILURE_RETRY,
//             handler preserves errno
//   load    - only run the bind() load generators, for testing wsl.exe itself

#define _GNU_SOURCE
#include <errno.h>
#include <linux/vm_sockets.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>

static int g_preserveErrno;

static void SigchldHandler(int Signal, siginfo_t* SigInfo, void* UContext)
{
    (void)Signal;
    (void)SigInfo;
    (void)UContext;

    // Same loop as SessionLeaderSigchldHandler(). The final waitpid() returns -1/ECHILD
    // when no children remain, which overwrites the interrupted call's errno.
    int saved = errno;
    while (waitpid(-1, NULL, WNOHANG) > 0)
    {
    }

    if (g_preserveErrno)
    {
        errno = saved;
    }
}

static void Load(void)
{
    for (;;)
    {
        int fd = socket(AF_INET, SOCK_STREAM, 0);
        if (fd < 0)
        {
            continue;
        }

        struct sockaddr_in address = {0};
        address.sin_family = AF_INET;
        address.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
        address.sin_port = htons(20000 + (rand() % 20000));
        bind(fd, (struct sockaddr*)&address, sizeof(address));
        close(fd);
    }
}

int main(int argc, char** argv)
{
    const char* mode = argc > 1 ? argv[1] : "plain";
    int iterations = argc > 2 ? atoi(argv[2]) : 2000;
    int loadProcs = argc > 3 ? atoi(argv[3]) : 4;
    int retry = strcmp(mode, "retry") == 0;
    g_preserveErrno = retry;

    if (strcmp(mode, "load") == 0)
    {
        // iterations is the duration in seconds here.
        for (int i = 0; i < loadProcs; i++)
        {
            if (fork() == 0)
            {
                srand(getpid());
                alarm(iterations);
                Load();
            }
        }

        while (wait(NULL) > 0)
        {
        }

        return 0;
    }

    pid_t loaders[64];
    for (int i = 0; i < loadProcs && i < 64; i++)
    {
        loaders[i] = fork();
        if (loaders[i] == 0)
        {
            srand(getpid());
            Load();
        }
    }

    // Install the handler after the load generators exist, matching the session leader
    // (which only reaps its own relay children). The loaders are killed with SIGKILL at the
    // end and reaped explicitly.
    struct sigaction action;
    memset(&action, 0, sizeof(action));
    action.sa_flags = SA_SIGINFO | (strcmp(mode, "restart") == 0 ? SA_RESTART : 0);
    action.sa_sigaction = SigchldHandler;
    sigaction(SIGCHLD, &action, NULL);

    int bindFail = 0, listenFail = 0, ok = 0;
    int errnoCounts[256] = {0};
    srand(time(NULL));

    for (int i = 0; i < iterations; i++)
    {
        // A short-lived child, like the relay of the previous command exiting.
        pid_t child = fork();
        if (child == 0)
        {
            struct timespec ts = {0, (rand() % 2000) * 1000};
            nanosleep(&ts, NULL);
            _exit(0);
        }

        int fd = socket(AF_VSOCK, SOCK_STREAM, 0);
        if (fd < 0)
        {
            perror("socket(AF_VSOCK)");
            return 1;
        }

        struct sockaddr_vm address = {0};
        address.svm_family = AF_VSOCK;
        address.svm_cid = VMADDR_CID_ANY;
        address.svm_port = VMADDR_PORT_ANY;

        int result = retry ? TEMP_FAILURE_RETRY(bind(fd, (struct sockaddr*)&address, sizeof(address)))
                           : bind(fd, (struct sockaddr*)&address, sizeof(address));
        if (result < 0)
        {
            bindFail++;
            errnoCounts[errno & 0xff]++;
        }
        else
        {
            result = retry ? TEMP_FAILURE_RETRY(listen(fd, 5)) : listen(fd, 5);
            if (result < 0)
            {
                listenFail++;
                errnoCounts[errno & 0xff]++;
            }
            else
            {
                ok++;
            }
        }

        close(fd);
    }

    for (int i = 0; i < loadProcs && i < 64; i++)
    {
        kill(loaders[i], SIGKILL);
    }

    signal(SIGCHLD, SIG_DFL);
    while (waitpid(-1, NULL, 0) > 0)
    {
    }

    printf("mode=%s iterations=%d load=%d ok=%d bind_failed=%d listen_failed=%d", mode, iterations, loadProcs, ok, bindFail, listenFail);
    for (int e = 0; e < 256; e++)
    {
        if (errnoCounts[e])
        {
            printf(" errno%d(%s)=%d", e, strerror(e), errnoCounts[e]);
        }
    }

    printf("\n");
    return 0;
}

Expected Behavior

wsl.exe -d <distro> -e <cmd> succeeds regardless of what else is calling bind() in the distro.

Actual Behavior

About 30% of my boots, Docker Desktop's WSL integration fails with "WSL integration with distro 'Ubuntu' unexpectedly stopped". Its wsl.exe -d ubuntu -e whoami hangs for 30 s and exits with:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.
Error code: Wsl/Service/0x8007274c

At that moment the distro's kernel log shows:

WSL (923 - SessionLeader) ERROR: UtilBindVsockAnyPort:322: bind failed 4
WSL (923 - SessionLeader) ERROR: SessionLeaderEntryUtilityVm:3083: InitCreateProcessUtilityVm failed

Clicking "Restart the WSL integration" succeeds immediately. This is the same signature as #14541 (closed after #14554 shipped in 2.7.3) and docker/desktop-feedback#144, where reporters still see it on 2.7.12 and 2.7.14 with networkingMode=mirrored + systemd=true.

Diagnostic Logs

I can collect collect-wsl-logs.ps1 output on request. The dmesg lines above and the reproducer cover the failure. I have a PR ready for this.

davidcristian avatar Oct 22 '26 21:10 davidcristian

Logs are required for review from WSL team

If this a feature request, please reply with '/feature'. If this is a question, reply with '/question'. Otherwise, please attach logs by following the instructions below, your issue will not be reviewed unless they are added. These logs will help us understand what is going on in your machine.

How to collect WSL logs

Download and execute collect-wsl-logs.ps1 in an administrative powershell prompt:

Invoke-WebRequest -UseBasicParsing "https://raw.githubusercontent.com/microsoft/WSL/master/diagnostics/collect-wsl-logs.ps1" -OutFile collect-wsl-logs.ps1
Set-ExecutionPolicy Bypass -Scope Process -Force
.\collect-wsl-logs.ps1

The script will output the path of the log file once done.

If this is a networking issue, please use .\collect-wsl-logs.ps1 -LogProfile networking instead, following the instructions in Collect WSL logs for networking issues

Once completed please upload the output files to this GitHub issue.

See Collect WSL logs (recommended method).

If you choose to email these logs instead of attaching them to the bug, please send them to [email protected] with the GitHub issue number in the subject, and include a link to your GitHub issue comment in the message body, and reply with '/emailed-logs'.

github-actions[bot] avatar Oct 22 '26 21:10 github-actions[bot]

Collected with collect-wsl-logs.ps1 (default profile) on WSL 3.0.3.0 / kernel 6.18.54.1, while running the streaming repro from the issue (wsl.exe -e true every 30 ms, up to 8 in flight on one console, 8 background bind() loops in the distro). It failed within the first 10 commands: Wsl/Service/WSAETIMEDOUT after 30.1 s. The failing command started around 21:56:10 UTC, and the run ended at 21:56:46 UTC.

The logs were collected from an elevated prompt under a separate admin account, so the archive's .wslconfig and HKCU export are that account's, not the affected user's. The affected user's .wslconfig:

[wsl2]
networkingMode=mirrored
memory=32GB

Distro: Ubuntu 24.04.5, /etc/wsl.conf has [boot] systemd=true.

Distro kernel log at the failure:

[ 1100.725818] WSL (1196 - SessionLeader) ERROR: UtilBindVsockAnyPort:330: bind failed 4
[ 1100.726238] WSL (1196 - SessionLeader) ERROR: SessionLeaderEntryUtilityVm:3261: InitCreateProcessUtilityVm failed

/emailed-logs

davidcristian avatar Oct 22 '26 21:10 davidcristian

Diagnostic information
Found '/emailed-logs', adding tag 'emailed-logs'

github-actions[bot] avatar Oct 22 '26 21:10 github-actions[bot]

I can confirm the same failure signature with WSL 2.7.14.0, Docker Desktop 4.96.3, Windows 11 (build 26200.9711), and an Ubuntu WSL 2 distro using mirrored networking.

Docker Desktop fails during "setup groups" when its "wsl.exe -d -e whoami" call times out with Wsl/Service/0x8007274c. The corresponding kernel messages are:

UtilBindVsockAnyPort:322: bind failed 4
SessionLeaderEntryUtilityVm:3083: InitCreateProcessUtilityVm failed

Two consecutive successful whoami probes immediately before launching Docker Desktop did not prevent the later integration call from failing. The signature also recurred after a full WSL shutdown and fresh startup, while subsequent independent commands in the Ubuntu distro could succeed.

This is an additional observation on the released 2.7.14.0 build. I have not tested a build containing the fix proposed in #42035.

agolanov avatar Oct 25 '26 15:10 agolanov

Fixed in 3.0.5 with #42035.

chemwolf6922 avatar Nov 17 '26 06:11 chemwolf6922