From 2a7137c644eb955d39ce3f07d75bf9c8fd9c932e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 28 Sep 2026 17:16:41 -0300 Subject: 9ns: a failed send leaves the replies already in the socket to be read A server that answers and then hangs up (pardes answering a Restore, then cutting every connection) could lose that answer: if another call's send found the socket closed first, die() failed every waiter at once, the one whose reply sat unread in the socket included. Now a failed send fails only its own call and stops further sends; the reader goes on to EOF (or for at most a second) and the session dies then. A test has the fake server answer a write and hang up before the reply is read. Co-Authored-By: Claude Opus 5.5 --- 9ns/src/nine.zig | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 95 insertions(+), 6 deletions(-) (limited to '9ns') diff --git a/9ns/src/nine.zig b/9ns/src/nine.zig index aefc75a..8b0a867 100644 --- a/9ns/src/nine.zig +++ b/9ns/src/nine.zig @@ -111,6 +111,11 @@ pub const Session = struct { /// The connection itself failed (EOF, a socket error, a reply that /// breaks the protocol): every call answers this. dead: ?Error = null, + /// A send failed: the server hung up, perhaps just after answering. No + /// more is sent, but the replies already in the socket are still read, + /// to EOF or until `drain_until`, before the session dies of this. + write_dead: ?Error = null, + drain_until: i64 = 0, // -- multiplexing (devmnt's Mnt: `queue`, `rip`) ------------------------------------ @@ -358,6 +363,7 @@ pub const Session = struct { while (true) { if (s.wedged) return error.Closed; if (s.dead) |e| return e; + if (s.write_dead) |e| return e; // Lock order: wlock, then mutex. s.mutex.unlock(io); s.wlock.lockUncancelable(io); @@ -380,7 +386,7 @@ pub const Session = struct { }; w.tag = tag; if (req == .version) s.versioning = w else s.waiting[tag] = w; - try s.send(c); + try s.send(); return; } } @@ -389,20 +395,31 @@ pub const Session = struct { /// with `mutex` and `wlock` held; returns with `mutex` held and `wlock` /// released. The socket write happens with `mutex` released, so the /// reader keeps reading while it blocks. - fn send(s: *Session, c: *Caller) Error!void { + fn send(s: *Session) Error!void { const io = lockIo(); s.mutex.unlock(io); const sent = writeOut(s); s.wlock.unlock(io); s.mutex.lockUncancelable(io); sent catch |e| { - s.die(e, c); + // This call's request did not go out, so it fails; the calls + // already sent may have their replies waiting to be read (a + // server that answers and then hangs up), so the session is not + // dead until the reader reaches the end of what came in. + if (s.write_dead == null) { + s.write_dead = e; + s.drain_until = nowMs() + drain_ms; + } return e; }; } const Until = enum { reply, tag }; + // ponytail: a fixed drain window; a server that hangs up closes the + // socket, so EOF normally ends the drain long before this. + const drain_ms = 1000; + /// Waits until `w` is answered (`.reply`: its reply or its Rflush is in) /// or a tag was freed (`.tag`). Takes the reading baton whenever nobody /// holds it (devmnt's `mountio` gate), reads for everyone while holding @@ -457,7 +474,11 @@ pub const Session = struct { s.die(error.Protocol, c); continue; } - const grace: i32 = if (flush_deadline) |d| @intCast(@max(d - nowMs(), 0)) else -1; + var grace: i32 = if (flush_deadline) |d| @intCast(@max(d - nowMs(), 0)) else -1; + if (s.write_dead != null) { + const left: i32 = @intCast(@max(s.drain_until - nowMs(), 0)); + grace = if (grace < 0) left else @min(grace, left); + } s.mutex.unlock(io); var got: usize = 0; const ready = c.wait(if (reading) s.fd else -1, grace); @@ -496,6 +517,11 @@ pub const Session = struct { } }, .timeout => { + // The drain after a failed send is over: nothing more is coming. + if (s.write_dead) |e| if (nowMs() >= s.drain_until) { + s.die(e, c); + continue; + }; s.wedge(c); return error.Interrupted; }, @@ -524,7 +550,7 @@ pub const Session = struct { }; w.flush_tag = tag; s.waiting[tag] = w; - try s.send(w.caller); + try s.send(); } }; @@ -1296,6 +1322,11 @@ pub const FakeServer = struct { on_hang_inject: ?*PipeInterrupt = null, /// Delay before every Rread, so a test can be sure the client is waiting. read_delay_ns: u64 = 0, + /// A Twrite is answered, and then the server hangs up (pardes answering + /// a Restore and cutting every connection). + hangup_after_write: bool = false, + /// Set once the server has closed its end. + gone: std.atomic.Value(bool) = .init(false), pub const file_qid: cloud9.Qid = .{ .type = 0, .version = 3, .path = 0x1234 }; pub const dir_qid: cloud9.Qid = .{ .type = cloud9.qtdir, .version = 1, .path = 0x1 }; @@ -1303,6 +1334,7 @@ pub const FakeServer = struct { pub fn run(fs: *FakeServer) void { fs.loop() catch |e| std.debug.print("fake server: {s}\n", .{@errorName(e)}); _ = linux.close(fs.fd); + fs.gone.store(true, .release); } fn loop(fs: *FakeServer) !void { @@ -1378,7 +1410,18 @@ pub const FakeServer = struct { try srv.reply(tag, .{ .rread = .{ .data = fs.readData(&data, m.fid, m.offset, m.count) } }); } }, - .twrite => |m| try srv.reply(tag, .{ .rwrite = .{ .count = @intCast(m.data.len) } }), + .twrite => |m| { + try srv.reply(tag, .{ .rwrite = .{ .count = @intCast(m.data.len) } }); + if (fs.hangup_after_write) { + while (srv.output().len != 0) { + const o = srv.output(); + const rc = linux.write(fs.fd, o.ptr, o.len); + if (linux.errno(rc) != .SUCCESS) return error.Write; + srv.wrote(rc); + } + return; + } + }, .tclunk => { _ = fs.clunks.fetchAdd(1, .seq_cst); try srv.reply(tag, .rclunk); @@ -1853,6 +1896,52 @@ test "multiplexing: a parked read holds up only its own caller; its interrupt fl try testing.expectEqual(@as(usize, 0), s.session.client.pending()); } +test "a server that answers and then hangs up: the reply is read, though another call's send failed first" { + // pardes answers a Restore's write and then cuts every connection. The + // write's Rwrite is in the socket when another caller's request finds + // the socket closed; that failed send must not lose the reply. + const ign = linux.Sigaction{ .handler = .{ .handler = linux.SIG.IGN }, .mask = linux.sigemptyset(), .flags = 0 }; + std.posix.sigaction(.PIPE, &ign, null); + var fs: FakeServer = .{ .fd = -1, .msize = 8192, .file_len = 50, .hangup_after_write = true }; + var pair = try fs.start(); + defer pair.close(); + const s = pair.session; + const io = lockIo(); + const writer = try pair.newCaller(); + defer FakeServer.Pair.freeCaller(writer); + const wfid = try pair.caller.clone(1); + _ = try pair.caller.open(wfid, cloud9.owrite); + + // The write goes out, and nobody reads its answer yet. + var w: Waiter = .{ .caller = writer }; + s.mutex.lockUncancelable(io); + s.transmit(&w, .{ .write = .{ .fid = wfid, .offset = 0, .data = "Restore x\n" } }) catch |e| { + s.leave(&w); + s.mutex.unlock(io); + return e; + }; + s.mutex.unlock(io); + // The server answers and hangs up; then another call's send fails. + const Gone = struct { + fs: *FakeServer, + fn check(g: @This()) bool { + return g.fs.gone.load(.acquire); + } + }; + try testing.expect(eventually(Gone{ .fs = &fs })); + try testing.expectError(error.Closed, pair.caller.stat(0)); + + // The write's reply was already in: it is read, not failed. + s.mutex.lockUncancelable(io); + const got = s.await(&w, .reply); + s.leave(&w); + s.mutex.unlock(io); + try got; + try testing.expectEqual(@as(u32, "Restore x\n".len), w.result.write); + // And then the session is over. + try testing.expectError(error.Closed, pair.caller.stat(0)); +} + test "multiplexing: with every tag parked, a call waits for a tag instead of failing" { // max_tags reads park (the client's whole request window); the next call // finds no tag. Interrupting one parked read sends its Tflush on the tag -- cgit v1.3