Skip to content

Commit

Permalink
bug(console_reader): replace TIOCSTI with busy wait to suppport kerne…
Browse files Browse the repository at this point in the history
…l > 6.2

Closes espressif/esp-idf#11027
  • Loading branch information
peterdragun committed Apr 21, 2023
1 parent c8d1579 commit 6eefb5c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
2 changes: 1 addition & 1 deletion esp_idf_monitor/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .base.ansi_color_converter import ANSIColorConverter # noqa: F401
from .base.ansi_color_converter import get_ansi_converter # noqa: F401

__version__ = '1.0.3'
__version__ = '1.0.4'
30 changes: 10 additions & 20 deletions esp_idf_monitor/base/console_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ def __init__(self, console, event_queue, cmd_queue, parser, test_mode):
def run(self):
# type: () -> None
self.console.setup()
if os.name == 'posix':
# Use non-blocking busy read to avoid using unsecure TIOCSTI from console.cancel().
# TIOCSTI is not supported on kernels newer than 6.2.
import termios
new = termios.tcgetattr(self.console.fd)
# new[6] - 'cc': a list of the tty special characters
new[6][termios.VMIN] = 0 # minimum bytes to read
new[6][termios.VTIME] = 2 # timer of 0.1 second granularity
termios.tcsetattr(self.console.fd, termios.TCSANOW, new)
try:
while self.alive:
try:
Expand All @@ -53,7 +62,7 @@ def run(self):
c = self.console.getkey()
except KeyboardInterrupt:
c = '\x03'
if c is not None:
if c:
ret = self.parser.parse(c)
if ret is not None:
(tag, cmd) = ret
Expand All @@ -65,22 +74,3 @@ def run(self):

finally:
self.console.cleanup()

def _cancel(self):
# type: () -> None
if os.name == 'posix' and not self.test_mode:
# this is the way cancel() is implemented in pyserial 3.3 or newer,
# older pyserial (3.1+) has cancellation implemented via 'select',
# which does not work when console sends an escape sequence response
#
# even older pyserial (<3.1) does not have this method
#
# on Windows there is a different (also hacky) fix, applied above.
#
# note that TIOCSTI is not implemented in WSL / bash-on-Windows.
# TODO: introduce some workaround to make it work there.
#
# Note: This would throw exception in testing mode when the stdin is connected to PTY.
import fcntl
import termios
fcntl.ioctl(self.console.fd, termios.TIOCSTI, b'\0')

0 comments on commit 6eefb5c

Please sign in to comment.