fix(ui): prevent Dart event loop freezing by switching master PTY fd to non-blocking mode via fcntl

This commit is contained in:
2026-07-16 23:19:24 +09:00
parent 7781e797aa
commit a6e4dc97a4
3 changed files with 11 additions and 0 deletions
@@ -50,6 +50,9 @@ typedef _waitpid_dart = int Function(int pid, ffi.Pointer<ffi.Int32> status, int
typedef _chdir_c = ffi.Int32 Function(ffi.Pointer<ffi.Char> path); typedef _chdir_c = ffi.Int32 Function(ffi.Pointer<ffi.Char> path);
typedef _chdir_dart = int Function(ffi.Pointer<ffi.Char> path); typedef _chdir_dart = int Function(ffi.Pointer<ffi.Char> path);
typedef _fcntl_c = ffi.Int32 Function(ffi.Int32 fd, ffi.Int32 cmd, ffi.Int32 arg);
typedef _fcntl_dart = int Function(int fd, int cmd, int arg);
base class Winsize extends ffi.Struct { base class Winsize extends ffi.Struct {
@ffi.Uint16() @ffi.Uint16()
external int ws_row; external int ws_row;
@@ -97,6 +100,7 @@ class PtySession {
final execvp = libc.lookupFunction<_execvp_c, _execvp_dart>('execvp'); final execvp = libc.lookupFunction<_execvp_c, _execvp_dart>('execvp');
final ioctl = libc.lookupFunction<_ioctl_c, _ioctl_dart>('ioctl'); final ioctl = libc.lookupFunction<_ioctl_c, _ioctl_dart>('ioctl');
final chdir = libc.lookupFunction<_chdir_c, _chdir_dart>('chdir'); final chdir = libc.lookupFunction<_chdir_c, _chdir_dart>('chdir');
final fcntl = libc.lookupFunction<_fcntl_c, _fcntl_dart>('fcntl');
// 1. Prepare master PTY // 1. Prepare master PTY
final masterFd = posixOpenpt(2 | 0x00000400); final masterFd = posixOpenpt(2 | 0x00000400);
@@ -121,6 +125,13 @@ class PtySession {
} }
final slaveName = slavePtr.cast<Utf8>().toDartString(); final slaveName = slavePtr.cast<Utf8>().toDartString();
// Set master fd to non-blocking mode to prevent freezing the Dart event loop (§6.7)
// F_GETFL = 3, F_SETFL = 4, O_NONBLOCK = 2048 (0x800)
final flags = fcntl(masterFd, 3, 0);
if (flags >= 0) {
fcntl(masterFd, 4, flags | 2048);
}
// 2. Resolve environment, isolating nested TMUX contexts (§6.7) // 2. Resolve environment, isolating nested TMUX contexts (§6.7)
final env = Map<String, String>.from(environment ?? Platform.environment); final env = Map<String, String>.from(environment ?? Platform.environment);
env.remove('TMUX'); env.remove('TMUX');