From e10eb8a2fd3b0e1cb325c353473a65a8bb04403a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sat, 27 Sep 2025 12:34:57 -0700 Subject: [PATCH] build: limit cpu affinity to 32 cpus on Linux (#8925) Related to #8924 Zig currenly has a bug where it crashes when compiling Ghostty on systems with more than 32 cpus (See the linked issue for the gory details). As a temporary hack, use `sched_setaffinity` on Linux systems to limit the compile to the first 32 cores. Note that this affects the build only. The resulting Ghostty executable is not limited in any way. This is a more general fix than wrapping the Zig compiler with `taskset`. First of all, it requires no action from the user or packagers. Second, it will be easier for us to remove once the upstream Zig bug is fixed. --- build.zig | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/build.zig b/build.zig index 38cfd0e56..a2fb25e96 100644 --- a/build.zig +++ b/build.zig @@ -8,7 +8,13 @@ comptime { } pub fn build(b: *std.Build) !void { - // This defines all the available build options (e.g. `-D`). + // Works around a Zig but still present in 0.15.1. Remove when fixed. + // https://github.com/ghostty-org/ghostty/issues/8924 + try limitCoresForZigBug(); + + // This defines all the available build options (e.g. `-D`). If you + // want to know what options are available, you can run `--help` or + // you can read `src/build/Config.zig`. const config = try buildpkg.Config.init(b); const test_filter = b.option( []const u8, @@ -258,3 +264,13 @@ pub fn build(b: *std.Build) !void { try translations_step.addError("cannot update translations when i18n is disabled", .{}); } } + +// WARNING: Remove this when https://github.com/ghostty-org/ghostty/issues/8924 is resolved! +// Limit ourselves to 32 cpus on Linux because of an upstream Zig bug. +fn limitCoresForZigBug() !void { + if (comptime builtin.os.tag != .linux) return; + const pid = std.os.linux.getpid(); + var set: std.bit_set.ArrayBitSet(usize, std.os.linux.CPU_SETSIZE * 8) = .initEmpty(); + for (0..32) |cpu| set.set(cpu); + try std.os.linux.sched_setaffinity(pid, &set.masks); +}