Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cmd2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1215,6 +1215,9 @@ def _write_bytes(stream: StdSim | TextIO, to_write: bytes | str) -> None:
stream.buffer.write(to_write)


_RELAY_CHUNK_SIZE = 65536


class _DescriptorRelay:
"""Carry output that subprocesses write to a terminal pipeline's descriptor.

Expand Down Expand Up @@ -1304,7 +1307,7 @@ def _relay(self) -> None:
# yet counted would look passed on to idle() and flush(), and cmd2's next write could overtake it.
incoming.poll()
with self._lock:
data = os.read(self._in_fd, 65536)
data = os.read(self._in_fd, _RELAY_CHUNK_SIZE)
self._received += len(data)
if not data:
return
Expand Down
22 changes: 14 additions & 8 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@
HELLO_WORLD = "Hello, world!"


def _pipe_capacity(fd: int) -> int:
"""Return the pipe capacity, falling back to the macOS/BSD maximum where it cannot be queried."""
import fcntl

return fcntl.fcntl(fd, fcntl.F_GETPIPE_SZ) if hasattr(fcntl, "F_GETPIPE_SZ") else 65536


def test_remove_duplicates_no_duplicates() -> None:
no_dups = [5, 4, 3, 2, 1]
assert cu.remove_duplicates(no_dups) == no_dups
Expand Down Expand Up @@ -480,18 +487,15 @@ def test_pipeline_writer_relay_lends_only_while_a_producer_waits(pauses) -> None
waiting, command code gets the terminal back, whether or not the consumer has read all
its output yet. That is as between cmd2's own writes.
"""
import fcntl
import subprocess
import threading

read_fd, write_fd = os.pipe()
# Size the payload from the consumer's pipe. Linux reports its capacity, which follows the page size (16 pages by
# default); macOS and the BSDs cap a pipe at 64 KiB.
capacity = fcntl.fcntl(write_fd, fcntl.F_GETPIPE_SZ) if hasattr(fcntl, "F_GETPIPE_SZ") else 65536
capacity = _pipe_capacity(write_fd)
# More than the relay and both pipes hold, so the child blocks until the consumer reads. What remains after the consumer's
# first read fits, so the child can then finish. It is also more than the consumer's pipe holds, so part of it is still
# in the relay when cmd2 writes after it.
payload = b"x" * (2 * capacity + 2 * 65536)
payload = b"x" * (2 * capacity + 2 * cu._RELAY_CHUNK_SIZE)
first_read = len(payload) - capacity - 16384 if pauses else len(payload)
received = bytearray()
lent = threading.Event()
Expand Down Expand Up @@ -559,8 +563,9 @@ def test_pipeline_writer_relay_lends_only_to_a_consumer_waiting_for_the_terminal
import subprocess
import threading

payload = b"x" * 262144
read_fd, write_fd = os.pipe()
capacity = _pipe_capacity(write_fd)
payload = b"x" * (4 * capacity)
received = bytearray()
lends = []

Expand All @@ -571,7 +576,7 @@ def lend_terminal():

def drain() -> None:
# So much slower than the producer that the relay's pipe fills up and its writes stall
while chunk := os.read(read_fd, 16384):
while chunk := os.read(read_fd, capacity // 4):
received.extend(chunk)
time.sleep(0.15)

Expand Down Expand Up @@ -599,8 +604,9 @@ def drain() -> None:
def test_pipeline_writer_relay_leaves_the_terminal_after_a_producer_finishes() -> None:
import subprocess

payload = b"x" * 70000
read_fd, write_fd = os.pipe()
capacity = _pipe_capacity(write_fd)
payload = b"x" * (capacity + 4096)
reader = mock.Mock(_lend_terminal=mock.Mock(side_effect=contextlib.nullcontext))
writer = cu._PipelineWriter(write_fd, reader)
try:
Expand Down
Loading