From 4dc7652aa2a4dde38c9cb2aea64d85749f915ab0 Mon Sep 17 00:00:00 2001 From: Techatrix <19954306+Techatrix@users.noreply.github.com> Date: Sat, 24 Jun 2023 21:17:56 +0000 Subject: [PATCH] fix timeout in ZCS receiveMessage (#1264) --- src/ZigCompileServer.zig | 39 +++++++++++++---------------- src/translate_c.zig | 6 ++--- tests/language_features/cimport.zig | 3 --- 3 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/ZigCompileServer.zig b/src/ZigCompileServer.zig index 4709336..96f6988 100644 --- a/src/ZigCompileServer.zig +++ b/src/ZigCompileServer.zig @@ -32,32 +32,27 @@ pub fn receiveMessage(client: *Client) !InMessage.Header { const Header = InMessage.Header; const fifo = client.pooler.fifo(.in); - while (try client.pooler.poll()) { - const buf = fifo.readableSlice(0); - assert(fifo.readableLength() == buf.len); - if (buf.len >= @sizeOf(Header)) { - // workaround for https://github.com/ziglang/zig/issues/14904 + var first_run = true; + var header: ?Header = null; + while (first_run or try client.pooler.poll()) { + first_run = false; + + if (header == null) { + if (fifo.readableLength() < @sizeOf(Header)) continue; + const buf = fifo.readableSlice(0); const bytes_len = bswap_and_workaround_u32(buf[4..][0..4]); const tag = bswap_and_workaround_tag(buf[0..][0..4]); - - if (buf.len - @sizeOf(Header) >= bytes_len) { - fifo.discard(@sizeOf(Header)); - return .{ - .tag = tag, - .bytes_len = bytes_len, - }; - } else { - const needed = bytes_len - (buf.len - @sizeOf(Header)); - const write_buffer = try fifo.writableWithSize(needed); - const amt = try client.in.readAll(write_buffer); - fifo.update(amt); - continue; - } + header = Header{ + .tag = tag, + .bytes_len = bytes_len, + }; + fifo.discard(@sizeOf(Header)); } - const write_buffer = try fifo.writableWithSize(256); - const amt = try client.in.read(write_buffer); - fifo.update(amt); + if (header) |h| { + if (fifo.readableLength() < h.bytes_len) continue; + return h; + } } return error.Timeout; } diff --git a/src/translate_c.zig b/src/translate_c.zig index 45fe112..9f9150d 100644 --- a/src/translate_c.zig +++ b/src/translate_c.zig @@ -236,12 +236,10 @@ pub fn translate( const body_size = @sizeOf(std.zig.Server.Message.EmitBinPath); if (header.bytes_len <= body_size) return error.InvalidResponse; - const trailing_size = header.bytes_len - body_size; - _ = try zcs.receiveEmitBinPath(); - const result_path = try zcs.receiveBytes(allocator, trailing_size); - defer allocator.free(result_path); + const trailing_size = header.bytes_len - body_size; + const result_path = zcs.pooler.fifo(.in).readableSliceOfLen(trailing_size); return Result{ .success = try URI.fromPath(allocator, std.mem.sliceTo(result_path, '\n')) }; }, diff --git a/tests/language_features/cimport.zig b/tests/language_features/cimport.zig index 6c0db4d..94da1b4 100644 --- a/tests/language_features/cimport.zig +++ b/tests/language_features/cimport.zig @@ -9,9 +9,6 @@ const translate_c = zls.translate_c; const allocator: std.mem.Allocator = std.testing.allocator; test "zig compile server - translate c" { - // FIXME: Disabled due to https://github.com/zigtools/zls/issues/1252 - if (@import("builtin").os.tag == .windows) return error.SkipZigTest; - var result1 = try testTranslate( \\void foo(int); \\void bar(float*);