fix timeout in ZCS receiveMessage (#1264)

This commit is contained in:
Techatrix 2023-06-24 21:17:56 +00:00 committed by GitHub
parent 0b1fc7eb6a
commit 4dc7652aa2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 29 deletions

View File

@ -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;
}

View File

@ -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')) };
},

View File

@ -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*);