Skip to content

Commit

Permalink
fix(concurrency): handle reading from pipe with Python 2 and 3 compat…
Browse files Browse the repository at this point in the history
…ibility

Signed-off-by: Martin Styk <[email protected]>
  • Loading branch information
StykMartin committed Oct 28, 2024
1 parent db340f3 commit b92810c
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions LabController/src/bkr/labcontroller/concurrency.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import errno
import logging
import gevent.event, gevent.socket, gevent.hub
import six

logger = logging.getLogger(__name__)

Expand All @@ -29,22 +30,27 @@ def _read_from_pipe(f):
discarding = False
while True:
try:
gevent.socket.wait_read(f.fileno())
chunk = f.read(4096)
if not chunk:
break
if not discarding:
chunks.append(chunk)
if len(chunks) >= 1000:
logger.error('Too many chunks read from fd %s, '
'child process is running amok?!', f.fileno())
chunks.append('+++ DISCARDED')
logger.error(
"Too many chunks read from fd %s, "
"child process is running amok?!",
f.fileno(),
)
chunks.append(b"+++ DISCARDED")
discarding = True
except IOError as e:
if e.errno != errno.EAGAIN:
raise
sys.exc_clear()
gevent.socket.wait_read(f.fileno())
return ''.join(chunks)
if six.PY3:
# Keep data in bytes until the end to reduce memory footprint
return b"".join(chunks).decode("utf-8")
return "".join(chunks)

def _timeout_kill(p, timeout):
gevent.sleep(timeout)
Expand Down

0 comments on commit b92810c

Please sign in to comment.