Skip to content

Commit 278041c

Browse files
authored
flatpak: terminate session if Ghostty disconnects from bus (ghostty-org#12427)
This makes sure that if Ghostty crashes, commands spawned are also terminated automatically by the Flatpak Session Helper. The few crashes I got left a lot of background processes, some of them pretty heavy and took awhile to be figured out.
2 parents 67b5783 + 8b8f713 commit 278041c

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

src/os/flatpak.zig

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ pub const FlatpakHostCommand = struct {
3333
@cInclude("gio/gio.h");
3434
@cInclude("gio/gunixfdlist.h");
3535
});
36+
/// Flags for HostCommand method
37+
///
38+
/// Ref: https://docs.flatpak.org/en/latest/libflatpak-api-reference.html#gdbus-method-org-freedesktop-Flatpak-Development.HostCommand
39+
const Flags = packed struct(c_uint) {
40+
/// Clear the environment
41+
clear_env: bool = false,
42+
/// Kill the sandbox when the caller disappears from the session bus
43+
watch_bus: bool = false,
44+
_reserved: std.meta.Int(.unsigned, @bitSizeOf(c_uint) - 2) = 0,
45+
};
3646

3747
/// Argv are the arguments to call on the host with argv[0] being
3848
/// the command to execute.
@@ -366,14 +376,17 @@ pub const FlatpakHostCommand = struct {
366376
const g_cwd = c.g_get_current_dir();
367377
defer c.g_free(g_cwd);
368378

379+
// Terminate session if Ghostty drops off the bus (e.g. due to crashes)
380+
const flags: Flags = .{ .watch_bus = true };
381+
369382
// The params for our RPC call
370383
const params = c.g_variant_new(
371384
"(^ay^aay@a{uh}@a{ss}u)",
372385
@as(*const anyopaque, if (self.cwd) |*cwd| cwd.ptr else g_cwd),
373386
args.ptr,
374387
c.g_variant_builder_end(fd_builder),
375388
c.g_variant_builder_end(env_builder),
376-
@as(c_int, 0),
389+
@as(c_uint, @bitCast(flags)),
377390
);
378391
_ = c.g_variant_ref_sink(params); // take ownership
379392
defer c.g_variant_unref(params);

0 commit comments

Comments
 (0)