return from main instead of calling exit (#894)

This commit is contained in:
Techatrix 2023-01-09 04:13:02 +00:00 committed by GitHub
parent 48288e02a9
commit 68790c73a7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 11 deletions

View File

@ -49,6 +49,10 @@ status: enum {
initialized, initialized,
/// the server has been shutdown and can't handle any more requests /// the server has been shutdown and can't handle any more requests
shutdown, shutdown,
/// the server is received a `exit` notification and has been shutdown
exiting_success,
/// the server is received a `exit` notification but has not been shutdown
exiting_failure,
}, },
// Code was based off of https://github.com/andersfr/zig-lsp/blob/master/server.zig // Code was based off of https://github.com/andersfr/zig-lsp/blob/master/server.zig
@ -1827,22 +1831,18 @@ fn initializedHandler(server: *Server, notification: types.InitializedParams) Er
} }
fn shutdownHandler(server: *Server, _: void) Error!?void { fn shutdownHandler(server: *Server, _: void) Error!?void {
defer server.status = .shutdown;
if (server.status != .initialized) return error.InvalidRequest; // received a shutdown request but the server is not initialized! if (server.status != .initialized) return error.InvalidRequest; // received a shutdown request but the server is not initialized!
// Technically we should deinitialize first and send possible errors to the client
return null; return null;
} }
fn exitHandler(server: *Server, _: void) noreturn { fn exitHandler(server: *Server, _: void) Error!void {
log.info("Server exiting...", .{}); server.status = switch (server.status) {
// Technically we should deinitialize first and send possible errors to the client .initialized => .exiting_failure,
.shutdown => .exiting_success,
const error_code: u8 = switch (server.status) { else => unreachable,
.uninitialized, .shutdown => 0,
else => 1,
}; };
std.os.exit(error_code);
} }
fn cancelRequestHandler(server: *Server, request: types.CancelParams) Error!void { fn cancelRequestHandler(server: *Server, request: types.CancelParams) Error!void {
@ -3007,12 +3007,14 @@ fn processMessage(server: *Server, message: Message) Error!void {
return error.InvalidRequest; // server received a request after shutdown! return error.InvalidRequest; // server received a request after shutdown!
}, },
.exiting_success,
.exiting_failure, => unreachable,
} }
const start_time = std.time.milliTimestamp(); const start_time = std.time.milliTimestamp();
defer { defer {
// makes `zig build test` look nice // makes `zig build test` look nice
if (!zig_builtin.is_test and !std.mem.eql(u8, method, "shutdown")) { if (!zig_builtin.is_test) {
const end_time = std.time.milliTimestamp(); const end_time = std.time.milliTimestamp();
log.debug("Took {}ms to process method {s}", .{ end_time - start_time, method }); log.debug("Took {}ms to process method {s}", .{ end_time - start_time, method });
} }

View File

@ -76,6 +76,8 @@ fn loop(
} }
server.processJsonRpc(&arena, json_message); server.processJsonRpc(&arena, json_message);
if(server.status == .exiting_success or server.status == .exiting_failure) return;
} }
} }
@ -387,4 +389,8 @@ pub fn main() !void {
defer server.deinit(); defer server.deinit();
try loop(&server, record_file, replay_file); try loop(&server, record_file, replay_file);
if (server.status == .exiting_failure) {
std.process.exit(1);
}
} }