Skip to content

Commit

Permalink
Ignore SIGPIPE
Browse files Browse the repository at this point in the history
Fixes #5359

The comments explain what's going on. Longer term we should adjust our
termio/exec to avoid the SIGPIPE but its still possible (i.e. that
thread crashes) to happen so we should be robust to it.
  • Loading branch information
mitchellh committed Jan 24, 2025
1 parent 47ff4c9 commit f73cae0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/global.zig
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ pub const GlobalState = struct {
}
}

// Setup our signal handlers before logging
initSignals();

// Output some debug information right away
std.log.info("ghostty version={s}", .{build_config.version_string});
std.log.info("ghostty build optimize={s}", .{build_config.mode_string});
Expand Down Expand Up @@ -175,6 +178,28 @@ pub const GlobalState = struct {
_ = value.deinit();
}
}

fn initSignals() void {
// Only posix systems.
if (comptime builtin.os.tag == .windows) return;

const p = std.posix;

var sa: p.Sigaction = .{
.handler = .{ .handler = p.SIG.IGN },
.mask = p.empty_sigset,
.flags = 0,
};

// We ignore SIGPIPE because it is a common signal we may get
// due to how we implement termio. When a terminal is closed we
// often write to a broken pipe to exit the read thread. This should
// be fixed one day but for now this helps make this a bit more
// robust.
p.sigaction(p.SIG.PIPE, &sa, null) catch |err| {
std.log.warn("failed to ignore SIGPIPE err={}", .{err});
};
}
};

/// Maintains the Unix resource limits that we set for our process. This
Expand Down
1 change: 1 addition & 0 deletions src/pty.zig
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ const PosixPty = struct {
try posix.sigaction(posix.SIG.HUP, &sa, null);
try posix.sigaction(posix.SIG.ILL, &sa, null);
try posix.sigaction(posix.SIG.INT, &sa, null);
try posix.sigaction(posix.SIG.PIPE, &sa, null);
try posix.sigaction(posix.SIG.SEGV, &sa, null);
try posix.sigaction(posix.SIG.TRAP, &sa, null);
try posix.sigaction(posix.SIG.TERM, &sa, null);
Expand Down

0 comments on commit f73cae0

Please sign in to comment.