P1sec / pysctp

SCTP stack for Python
http://www.p1sec.com
165 stars 67 forks source link

All Python threads hang if one of them blocks on a recv/send #7

Closed ilor closed 6 years ago

ilor commented 13 years ago

In a multi-threaded Python application if one of the threads calls send or recv on a sctp socket, all the threads hang. This is because of the Python GIL (global interpreter lock) not being released by the C module's code.

The issue can be fixed by releasing the lock before the blocking calls to sctp_sendmsg and sctp_recvmsg, patch follows:

diff --git a/_sctp.c b/_sctp.c
index 3ad048d..ec68fc9 100644
--- a/_sctp.c
+++ b/_sctp.c
@@ -1411,8 +1411,10 @@ static PyObject* sctp_send_msg(PyObject* dummy, PyObject* args)
                }
        }

+       Py_BEGIN_ALLOW_THREADS
        size_sent = sctp_sendmsg(fd, msg, msg_len, (struct sockaddr*) psto, sto_len, ppid, 
                                        flags, stream, ttl, context);
+       Py_END_ALLOW_THREADS

        if (size_sent < 0) {
                PyErr_SetFromErrno(PyExc_IOError);
@@ -1564,7 +1566,9 @@ static PyObject* sctp_recv_msg(PyObject* dummy, PyObject* args)
        bzero(&sfrom, sizeof(sfrom));
        bzero(&sinfo, sizeof(sinfo));

+       Py_BEGIN_ALLOW_THREADS
        size = sctp_recvmsg(fd, msg, max_len, (struct sockaddr*) &sfrom, &sfrom_len, &sinfo, &flags);
+       Py_END_ALLOW_THREADS                                                                                                                                                                                        

        if (size < 0) {                                                                                                                                                                                             
                free(msg);                                                                                                                                                                                          
p1-bmu commented 6 years ago

patch applied