Skip to content

Commit 31fe4f1

Browse files
committed
Fix pmonitor() to not return blank lines on EOF
fixes mininet#109 (hopefully)
1 parent dcb3036 commit 31fe4f1

1 file changed

Lines changed: 14 additions & 17 deletions

File tree

mininet/util.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from time import sleep
66
from resource import setrlimit, RLIMIT_NPROC, RLIMIT_NOFILE
7-
from select import poll, POLLIN
7+
from select import poll, POLLIN, POLLHUP
88
from subprocess import call, check_call, Popen, PIPE, STDOUT
99
import re
1010
from fcntl import fcntl, F_GETFL, F_SETFL
@@ -326,27 +326,24 @@ def pmonitor(popens, timeoutms=500, readline=True,
326326
# Use non-blocking reads
327327
flags = fcntl( fd, F_GETFL )
328328
fcntl( fd, F_SETFL, flags | O_NONBLOCK )
329-
while True:
329+
while popens:
330330
fds = poller.poll( timeoutms )
331331
if fds:
332-
for fd, _event in fds:
332+
for fd, event in fds:
333333
host = fdToHost[ fd ]
334334
popen = popens[ host ]
335-
if readline:
336-
# Attempt to read a line of output
337-
# This blocks until we receive a newline!
338-
line = popen.stdout.readline()
339-
else:
340-
line = popen.stdout.read( readmax )
341-
yield host, line
335+
if event & POLLIN:
336+
if readline:
337+
# Attempt to read a line of output
338+
# This blocks until we receive a newline!
339+
line = popen.stdout.readline()
340+
else:
341+
line = popen.stdout.read( readmax )
342+
yield host, line
342343
# Check for EOF
343-
if not line:
344-
popen.poll()
345-
if popen.returncode is not None:
346-
poller.unregister( fd )
347-
del popens[ host ]
348-
if not popens:
349-
return
344+
elif event & POLLHUP:
345+
poller.unregister( fd )
346+
del popens[ host ]
350347
else:
351348
yield None, ''
352349

0 commit comments

Comments
 (0)