set S_CTTYREF on macOS to prevent PTY output loss — workaround for XNU kernel bug (pexpect #662)
- SHA
ad653e81e4a61f804828a08feaf84bf46f351655- Parents
-
747a4ef - Tree
e1e585e
ad653e8
ad653e81e4a61f804828a08feaf84bf46f351655747a4ef
e1e585e| Status | File | + | - |
|---|---|---|---|
| M |
src/c_interop/fd_wrapper.c
|
12 | 0 |
| M |
src/fortsh.f90
|
8 | 0 |
src/c_interop/fd_wrapper.cmodified@@ -5,6 +5,18 @@ | ||
| 5 | 5 | #include <sys/stat.h> |
| 6 | 6 | #include <sys/types.h> |
| 7 | 7 | |
| 8 | +// macOS kernel bug workaround: set S_CTTYREF on the controlling terminal. | |
| 9 | +// On macOS, PTY slave output is discarded when the child process exits unless | |
| 10 | +// /dev/tty was opened (which sets S_CTTYREF). Without this, pexpect-based tests | |
| 11 | +// lose output from commands that fork short-lived child processes. | |
| 12 | +// See: https://github.com/pexpect/pexpect/issues/662 | |
| 13 | +void fortsh_set_cttyref(void) { | |
| 14 | +#ifdef __APPLE__ | |
| 15 | + int fd = open("/dev/tty", O_WRONLY); | |
| 16 | + if (fd >= 0) close(fd); | |
| 17 | +#endif | |
| 18 | +} | |
| 19 | + | |
| 8 | 20 | // Wrapper for open() that takes mode as a separate parameter |
| 9 | 21 | // This works around a bug in Fortran's C binding where mode_t is not passed correctly |
| 10 | 22 | int fortsh_open(const char *pathname, int flags, int mode) { |
src/fortsh.f90modified@@ -54,6 +54,14 @@ program fortran_shell | ||
| 54 | 54 | character(len=16) :: col_str_buf |
| 55 | 55 | character(len=2048) :: embedded_prompt |
| 56 | 56 | |
| 57 | + ! macOS: set S_CTTYREF on controlling terminal to prevent PTY output loss | |
| 58 | + ! (macOS kernel discards slave PTY buffer on child exit without this flag) | |
| 59 | + interface | |
| 60 | + subroutine fortsh_set_cttyref() bind(C, name="fortsh_set_cttyref") | |
| 61 | + end subroutine | |
| 62 | + end interface | |
| 63 | + call fortsh_set_cttyref() | |
| 64 | + | |
| 57 | 65 | ! Initialize performance monitoring |
| 58 | 66 | call init_performance_monitoring() |
| 59 | 67 | |