Skip to content

non blocking connect() never succeeds #2949

Closed

Description

@mcgoo

WSL never returns 0 for success if connect() is called multiple times on a non-blocking socket, it transitions straight from EINPROGRESS to EISCONN.

Build 10.0.16299

repro:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <errno.h>
#include <netinet/in.h>

int main() {

    int fd = socket( AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0 );
    if (fd < 0)
        perror("socket: ");

    struct sockaddr_in ip4addr;
    ip4addr.sin_family = AF_INET;
    ip4addr.sin_port = htons(80);
    inet_pton(AF_INET, "74.125.69.99", &ip4addr.sin_addr);


    for(;;) {

        int res = connect(fd, (struct sockaddr*)&ip4addr, sizeof(ip4addr));
        printf("called connect, res=%d ", res);
        if(res < 0) {
            printf("    errno=%d ", errno);
            switch(errno) {
                case EINPROGRESS:
                    printf("        in progress\n");
                    continue;
                case EALREADY:
                    printf("        already in progress\n");
                    continue;
                case EISCONN:
                    printf("        is connected\n");
                    goto done;
                default:
                    printf("        something else, bailing out\n");
                    goto done;
            }
        } else {
            printf("\n");
        }

    }
done:

    return 0;
}

Linux

called connect, res=-1     errno=115         in progress
called connect, res=-1     errno=114         already in progress
called connect, res=-1     errno=114         already in progress
...
called connect, res=-1     errno=114         already in progress
called connect, res=-1     errno=114         already in progress
called connect, res=0
called connect, res=-1     errno=106         is connected

WSL

called connect, res=-1     errno=115         in progress
called connect, res=-1     errno=114         already in progress
called connect, res=-1     errno=114         already in progress
...
called connect, res=-1     errno=114         already in progress
called connect, res=-1     errno=114         already in progress
called connect, res=-1     errno=106         is connected

Possibly related to #2846

Activity

  1. mcgoo commented on Feb 13, 2018

    @mcgoo
    Author

    Also present in 10.0.17093.1000.

  2. therealkenc commented on Feb 13, 2018

    @therealkenc

    Possibly related to #2846

    I'd be surprised if it wasn't. Was irssi what you were tracking, or did you just happen across that issue? Which is to say, if you have another breaking case, the better. Great post on the merits either way.

  3. mcgoo commented on Feb 14, 2018

    @mcgoo
    Author

    No, it's a binary vendor library. I found the irssi issue by searching for WSL connect EINPROGRESS I think.

  4. therealkenc commented on Aug 1, 2020

    @therealkenc

    Just did the exercise and the OP (thank you for the concise repro) still does not behave correctly (return res=0) on WSL1 20180.

    image

    Better in WSL2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions