Skip to content

Commit

Permalink
Retry if a thread closes a socket before we select() on it
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalresistor committed Apr 21, 2022
1 parent 603d2c1 commit fb33aaa
Showing 1 changed file with 35 additions and 22 deletions.
57 changes: 35 additions & 22 deletions src/waitress/wasyncore.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,31 +151,44 @@ def poll(timeout=0.0, map=None):
if map is None: # pragma: no cover
map = socket_map
if map:
r = []
w = []
e = []
for fd, obj in list(map.items()): # list() call FBO py3
is_r = obj.readable()
is_w = obj.writable()
if is_r:
r.append(fd)
# accepting sockets should not be writable
if is_w and not obj.accepting:
w.append(fd)
if is_r or is_w:
e.append(fd)
if [] == r == w == e:
time.sleep(timeout)
return
fds = map.keys()

try:
r, w, e = select.select(r, w, e, timeout)
except OSError as err:
if err.args[0] != EINTR:
raise
else:
while True:
r = []
w = []
e = []
for fd in fds:
obj = map.get(fd)
if obj is None: # pragma: no cover
continue

is_r = obj.readable()
is_w = obj.writable()
if is_r:
r.append(fd)
# accepting sockets should not be writable
if is_w and not obj.accepting:
w.append(fd)
if is_r or is_w:
e.append(fd)
if [] == r == w == e:
time.sleep(timeout)
return

try:
r, w, e = select.select(r, w, e, timeout)
except OSError as err:
if err.args[0] == EBADF: # pragma: no cover
if fds != map.keys():
fds = map.keys()
continue
if err.args[0] != EINTR:
raise
else:
return

break

for fd in r:
obj = map.get(fd)
if obj is None: # pragma: no cover
Expand Down

0 comments on commit fb33aaa

Please sign in to comment.