Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const Browser = @import("browser/browser.zig").Browser;
const build_config = @import("build_config");
const parser = @import("browser/netsurf.zig");

var _app: ?*App = null;
var _server: ?Server = null;

pub fn main() !void {
// allocator
// - in Debug mode we use the General Purpose Allocator to detect memory leaks
Expand All @@ -51,6 +54,34 @@ pub fn main() !void {
};
}

// Handle app shutdown gracefuly on signals.
fn shutdown() void {
const sigaction: std.posix.Sigaction = .{
.handler = .{
.handler = struct {
pub fn handler(_: c_int) callconv(.c) void {
// Shutdown service gracefuly.
if (_server) |server| {
server.deinit();
}
if (_app) |app| {
app.deinit();
}
std.posix.exit(0);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's worth it, we can create a global ?App and ?Server, and try to shutdown them cleanly.

The one argument that I can make for going through the trouble is that, on deinit, the app flushes the telemetry queue. Can also do it in a separate PR.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point, it would be nice, I can try that.

}
}.handler,
},
.mask = std.posix.empty_sigset,
.flags = 0,
};
// Exit the program on SIGINT signal. When running the browser in a Docker
// container, sending a CTRL-C (SIGINT) signal is catched but doesn't exit
// the program. Here we force exiting on SIGINT.
std.posix.sigaction(std.posix.SIG.INT, &sigaction, null);
std.posix.sigaction(std.posix.SIG.TERM, &sigaction, null);
std.posix.sigaction(std.posix.SIG.QUIT, &sigaction, null);
}

fn run(alloc: Allocator) !void {
var args_arena = std.heap.ArenaAllocator.init(alloc);
defer args_arena.deinit();
Expand Down Expand Up @@ -81,7 +112,8 @@ fn run(alloc: Allocator) !void {
const platform = try Platform.init();
defer platform.deinit();

var app = try App.init(alloc, .{
// _app is global to handle graceful shutdown.
_app = try App.init(alloc, .{
.run_mode = args.mode,
.platform = &platform,
.http_proxy = args.httpProxy(),
Expand All @@ -92,6 +124,7 @@ fn run(alloc: Allocator) !void {
.http_max_host_open = args.httpMaxHostOpen(),
.http_max_concurrent = args.httpMaxConcurrent(),
});
const app = _app.?;
defer app.deinit();
app.telemetry.record(.{ .run = {} });

Expand All @@ -103,7 +136,9 @@ fn run(alloc: Allocator) !void {
return args.printUsageAndExit(false);
};

var server = try Server.init(app, address);
// _server is global to handle graceful shutdown.
_server = try Server.init(app, address);
const server = &_server.?;
defer server.deinit();

server.run(address, opts.timeout) catch |err| {
Expand Down