Skip to content

Commit bccacd1

Browse files
ahcubpitrou
authored andcommitted
bpo-17560: Too small type for struct.pack/unpack in mutliprocessing.Connection (GH-10305)
Allow sending more than 2 GB at once on a multiprocessing connection on non-Windows systems.
1 parent 75d9d59 commit bccacd1

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

Lib/multiprocessing/connection.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -389,23 +389,33 @@ def _recv(self, size, read=_read):
389389

390390
def _send_bytes(self, buf):
391391
n = len(buf)
392-
# For wire compatibility with 3.2 and lower
393-
header = struct.pack("!i", n)
394-
if n > 16384:
395-
# The payload is large so Nagle's algorithm won't be triggered
396-
# and we'd better avoid the cost of concatenation.
392+
if n > 0x7fffffff:
393+
pre_header = struct.pack("!i", -1)
394+
header = struct.pack("!Q", n)
395+
self._send(pre_header)
397396
self._send(header)
398397
self._send(buf)
399398
else:
400-
# Issue #20540: concatenate before sending, to avoid delays due
401-
# to Nagle's algorithm on a TCP socket.
402-
# Also note we want to avoid sending a 0-length buffer separately,
403-
# to avoid "broken pipe" errors if the other end closed the pipe.
404-
self._send(header + buf)
399+
# For wire compatibility with 3.7 and lower
400+
header = struct.pack("!i", n)
401+
if n > 16384:
402+
# The payload is large so Nagle's algorithm won't be triggered
403+
# and we'd better avoid the cost of concatenation.
404+
self._send(header)
405+
self._send(buf)
406+
else:
407+
# Issue #20540: concatenate before sending, to avoid delays due
408+
# to Nagle's algorithm on a TCP socket.
409+
# Also note we want to avoid sending a 0-length buffer separately,
410+
# to avoid "broken pipe" errors if the other end closed the pipe.
411+
self._send(header + buf)
405412

406413
def _recv_bytes(self, maxsize=None):
407414
buf = self._recv(4)
408415
size, = struct.unpack("!i", buf.getvalue())
416+
if size == -1:
417+
buf = self._recv(8)
418+
size, = struct.unpack("!Q", buf.getvalue())
409419
if maxsize is not None and size > maxsize:
410420
return None
411421
return self._recv(size)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow sending more than 2 GB at once on a multiprocessing connection on non-Windows systems.

0 commit comments

Comments
 (0)