This repository has been archived by the owner on Apr 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
python-2.6.6-multiprocessing-ignore-eintr.patch
198 lines (188 loc) · 6.65 KB
/
python-2.6.6-multiprocessing-ignore-eintr.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
diff -up Python-2.6.6/Lib/multiprocessing/connection.py.eintr Python-2.6.6/Lib/multiprocessing/connection.py
--- Python-2.6.6/Lib/multiprocessing/connection.py.eintr 2015-02-09 10:36:07.108787015 +0100
+++ Python-2.6.6/Lib/multiprocessing/connection.py 2015-02-09 10:38:31.225809531 +0100
@@ -238,7 +238,14 @@ class SocketListener(object):
self._unlink = None
def accept(self):
- s, self._last_accepted = self._socket.accept()
+ while True:
+ try:
+ s, self._last_accepted = self._socket.accept()
+ except socket.error as e:
+ if e.args[0] != errno.EINTR:
+ raise
+ else:
+ break
fd = duplicate(s.fileno())
conn = _multiprocessing.Connection(fd)
s.close()
diff -up Python-2.6.6/Lib/test/test_multiprocessing.py.eintr Python-2.6.6/Lib/test/test_multiprocessing.py
--- Python-2.6.6/Lib/test/test_multiprocessing.py.eintr 2015-02-09 10:38:54.435980621 +0100
+++ Python-2.6.6/Lib/test/test_multiprocessing.py 2015-02-09 10:40:19.370606056 +0100
@@ -1910,7 +1910,69 @@ class TestStdinBadfiledescriptor(unittes
flike.flush()
assert sio.getvalue() == 'foo'
-testcases_other = [OtherTest, TestInvalidHandle, TestStdinBadfiledescriptor]
+class TestIgnoreEINTR(unittest.TestCase):
+
+ @classmethod
+ def _test_ignore(cls, conn):
+ def handler(signum, frame):
+ pass
+ signal.signal(signal.SIGUSR1, handler)
+ conn.send('ready')
+ x = conn.recv()
+ conn.send(x)
+ conn.send_bytes(b'x'*(1024*1024)) # sending 1 MB should block
+
+ def test_ignore(self):
+ conn, child_conn = multiprocessing.Pipe()
+ try:
+ p = multiprocessing.Process(target=self._test_ignore,
+ args=(child_conn,))
+ p.daemon = True
+ p.start()
+ child_conn.close()
+ self.assertEqual(conn.recv(), 'ready')
+ time.sleep(0.1)
+ os.kill(p.pid, signal.SIGUSR1)
+ time.sleep(0.1)
+ conn.send(1234)
+ self.assertEqual(conn.recv(), 1234)
+ time.sleep(0.1)
+ os.kill(p.pid, signal.SIGUSR1)
+ self.assertEqual(conn.recv_bytes(), b'x'*(1024*1024))
+ time.sleep(0.1)
+ p.join()
+ finally:
+ conn.close()
+
+ @classmethod
+ def _test_ignore_listener(cls, conn):
+ def handler(signum, frame):
+ pass
+ signal.signal(signal.SIGUSR1, handler)
+ l = multiprocessing.connection.Listener()
+ conn.send(l.address)
+ a = l.accept()
+ a.send('welcome')
+
+ def test_ignore_listener(self):
+ conn, child_conn = multiprocessing.Pipe()
+ try:
+ p = multiprocessing.Process(target=self._test_ignore_listener,
+ args=(child_conn,))
+ p.daemon = True
+ p.start()
+ child_conn.close()
+ address = conn.recv()
+ time.sleep(0.1)
+ os.kill(p.pid, signal.SIGUSR1)
+ time.sleep(0.1)
+ client = multiprocessing.connection.Client(address)
+ self.assertEqual(client.recv(), 'welcome')
+ p.join()
+ finally:
+ conn.close()
+
+testcases_other = [OtherTest, TestInvalidHandle, TestStdinBadfiledescriptor, TestIgnoreEINTR]
#
#
diff -up Python-2.6.6/Modules/_multiprocessing/socket_connection.c.eintr Python-2.6.6/Modules/_multiprocessing/socket_connection.c
--- Python-2.6.6/Modules/_multiprocessing/socket_connection.c.eintr 2015-02-09 10:40:43.404781926 +0100
+++ Python-2.6.6/Modules/_multiprocessing/socket_connection.c 2015-02-09 11:02:35.755103437 +0100
@@ -22,6 +22,21 @@
#endif
/*
+ * Wrapper for PyErr_CheckSignals() which can be called without the GIL
+ */
+
+static int
+check_signals(void)
+{
+ PyGILState_STATE state;
+ int res;
+ state = PyGILState_Ensure();
+ res = PyErr_CheckSignals();
+ PyGILState_Release(state);
+ return res;
+}
+
+/*
* Send string to file descriptor
*/
@@ -33,7 +48,13 @@ _conn_sendall(HANDLE h, char *string, si
while (length > 0) {
res = WRITE(h, p, length);
- if (res < 0)
+ if (res < 0) {
+ if (errno == EINTR) {
+ if (check_signals() < 0)
+ return MP_EXCEPTION_HAS_BEEN_SET;
+ continue;
+ }
return MP_SOCKET_ERROR;
+ }
length -= res;
p += res;
@@ -55,12 +75,16 @@ _conn_recvall(HANDLE h, char *buffer, si
while (remaining > 0) {
temp = READ(h, p, remaining);
- if (temp <= 0) {
- if (temp == 0)
- return remaining == length ?
- MP_END_OF_FILE : MP_EARLY_END_OF_FILE;
- else
- return temp;
+ if (temp < 0) {
+ if (errno == EINTR) {
+ if (check_signals() < 0)
+ return MP_EXCEPTION_HAS_BEEN_SET;
+ continue;
+ }
+ return temp;
+ }
+ else if (temp == 0) {
+ return remaining == length ? MP_END_OF_FILE : MP_EARLY_END_OF_FILE;
}
remaining -= temp;
p += temp;
@@ -166,8 +190,18 @@ conn_poll(ConnectionObject *conn, double
p.events = POLLIN | POLLPRI;
p.revents = 0;
- res = poll(&p, 1, (int)(timeout * 1000));
-
+ if (timeout < 0) {
+ do {
+ res = poll(&p, 1, (int)(timeout * 1000));
+ } while (res < 0 && errno == EINTR);
+ } else {
+ res = poll(&p, 1, (int)(timeout * 1000));
+ if (res < 0 && errno == EINTR) {
+ /* We were interrupted by a signal. Just indicate a
+ timeout even though we are early. */
+ return FALSE;
+ }
+ }
if (res < 0) {
return MP_SOCKET_ERROR;
} else if (p.revents & (POLLIN | POLLPRI)) {
@@ -200,12 +234,19 @@ conn_poll(ConnectionObject *conn, double
FD_SET((SOCKET)conn->handle, &rfds);
if (timeout < 0.0) {
- res = select((int)conn->handle+1, &rfds, NULL, NULL, NULL);
+ do {
+ res = select((int)conn->handle+1, &rfds, NULL, NULL, NULL);
+ } while (res < 0 && errno == EINTR);
} else {
struct timeval tv;
tv.tv_sec = (long)timeout;
tv.tv_usec = (long)((timeout - tv.tv_sec) * 1e6 + 0.5);
res = select((int)conn->handle+1, &rfds, NULL, NULL, &tv);
+ if (res < 0 && errno == EINTR) {
+ /* We were interrupted by a signal. Just indicate a
+ timeout even though we are early. */
+ return FALSE;
+ }
}
if (res < 0) {