Skip to content

Commit bd53348

Browse files
author
Pietro Albini
committed
Catch InterruptedError while interacting with sockets in the IPC
When someone sends a signal to a process, every system call running in it simply interrupts for reasons. Python 3.5 automatically retries the call, but Python 3.4 raises an InterruptedError. This commit makes sure if a signal is received on Python 3.4 and reading/writing on sockets in the IPC server is interrupted, the call is retried.
1 parent f200524 commit bd53348

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

botogram/runner/ipc.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,14 @@ def _read_from_socket(conn, length):
236236
if chunk > READ_MAX_CHUNK:
237237
chunk = READ_MAX_CHUNK
238238

239-
resp = conn.recv(chunk)
239+
# In Python 3.4, when the process received a signal every system call
240+
# is interrupted, so it's better to retry sending the data instead of
241+
# crashing when someone signals the process
242+
try:
243+
resp = conn.recv(chunk)
244+
except InterruptedError:
245+
continue
246+
240247
if len(resp) == 0:
241248
raise EOFError("Broken socket!")
242249

@@ -250,7 +257,14 @@ def _write_on_socket(conn, data):
250257
"""Write a chunk of data on a connection"""
251258
remaining = len(data)
252259
while remaining > 0:
253-
sent = conn.send(data[len(data) - remaining:])
260+
# In Python 3.4, when the process received a signal every system call
261+
# is interrupted, so it's better to retry sending the data instead of
262+
# crashing when someone signals the process
263+
try:
264+
sent = conn.send(data[len(data) - remaining:])
265+
except InterruptedError:
266+
continue
267+
254268
if sent == 0:
255269
raise EOFError("Broken socket!")
256270

0 commit comments

Comments
 (0)