diff -r 97e01e107591 Modules/socketmodule.c --- a/Modules/socketmodule.c Sun Mar 08 15:29:39 2015 -0700 +++ b/Modules/socketmodule.c Mon Mar 09 10:04:46 2015 +0100 @@ -2387,10 +2387,16 @@ static int internal_connect(PySocketSockObject *s, struct sockaddr *addr, int addrlen, int *timeoutp) { - int res, timeout; + int res, timeout, async_err = 0; timeout = 0; - res = connect(s->sock_fd, addr, addrlen); + do { + Py_BEGIN_ALLOW_THREADS + res = connect(s->sock_fd, addr, addrlen); + Py_END_ALLOW_THREADS + } while (res < 0 && errno == EINTR && !(async_err = PyErr_CheckSignals())); + if (async_err) + return NULL; #ifdef MS_WINDOWS @@ -2401,12 +2407,15 @@ internal_connect(PySocketSockObject *s, fd_set fds; fd_set fds_exc; struct timeval tv; + tv.tv_sec = (int)s->sock_timeout; tv.tv_usec = (int)((s->sock_timeout - tv.tv_sec) * 1e6); FD_ZERO(&fds); FD_SET(s->sock_fd, &fds); FD_ZERO(&fds_exc); FD_SET(s->sock_fd, &fds_exc); + + Py_BEGIN_ALLOW_THREADS res = select(Py_SAFE_DOWNCAST(s->sock_fd+1, SOCKET_T, int), NULL, &fds, &fds_exc, &tv); if (res == 0) { @@ -2433,6 +2442,7 @@ internal_connect(PySocketSockObject *s, } } /* else if (res < 0) an error occurred */ + Py_END_ALLOW_THREADS } } @@ -2443,6 +2453,7 @@ internal_connect(PySocketSockObject *s, if (s->sock_timeout > 0.0) { if (res < 0 && errno == EINPROGRESS && IS_SELECTABLE(s)) { + Py_BEGIN_ALLOW_THREADS timeout = internal_select(s, 1); if (timeout == 0) { /* Bug #1019808: in case of an EINPROGRESS, @@ -2460,6 +2471,7 @@ internal_connect(PySocketSockObject *s, } else res = EWOULDBLOCK; /* timed out */ + Py_END_ALLOW_THREADS } } @@ -2485,9 +2497,7 @@ sock_connect(PySocketSockObject *s, PyOb if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) return NULL; - Py_BEGIN_ALLOW_THREADS res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); - Py_END_ALLOW_THREADS if (timeout == 1) { PyErr_SetString(socket_timeout, "timed out"); @@ -2519,9 +2529,7 @@ sock_connect_ex(PySocketSockObject *s, P if (!getsockaddrarg(s, addro, SAS2SA(&addrbuf), &addrlen)) return NULL; - Py_BEGIN_ALLOW_THREADS res = internal_connect(s, SAS2SA(&addrbuf), addrlen, &timeout); - Py_END_ALLOW_THREADS /* Signals are not errors (though they may raise exceptions). Adapted from PyErr_SetFromErrnoWithFilenameObject(). */