Skip to content
This repository has been archived by the owner on Jun 16, 2020. It is now read-only.

Commit

Permalink
Fix select() deadlock on BSD OSes (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbedos committed Jul 10, 2018
1 parent 464f5ea commit 3a459cc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
14 changes: 6 additions & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,15 @@ jobs:
- python: 3.8-dev
dist: xenial
sudo: true
- python: nightly
dist: xenial
sudo: true
- os: osx
language: generic

matrix:
allow_failures:
- python: nightly
before_install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update && brew upgrade python && pip3 install --upgrade pip setuptools wheel; fi

install:
- pip install -U -e .[dev]
- pip freeze
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then sudo pip3 install -U -e .[dev]; else pip install -U -e .[dev]; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then pip3 freeze; else pip freeze; fi

script:
- coverage run --branch --source termtosvg -m unittest -v
Expand Down
22 changes: 11 additions & 11 deletions termtosvg/term.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging
import os
import pty
import selectors
import select
import struct
import termios
import tty
Expand Down Expand Up @@ -117,24 +117,24 @@ def _capture_data(input_fileno, output_fileno, master_fd, buffer_size=1024):
generator.
See https://github.com/python/cpython/blob/master/Lib/pty.py
"""
sel = selectors.DefaultSelector()
sel.register(master_fd, selectors.EVENT_READ)
sel.register(input_fileno, selectors.EVENT_READ)
rlist = [input_fileno, master_fd]
xlist = [input_fileno, output_fileno, master_fd]

while {master_fd, input_fileno} <= set(sel.get_map()):
events = sel.select()
for key, _ in events:
xfds = []
while not xfds:
rfds, _, xfds = select.select(rlist, [], xlist)
for fd in rfds:
try:
data = os.read(key.fileobj, buffer_size)
data = os.read(fd, buffer_size)
except OSError:
sel.unregister(key.fileobj)
xfds.append(fd)
continue

if not data:
sel.unregister(key.fileobj)
xfds.append(fd)
continue

if key.fileobj == input_fileno:
if fd == input_fileno:
write_fileno = master_fd
else:
write_fileno = output_fileno
Expand Down

0 comments on commit 3a459cc

Please sign in to comment.