The following Python program hangs forever on MacOS, but does not hang on Linux:
import os, termios, time
device_fd, tty_fd = os.openpty()
def loop():
while True:
data = os.read(device_fd, 1)
print(data) # <--- never gets here
time.sleep(.1)
from threading import Thread
thread = Thread(target=loop, daemon=True)
thread.start()
os.write(tty_fd, b'123')
termios.tcdrain(tty_fd) # <--- gets stuck here
time.sleep(3) # allow thread to read all data
When I sample the process using Activity Monitor I always get the following at the bottom of the call graph for the main thread:
+ 2553 termios_tcdrain (in termios.cpython-39-darwin.so) + 56 [0x1048dedcc]
+ 2553 tcdrain (in libsystem_c.dylib) + 48 [0x19f27c454]
+ 2553 ioctl (in libsystem_kernel.dylib) + 36 [0x19f30b0c0]
+ 2553 __ioctl (in libsystem_kernel.dylib) + 8 [0x19f30b0d4]
Could someone help me understand why the program gets stuck on MacOS? I'm struggling to debug the problem beyond this point.
I've attached the full sample. Other details:
- Tested on MacOS 12.5.1 (ARM CPU).
- Also tested on MacOS with an Intel CPU.