summaryrefslogtreecommitdiff
path: root/runtime/client.zig
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/client.zig')
-rw-r--r--runtime/client.zig47
1 files changed, 34 insertions, 13 deletions
diff --git a/runtime/client.zig b/runtime/client.zig
index 43fdf98d..1d348aea 100644
--- a/runtime/client.zig
+++ b/runtime/client.zig
@@ -95,7 +95,7 @@ pub const Start = struct {
s.o.frames = &s.frames;
// Exec'd at an Upgrade (`execAgain`): for the view it had.
if (init.environ_map.get("PARDES_JOIN_VIEW")) |v| s.o.join_view = std.fmt.parseInt(u8, v, 10) catch null;
- s.r = replica.join(s.gpa, &s.joined, dial.path, dial.session, s.o, s.have, &s.why, null) orelse return false;
+ s.r = replica.join(s.gpa, &s.joined, dial.path, dial.session, s.o, s.have, &s.why, null, false) orelse return false;
return true;
}
@@ -175,7 +175,7 @@ pub fn Loop(comptime Frontend: type) type {
frames: *pardes.FrameStats,
pub fn init(fe: *Frontend, s: *Start) !L {
- var l: L = .{ .link = .{ .r = s.r, .fd = s.joined.fd }, .fe = fe, .frames = &s.frames };
+ var l: L = .{ .link = .{ .r = s.r, .fd = s.joined.fd, .far = s.have != null }, .fe = fe, .frames = &s.frames };
try l.link.fromEnv(s.gpa, s.env);
return l;
}
@@ -209,11 +209,23 @@ pub fn Loop(comptime Frontend: type) type {
l.after();
}
if (s.env.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file);
- if (!link.gone) replica.bye(s.joined.fd, .{ .again = s.r.diverged != null or link.lagged != 0, .close = link.close });
- // The link's reader ends with the link, posting as it goes:
- // what it posted is let go until it has (a terminal's queue
- // is bounded: a reader blocked on it would never end).
- _ = libc.shutdown(s.joined.fd, 2);
+ // Its link up and the session's state to take again: its
+ // goodbye and its hello said on it, the session's dump comes
+ // there (`replica.comeBack`); its reader reads on to the welcome,
+ // letting the stream it leaves go. Only a link that is gone
+ // is dialled again.
+ const here = link.backHere() and s.joined.rest.overlong() == null and back: {
+ link.back.store(true, .release);
+ break :back replica.comeBack(s.joined.fd, s.dial.session, s.r.view);
+ };
+ if (!here) {
+ if (!link.gone) replica.bye(s.joined.fd, .{ .again = s.r.diverged != null or link.lagged != 0, .close = link.close });
+ _ = libc.shutdown(s.joined.fd, 2);
+ }
+ // The link's reader ends with the link (or at the welcome),
+ // posting as it goes: what it posted is let go until it has
+ // (a terminal's queue is bounded: a reader blocked on it
+ // would never end).
while (true) {
const ended = done.load(.acquire);
l.fe.drop();
@@ -221,14 +233,23 @@ pub fn Loop(comptime Frontend: type) type {
std.Thread.yield() catch {};
}
reader.join();
- _ = libc.close(s.joined.fd);
- s.joined.rest.deinit(s.gpa);
+ if (!here) {
+ _ = libc.close(s.joined.fd);
+ s.joined.rest.deinit(s.gpa);
+ }
// Its link cut with no word by a session that is there still
// (an Upgrade's exec): joined again, as when told so.
if (link.gone and link.refused == null and link.ended == null and !link.left and replica.cutListed("the session hung up", s.dial.session, s.have != null))
link.refused = std.heap.c_allocator.dupe(u8, replica.upgrading) catch null;
- // Why it joins again is in its notice until a key.
- if (!link.rejoins()) {
+ // A last word goes first, one read after it said it comes
+ // back too (a Detach in it, the session's end). Why it joins
+ // again is in its notice until a key.
+ const goes = link.ended != null or link.left;
+ if (here and goes) {
+ _ = libc.close(s.joined.fd);
+ s.joined.rest.deinit(s.gpa);
+ }
+ if (goes or !link.rejoins()) {
// What it made since it joined, kept for its next start.
if (s.have) |h| h.keep(s.r.core());
s.r.deinit();
@@ -238,7 +259,7 @@ pub fn Loop(comptime Frontend: type) type {
}
// The session again, the core it leaves held meanwhile: its
// blobs are not sent again.
- s.r = replica.join(s.gpa, &s.joined, s.dial.path, s.dial.session, s.o, s.have, &s.why, s.r) orelse {
+ s.r = replica.join(s.gpa, &s.joined, s.dial.path, s.dial.session, s.o, s.have, &s.why, s.r, here) orelse {
if (replica.ended(&s.why)) return .gone;
if (replica.otherBuild(&s.why) and upgrade.replaced()) return .{ .upgrade = s.joined.view };
return .failed;
@@ -272,7 +293,7 @@ pub fn Loop(comptime Frontend: type) type {
/// and its end.
fn read(l: *L, s: *Start, done: *std.atomic.Value(bool)) void {
defer done.store(true, .release);
- replica.readLink(s.gpa, s.joined.fd, &s.joined.rest, l.fe, Frontend.said);
+ replica.readLink(s.gpa, s.joined.fd, &s.joined.rest, &l.link, l.fe, Frontend.said);
l.fe.ended();
}