|
| 1 | +import errno |
| 2 | +import fcntl |
| 3 | +import os |
| 4 | +import pty |
| 5 | +import signal |
| 6 | +import sys |
| 7 | +import termios |
| 8 | + |
| 9 | + |
| 10 | +def handler(sig, f): |
| 11 | + pass |
| 12 | + |
| 13 | + |
| 14 | +def create_eio_condition(): |
| 15 | + # SIGINT handler used to produce an EIO. |
| 16 | + # See https://github.com/python/cpython/issues/135329. |
| 17 | + try: |
| 18 | + master_fd, slave_fd = pty.openpty() |
| 19 | + child_pid = os.fork() |
| 20 | + if child_pid == 0: |
| 21 | + try: |
| 22 | + os.setsid() |
| 23 | + fcntl.ioctl(slave_fd, termios.TIOCSCTTY, 0) |
| 24 | + child_process_group_id = os.getpgrp() |
| 25 | + grandchild_pid = os.fork() |
| 26 | + if grandchild_pid == 0: |
| 27 | + os.setpgid(0, 0) # set process group for grandchild |
| 28 | + os.dup2(slave_fd, 0) # redirect stdin |
| 29 | + if slave_fd > 2: |
| 30 | + os.close(slave_fd) |
| 31 | + # Fork grandchild for terminal control manipulation |
| 32 | + if os.fork() == 0: |
| 33 | + sys.exit(0) # exit the child process that was just obtained |
| 34 | + else: |
| 35 | + try: |
| 36 | + os.tcsetpgrp(0, child_process_group_id) |
| 37 | + except OSError: |
| 38 | + pass |
| 39 | + sys.exit(0) |
| 40 | + else: |
| 41 | + # Back to child |
| 42 | + try: |
| 43 | + os.setpgid(grandchild_pid, grandchild_pid) |
| 44 | + except ProcessLookupError: |
| 45 | + pass |
| 46 | + os.tcsetpgrp(slave_fd, grandchild_pid) |
| 47 | + if slave_fd > 2: |
| 48 | + os.close(slave_fd) |
| 49 | + os.waitpid(grandchild_pid, 0) |
| 50 | + # Manipulate terminal control to create EIO condition |
| 51 | + os.tcsetpgrp(master_fd, child_process_group_id) |
| 52 | + # Now try to read from master - this might cause EIO |
| 53 | + try: |
| 54 | + os.read(master_fd, 1) |
| 55 | + except OSError as e: |
| 56 | + if e.errno == errno.EIO: |
| 57 | + print(f"Setup created EIO condition: {e}", file=sys.stderr) |
| 58 | + sys.exit(0) |
| 59 | + except Exception as setup_e: |
| 60 | + print(f"Setup error: {setup_e}", file=sys.stderr) |
| 61 | + sys.exit(1) |
| 62 | + else: |
| 63 | + # Parent process |
| 64 | + os.close(slave_fd) |
| 65 | + os.waitpid(child_pid, 0) |
| 66 | + # Now replace stdin with master_fd and try to read |
| 67 | + os.dup2(master_fd, 0) |
| 68 | + os.close(master_fd) |
| 69 | + # This should now trigger EIO |
| 70 | + print(f"Unexpectedly got input: {input()!r}", file=sys.stderr) |
| 71 | + sys.exit(0) |
| 72 | + except OSError as e: |
| 73 | + if e.errno == errno.EIO: |
| 74 | + print(f"Got EIO: {e}", file=sys.stderr) |
| 75 | + sys.exit(1) |
| 76 | + elif e.errno == errno.ENXIO: |
| 77 | + print(f"Got ENXIO (no such device): {e}", file=sys.stderr) |
| 78 | + sys.exit(1) # Treat ENXIO as success too |
| 79 | + else: |
| 80 | + print(f"Got other OSError: errno={e.errno} {e}", file=sys.stderr) |
| 81 | + sys.exit(2) |
| 82 | + except EOFError as e: |
| 83 | + print(f"Got EOFError: {e}", file=sys.stderr) |
| 84 | + sys.exit(3) |
| 85 | + except Exception as e: |
| 86 | + print(f"Got unexpected error: {type(e).__name__}: {e}", file=sys.stderr) |
| 87 | + sys.exit(4) |
| 88 | + |
| 89 | + |
| 90 | +if __name__ == "__main__": |
| 91 | + # Set up signal handler for coordination |
| 92 | + signal.signal(signal.SIGUSR1, lambda *a: create_eio_condition()) |
| 93 | + print("READY", flush=True) |
| 94 | + signal.pause() |
0 commit comments