fortrangoingonforty/fortsh / ad653e8

Browse files

set S_CTTYREF on macOS to prevent PTY output loss — workaround for XNU kernel bug (pexpect #662)

Authored by espadonne
SHA
ad653e81e4a61f804828a08feaf84bf46f351655
Parents
747a4ef
Tree
e1e585e

2 changed files

StatusFile+-
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 @@
55
 #include <sys/stat.h>
66
 #include <sys/types.h>
77
 
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
+
820
 // Wrapper for open() that takes mode as a separate parameter
921
 // This works around a bug in Fortran's C binding where mode_t is not passed correctly
1022
 int fortsh_open(const char *pathname, int flags, int mode) {
src/fortsh.f90modified
@@ -54,6 +54,14 @@ program fortran_shell
5454
   character(len=16) :: col_str_buf
5555
   character(len=2048) :: embedded_prompt
5656
 
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
+
5765
   ! Initialize performance monitoring
5866
   call init_performance_monitoring()
5967