summaryrefslogtreecommitdiff
path: root/src/Fsys.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 00:59:14 -0300
committerGabriel Schneider <[email protected]>2026-11-17 10:06:26 -0300
commit7f418a6bb3c2d5319d781a61a14aeaf15aff4c89 (patch)
tree6b3324331fe2e4ed195e4864eca8362d4137b9bf /src/Fsys.zig
parentb44d970c121a65e1f9de04fee600a3b495fb6c96 (diff)
downloadcloud9-7f418a6bb3c2d5319d781a61a14aeaf15aff4c89.tar.gz
cloud9-7f418a6bb3c2d5319d781a61a14aeaf15aff4c89.zip
Fsys: a deadline on every call, so a silent server is a timeout
A server that accepted and then said nothing held the caller for ever: Fsys read its socket with no limit, the version in init included, while pardes's own client gives a call 2 s and a write that runs a command 30 s. Fsys gains one field, `timeout` (an Io.Timeout), which init sets to `default_timeout`, 30 s, before it speaks; each call's answer is read by operateTimeout until that long after the call is sent, and past it the call is error.Timeout (the answer may come yet, under a tag the next call does not expect, so the connection is to be closed). `.none` waits for ever: verbs' follow sets it, since following an event file waits as long as the events take; read, ls, ask and the rest keep the deadline and say "no answer in 30000 ms; fs follow waits for ever". Test: a held read nobody feeds, on a 100 ms timeout, is error.Timeout after 90-5000 ms (poll counts whole milliseconds). cloud9's test 108 of 108; serve's "two clients at once" and "repeated connect/disconnect" failed once each at load 13 and passed in the four runs after, as on the base: they do not touch Fsys. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'src/Fsys.zig')
-rw-r--r--src/Fsys.zig49
1 files changed, 44 insertions, 5 deletions
diff --git a/src/Fsys.zig b/src/Fsys.zig
index 224087b..745ebfd 100644
--- a/src/Fsys.zig
+++ b/src/Fsys.zig
@@ -11,6 +11,10 @@
//! fid is let go of without waiting for the answer: an open, a stat or a
//! remove costs one round trip. The buffers are in the struct
//! (2 × `msize`): an `Fsys` stays where it is while it is used.
+//!
+//! A call waits `timeout` for its answer (30 s from `init`), so a server
+//! that accepts and then says nothing is `error.Timeout`, not a caller
+//! blocked for ever.
const std = @import("std");
const Io = std.Io;
const c9 = @import("root.zig");
@@ -37,8 +41,15 @@ pub const Error = error{
/// The server broke the protocol, or a request could not be made
/// (a name with a slash or longer than a message).
Botch,
+ /// No answer within `timeout`. The answer may come yet, under a tag
+ /// the next call does not expect: `close` the connection.
+ Timeout,
};
+/// What `init` sets `timeout` to: pardes's client waits this long for a
+/// write that runs a command.
+pub const default_timeout: Io.Timeout = .{ .duration = .{ .raw = .fromSeconds(30), .clock = .awake } };
+
io: Io,
stream: Io.net.Stream,
cl: Client,
@@ -54,6 +65,10 @@ walk_tag: u16,
walk_asked: usize,
words: [fs.errmax]u8,
words_len: usize,
+/// How long a call waits for its answer, from when it is made: `init`
+/// sets `default_timeout`, before it speaks; `.none` waits for ever, as
+/// following an event file wants.
+timeout: Io.Timeout,
/// Speaks 9P on `stream`, which is this one's from here (`close`), on a
/// refusal too: the version, then an attach as `uname` to the tree's root.
@@ -66,6 +81,7 @@ pub fn init(f: *Fsys, io: Io, stream: Io.net.Stream, uname: []const u8) Error!vo
f.walk_tag = wire.notag;
f.walk_asked = 0;
f.words_len = 0;
+ f.timeout = default_timeout;
errdefer f.close();
const v = (try f.rpc(.{ .version = .{ .msize = msize } })).version;
if (!std.mem.eql(u8, v.version, "9P2000")) return f.refuse("the server does not speak 9P2000");
@@ -288,8 +304,9 @@ fn rpc(f: *Fsys, req: Client.Request) Error!Client.Result {
return error.Botch;
};
try f.ship();
+ const deadline = f.timeout.toDeadline(f.io);
while (true) {
- const done = try f.next();
+ const done = try f.next(deadline);
if (f.walk_tag != wire.notag and done.tag == f.walk_tag) {
f.walk_tag = wire.notag;
switch (done.result) {
@@ -314,9 +331,9 @@ fn rpc(f: *Fsys, req: Client.Request) Error!Client.Result {
}
}
-/// The next answer, read from the socket as it takes; its slices are
-/// good until the next.
-fn next(f: *Fsys) Error!Client.Done {
+/// The next answer, read from the socket until `deadline`; its slices
+/// are good until the next.
+fn next(f: *Fsys, deadline: Io.Timeout) Error!Client.Done {
while (true) {
if (f.cl.take()) |done| return done;
if (f.cl.dead) return error.Botch;
@@ -325,7 +342,10 @@ fn next(f: *Fsys) Error!Client.Done {
// (what `push` would copy them to).
var room = [1][]u8{f.cl.in[f.cl.in_len..]};
if (room[0].len == 0) return error.Botch;
- const n = f.stream.read(f.io, &room) catch return error.Hangup;
+ const n = ((f.io.operateTimeout(.{ .net_read = .{ .socket_handle = f.stream.socket.handle, .data = &room } }, deadline) catch |err| switch (err) {
+ error.Timeout => return error.Timeout,
+ else => return error.Hangup,
+ }).net_read catch return error.Hangup).data_len;
if (n == 0) return error.Hangup;
f.cl.in_len += n;
}
@@ -517,6 +537,25 @@ test "fsys: a server that goes away mid-read is a hangup" {
try feeder.await(testing.io);
}
+test "fsys: a server that says nothing is a timeout, not a caller held for ever" {
+ const t = try testing.allocator.create(TestTree);
+ defer testing.allocator.destroy(t);
+ try t.start();
+ defer t.stop();
+ const f = try dialed(t, .unix);
+ defer hangUp(f);
+ try testing.expectEqual(default_timeout, f.timeout);
+ // `event`'s reads are held until fed, and nothing feeds this one.
+ f.timeout = .{ .duration = .{ .raw = .fromMilliseconds(100), .clock = .awake } };
+ var store: [64]u8 = undefined;
+ var out: Io.Writer = .fixed(&store);
+ const began = Io.Clock.Timestamp.now(testing.io, .awake);
+ try testing.expectError(error.Timeout, f.readFile("event", &out));
+ const waited = began.durationTo(Io.Clock.Timestamp.now(testing.io, .awake)).raw.toMilliseconds();
+ // poll(2) counts whole milliseconds, and may wake one early.
+ try testing.expect(waited >= 90 and waited < 5000);
+}
+
test "fsys: create, write, remove" {
const t = try testing.allocator.create(TestTree);
defer testing.allocator.destroy(t);