This is the mail archive of the cygwin mailing list for the Cygwin project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

deadlock/hang when calling close()/connect() at the same time on the same socket

It appears that opening a socket, and then calling connect() on it and
from another thread calling close() on the socket while it's still in
connect() results in a deadlock. Furthermore in this state the thread
cannot be canceled and connect() will never return (my testcase uses
pthread_cancel(), but it happens without that as well)

This does not happen on linux. I tested this with the latest snapshot,
same problem. I'm running Win7 and I've attached a test case.
#include <pthread.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdio.h>

int sock;
struct sockaddr_in addr;

void* my_thread(void *arg);
int main(int argc, char **argv){
    printf("Test started\n");
    pthread_t thread;
    sock = socket(AF_INET, SOCK_STREAM, 0);
    addr.sin_family = AF_INET;
    addr.sin_port = htons(8676);
    addr.sin_addr.s_addr = inet_addr("");

    pthread_create(&thread, NULL, my_thread, NULL);
    if (close(sock)){
    pthread_join(thread, NULL);
    printf("no bug\n");
    return 0;

void* my_thread(void *arg){

    if (connect(sock, (struct sockaddr*)&addr, sizeof(addr)) == -1){
    } else {
        printf("Returned success\n");

    return NULL;

Attachment: cygcheck.out
Description: Text document

Problem reports:
Unsubscribe info:

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]