console: an end stays pending until its writes land; admission cleanup within OpenWait; ended ids pruned (rootfs plan 6 task 10 fix round 1)
Astra's three P2s: - A failed grant DELETE is returned: endWrites clears the marker, deletes the grant, then writes the interactive-end row once; a failure keeps the end in endPending for the sweep. - The admission step's OpenWait deadline covers its grant cleanup; a grant not deleted in time is pended grant-only (no row) and the sweep deletes it, so the reservation and the unit never outlive OpenWait. - hub.ended keeps an id until its PickupBy and is pruned on each end and sweep pass; activeSession is the box door's lookup (ended, pruned and unknown ids alike are 404). Record-write census: admitSession and endWrites re-pinned. Root-read census unchanged (seal f02fd204).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
internal/console/apiinteractive.go+47 −22internal/console/apiinteractive_lifetimes_test.go+2 −2internal/console/apiinteractive_test.go+200 −1internal/console/interactivehub.go+87 −19internal/console/runsweep.go+1 −1internal/provision/rootfs_apps_6_census_test.go+6 −2
Intent
<cross-session-message from="uds:/run/user/1001/cc-socks/3180344.sock" from-name="platform-dd" from-mode="bypass"> platform-61: go. Nothing of mine is about to merge. The durability branch is design and plan only, and nothing of mine is in flight on dev. If v0.32.0 didn't already include a67f377 (region HA dead-holder backoff), v0.32.1 will. Either way it's dormant unless a born-HA host exists. On dev those are hapair-a1 and hacheck-ha1, so please leave them alone. </cross-session-message>
<task-notification> <task-id>buikt17u8</task-id> <tool-use-id>toolu_019utLdFzH6x7d9MqEoXKAjH</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/ba9fa944-a429-4788-9b3c-f2b2f42617f3/tasks/buikt17u8.output</output-file> <status>completed</status> <summary>Background command "Run Astra review of Plan 6 Task 10" completed (exit code 0)</summary> </task-notification>
Attributed to this commit: 17.4 g CO₂e (2,181,318 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on rootfs-apps-6-interactive, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/internal/console/apiinteractive.go b/internal/console/apiinteractive.go| index da41dd2cb..6693325b3 100644 |
| --- a/internal/console/apiinteractive.go |
| +++ b/internal/console/apiinteractive.go |
| @@ -225,7 +225,10 @@ func (s *Server) serveInteractive(rctx context.Context, ws *websocket.Conn, d in |
| }() |
| var refusal *interactive.End |
| err = s.lifetimesStep(ctx, true, func(sctx context.Context) error { |
| - sctx, cancel := context.WithTimeout(sctx, interactive.OpenWait) |
| + // The admission's whole bound: its writes and any cleanup of them |
| + // (fix round 1, P2 2). The reservation and the unit are released |
| + // when this returns. |
| + sctx, cancel := context.WithTimeout(sctx, h.openWait()) |
| defer cancel() |
| refusal = s.admitSession(sctx, sess, token) |
| activated = refusal == nil |
| @@ -271,7 +274,7 @@ func receiveOpen(ws *websocket.Conn, wait time.Duration) (interactive.Open, bool |
| // the manifest of the version the record's channel serves. It is used only |
| // for the audit row and this check; the box gets the Open as sent. |
| func (s *Server) resolveConsole(ctx context.Context, d interactiveDoor) ([]string, string, *interactive.End) { |
| - ctx, cancel := context.WithTimeout(ctx, interactive.OpenWait) |
| + ctx, cancel := context.WithTimeout(ctx, s.interactive.openWait()) |
| defer cancel() |
| none := &interactive.End{Detail: fmt.Sprintf(interactiveNoConsoleMsg, d.host, d.app, d.host)} |
| failed := &interactive.End{Detail: interactiveFailedMsg} |
| @@ -317,7 +320,7 @@ func (s *Server) admitSession(ctx context.Context, sess *hubSession, token strin |
| } |
| if err := s.putGrant(ctx, sess, wire); err != nil { |
| log.Printf("console: api interactive %s/%s/%s: write the grant %s: %v", sess.acct, sess.app, sess.host, sess.id, err) |
| - s.deleteGrant(ctx, sess) // the write may have landed |
| + s.cleanupGrant(ctx, sess) // the write may have landed |
| return failed |
| } |
| _, err = provision.SetRecordInteractive(ctx, s.cfg.Releases, sess.acct, sess.app, sess.host, |
| @@ -333,7 +336,7 @@ func (s *Server) admitSession(ctx context.Context, sess *hubSession, token strin |
| // A failure whose outcome is unknown may have landed the marker; with |
| // the id released it is dead (not live), so the next admission |
| // replaces it and the sweep clears it. |
| - s.deleteGrant(ctx, sess) |
| + s.cleanupGrant(ctx, sess) |
| return admissionRefusal(err, sess) |
| } |
| if h.afterAdmit != nil { |
| @@ -434,33 +437,45 @@ func (s *Server) endSession(ws *websocket.Conn, sess *hubSession, e interactive. |
| // endWrites are an end's writes: the marker cleared (InteractiveClear: a |
| // marker already gone, or replaced by a newer session, is left as it is), |
| -// the grant deleted and the interactive-end row. A record write that fails |
| -// is returned before anything else is done, so a retry repeats all three. |
| +// the grant deleted, then the interactive-end row. A failed write is |
| +// returned before the row, so the end stays pending and a retry repeats |
| +// what is left (an absent marker and an absent grant are each done): the |
| +// row is written once, after every write has landed. |
| func (s *Server) endWrites(ctx context.Context, sess *hubSession, e interactive.End) error { |
| _, err := provision.SetRecordInteractive(ctx, s.cfg.Releases, sess.acct, sess.app, sess.host, provision.InteractiveClear(sess.id)) |
| if err != nil && !errors.Is(err, provision.ErrInteractiveConflict) && !errors.Is(err, os.ErrNotExist) { |
| return err |
| } |
| - s.deleteGrant(ctx, sess) |
| + if err := s.deleteGrant(ctx, sess); err != nil { |
| + return err |
| + } |
| s.auditActivity(sess.account, sess.app, sess.by, Activity{Action: "interactive-end", TargetType: "instance", Target: sess.host, |
| Detail: interactiveEndDetail(sess.id, e)}) |
| return nil |
| } |
| -// finishPendingInteractiveEnds makes the writes of every end the gate |
| -// refused, under the sweep pass's own unit, and ends each id that lands. |
| -func (s *Server) finishPendingInteractiveEnds(ctx context.Context) { |
| +// finishPendingInteractiveEnds makes the writes of every end that did not |
| +// finish, under the sweep pass's own unit and bound, ends each that lands, |
| +// and prunes the ended ids whose pickup window passed by now. |
| +func (s *Server) finishPendingInteractiveEnds(ctx context.Context, now time.Time) { |
| if s.interactive == nil { |
| return |
| } |
| - for sess, e := range s.interactive.pending() { |
| - if err := s.endWrites(ctx, sess, e); err != nil { |
| - log.Printf("console: run sweep: interactive end %s/%s/%s %s: %v", sess.acct, sess.app, sess.host, sess.id, err) |
| - jobFailed(ctx, fmt.Errorf("run sweep: interactive end %s: %w", sess.id, err)) |
| + for _, w := range s.interactive.pending() { |
| + var err error |
| + if w.grantOnly { |
| + err = s.deleteGrant(ctx, w.sess) |
| + } else { |
| + err = s.endWrites(ctx, w.sess, w.end) |
| + } |
| + if err != nil { |
| + log.Printf("console: run sweep: interactive end %s/%s/%s %s: %v", w.sess.acct, w.sess.app, w.sess.host, w.sess.id, err) |
| + jobFailed(ctx, fmt.Errorf("run sweep: interactive end %s: %w", w.sess.id, err)) |
| continue |
| } |
| - s.interactive.finished(sess.id) |
| + s.interactive.finished(w.sess.id) |
| } |
| + s.interactive.prune(now) |
| } |
| // putGrant writes a session's signed grant to its GrantKey: an object |
| @@ -470,16 +485,26 @@ func (s *Server) putGrant(ctx context.Context, sess *hubSession, wire []byte) er |
| return err |
| } |
| -// deleteGrant deletes a session's grant, best effort: Task 11's housekeeping |
| -// deletes a grant no record names. |
| -func (s *Server) deleteGrant(ctx context.Context, sess *hubSession) { |
| - ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), interactiveEndTimeout) |
| - defer cancel() |
| - if err := s.cfg.Releases.Delete(ctx, interactive.GrantKey(sess.acct, sess.app, sess.id)); err != nil && !errors.Is(err, os.ErrNotExist) { |
| - log.Printf("console: api interactive %s/%s/%s: delete the grant %s: %v", sess.acct, sess.app, sess.host, sess.id, err) |
| +// cleanupGrant deletes a refused admission's grant within the admission's |
| +// own deadline (ctx). One it cannot delete in time waits for the sweep, so |
| +// the reservation and the unit are never held past OpenWait for it. |
| +func (s *Server) cleanupGrant(ctx context.Context, sess *hubSession) { |
| + if err := s.deleteGrant(ctx, sess); err != nil { |
| + log.Printf("console: api interactive %s/%s/%s: the grant %s waits for the sweep: %v", sess.acct, sess.app, sess.host, sess.id, err) |
| + s.interactive.pendGrant(sess) |
| } |
| } |
| +// deleteGrant deletes a session's grant under ctx; an absent grant is |
| +// deleted already. |
| +func (s *Server) deleteGrant(ctx context.Context, sess *hubSession) error { |
| + err := s.cfg.Releases.Delete(ctx, interactive.GrantKey(sess.acct, sess.app, sess.id)) |
| + if err != nil && !errors.Is(err, os.ErrNotExist) { |
| + return err |
| + } |
| + return nil |
| +} |
| + |
| // refuseSession ends a session that was never admitted with End refused. |
| func refuseSession(ws *websocket.Conn, e interactive.End) { |
| e.Reason, e.Ended = interactive.EndRefused, time.Now().UTC() |
diff --git a/internal/console/apiinteractive_lifetimes_test.go b/internal/console/apiinteractive_lifetimes_test.go| index 4cb5c9a56..17a9dd5d8 100644 |
| --- a/internal/console/apiinteractive_lifetimes_test.go |
| +++ b/internal/console/apiinteractive_lifetimes_test.go |
| @@ -222,8 +222,8 @@ func TestAWaitingSessionExpiresDuringTheDrain(t *testing.T) { |
| if len(evs) != 1 || ixEndRow(t, evs[0]).Reason != interactive.EndExpired || ixEndRow(t, evs[0]).ID != id { |
| t.Fatalf("end rows %+v, want one expired", evs) |
| } |
| - if st := hubState(h, id); st != "ended" { |
| - t.Fatalf("hub: %q, want ended", st) |
| + if st := hubState(h, id); st != "" { |
| + t.Fatalf("hub: %q, want it pruned: its cleanup is done and its pickup window has passed", st) |
| } |
| if !ixSweep(fx) { |
| t.Fatal("the second sweep pass was not admitted") |
diff --git a/internal/console/apiinteractive_test.go b/internal/console/apiinteractive_test.go| index 3b378dff1..8ee120019 100644 |
| --- a/internal/console/apiinteractive_test.go |
| +++ b/internal/console/apiinteractive_test.go |
| @@ -244,7 +244,7 @@ func hubState(h *interactiveHub, id string) string { |
| if _, ok := h.endPending[id]; ok { |
| return "pending" |
| } |
| - if h.ended[id] { |
| + if _, ok := h.ended[id]; ok { |
| return "ended" |
| } |
| return "" |
| @@ -990,3 +990,202 @@ func TestAdmissionLandsThroughALostResponse(t *testing.T) { |
| ws.Close() |
| ixWaitEvents(t, fx, "interactive-end", 1) |
| } |
| + |
| +// ixSweepAt is one run-sweep pass at now, as its worker runs it: a mutating |
| +// lifetimes iteration. It reports whether the pass was admitted. |
| +func ixSweepAt(fx *interactiveFx, now time.Time) bool { |
| + return fx.srv.lifetimesIteration(context.Background(), "run sweep", true, false, func(ctx context.Context) { |
| + fx.srv.runSweepPass(ctx, now, false) |
| + }) |
| +} |
| + |
| +// ixWaitHub waits up to 10 seconds for id to reach state in the hub. |
| +func ixWaitHub(t *testing.T, h *interactiveHub, id, state string) { |
| + t.Helper() |
| + deadline := time.Now().Add(10 * time.Second) |
| + for hubState(h, id) != state && time.Now().Before(deadline) { |
| + time.Sleep(10 * time.Millisecond) |
| + } |
| + if got := hubState(h, id); got != state { |
| + t.Fatalf("hub: %s is %q, want %q", id, got, state) |
| + } |
| +} |
| + |
| +// TestAFailedGrantDeleteKeepsTheEndPending (fix round 1, P2 1): an end's |
| +// cleanup stays pending until every write it needs has landed. The bucket |
| +// refuses the grant's DELETE once (an s3:DeleteObject Deny, through the |
| +// fixture's real store): the end waits in endPending with no row; the next |
| +// sweep pass deletes the grant and writes the one interactive-end row. |
| +func TestAFailedGrantDeleteKeepsTheEndPending(t *testing.T) { |
| + fx := newIxFx(t, "code-ix-delete") |
| + h := fx.srv.interactive |
| + ws := ixStart(t, fx, fx.alice, ixOpenArgv()) |
| + rec, _ := ixRecord(t, fx, ixHost) |
| + id := rec.Interactive.ID |
| + grant := interactive.GrantKey(fx.acct, "shop", id) |
| + var refusals atomic.Int32 |
| + refusals.Store(1) |
| + fx.rel.(*deletableStore).setDeleteHook(func(_ context.Context, key string) error { |
| + if key == grant && refusals.Add(-1) >= 0 { |
| + return errors.New("AccessDenied: s3:DeleteObject") |
| + } |
| + return nil |
| + }) |
| + ws.Close() |
| + ixWaitHub(t, h, id, "pending") |
| + if _, _, err := fx.rel.Get(context.Background(), grant); err != nil { |
| + t.Fatalf("the grant whose DELETE failed: %v", err) |
| + } |
| + if evs := ixEvents(t, fx, "interactive-end"); len(evs) != 0 { |
| + t.Fatalf("an end row before its cleanup finished: %+v", evs) |
| + } |
| + if !ixSweepAt(fx, time.Now()) { |
| + t.Fatal("the sweep pass was not admitted") |
| + } |
| + if _, _, err := fx.rel.Get(context.Background(), grant); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatalf("the grant after the sweep: %v", err) |
| + } |
| + evs := ixEvents(t, fx, "interactive-end") |
| + if len(evs) != 1 || ixEndRow(t, evs[0]).Reason != interactive.EndCancelled || ixEndRow(t, evs[0]).ID != id { |
| + t.Fatalf("end rows %+v, want one cancelled", evs) |
| + } |
| + if rec, _ := ixRecord(t, fx, ixHost); rec.Interactive != nil { |
| + t.Fatalf("marker %+v", rec.Interactive) |
| + } |
| + if st := hubState(h, id); st != "ended" { |
| + t.Fatalf("hub: %q, want ended until its pickup window passes", st) |
| + } |
| + ixSweepAt(fx, time.Now()) |
| + if evs := ixEvents(t, fx, "interactive-end"); len(evs) != 1 { |
| + t.Fatalf("a second pass wrote again: %+v", evs) |
| + } |
| +} |
| + |
| +// hungGrantStore lands the grant's PUT and then never answers it, and holds |
| +// the first grant DELETE until its context ends: a bucket that stops |
| +// answering mid-admission. Later DELETEs answer. |
| +type hungGrantStore struct { |
| + release.Store |
| + deletes atomic.Int32 |
| +} |
| + |
| +func (h *hungGrantStore) Put(ctx context.Context, key string, body []byte, ifMatch string) (string, error) { |
| + etag, err := h.Store.Put(ctx, key, body, ifMatch) |
| + if err != nil || !strings.Contains(key, "/interactive/") { |
| + return etag, err |
| + } |
| + <-ctx.Done() |
| + return "", ctx.Err() |
| +} |
| + |
| +func (h *hungGrantStore) Delete(ctx context.Context, key string) error { |
| + if strings.Contains(key, "/interactive/") && h.deletes.Add(1) == 1 { |
| + <-ctx.Done() |
| + return ctx.Err() |
| + } |
| + return h.Store.Delete(ctx, key) |
| +} |
| + |
| +// TestAFailedGrantPutReleasesWithinOpenWait (fix round 1, P2 2): the |
| +// admission's reservation, its unit and its cleanup stay within OpenWait. |
| +// The grant PUT lands but its answer never comes, and the cleanup DELETE |
| +// hangs too: the CLI gets End refused C37 within the bound, the hub and the |
| +// gate hold nothing, and the sweep deletes the grant later, writing no row. |
| +func TestAFailedGrantPutReleasesWithinOpenWait(t *testing.T) { |
| + var hung *hungGrantStore |
| + fx := newInteractiveFxWith(t, 7, "code-ix-hung", consoleFixtureOptions{WithKey: true, WrapReleases: func(s release.Store) release.Store { |
| + hung = &hungGrantStore{Store: s} |
| + return hung |
| + }}) |
| + ixWorld(t, fx) |
| + h := fx.srv.interactive |
| + const wait = 500 * time.Millisecond |
| + h.openWaitFor = wait |
| + var reservedID atomic.Value |
| + h.afterReserve = func(id string) { reservedID.Store(id) } |
| + ws, err := ixDial(t, fx.ts.URL, fx.ixURL(ixHost), fx.alice) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + start := time.Now() |
| + ixSendOpen(t, ws, ixOpenArgv()) |
| + e := ixWantEnd(t, ws) |
| + if took := time.Since(start); took > wait+time.Second { |
| + t.Fatalf("the refusal took %v, past OpenWait %v", took, wait) |
| + } |
| + if e.Reason != interactive.EndRefused || e.Detail != "could not open the session. try again" { |
| + t.Fatalf("end %+v, want refused with RFS-6-C37", e) |
| + } |
| + id, _ := reservedID.Load().(string) |
| + deadline := time.Now().Add(time.Second) |
| + for (hubCount(h) != 0 || fx.srv.cutover.status().Pending != 0) && time.Now().Before(deadline) { |
| + time.Sleep(5 * time.Millisecond) |
| + } |
| + if hubCount(h) != 0 || fx.srv.cutover.status().Pending != 0 { |
| + t.Fatalf("after the refusal the hub holds %d and the gate counts %d units", hubCount(h), fx.srv.cutover.status().Pending) |
| + } |
| + grant := interactive.GrantKey(fx.acct, "shop", id) |
| + if _, _, err := fx.rel.Get(context.Background(), grant); err != nil { |
| + t.Fatalf("the landed grant: %v", err) |
| + } |
| + if st := hubState(h, id); st != "pending" { |
| + t.Fatalf("hub: %q, want its cleanup pending for the sweep", st) |
| + } |
| + if !ixSweepAt(fx, time.Now()) { |
| + t.Fatal("the sweep pass was not admitted") |
| + } |
| + if _, _, err := fx.rel.Get(context.Background(), grant); !errors.Is(err, os.ErrNotExist) { |
| + t.Fatalf("the grant after the sweep: %v", err) |
| + } |
| + if st := hubState(h, id); st != "" { |
| + t.Fatalf("hub: %q, want nothing: no session started", st) |
| + } |
| + if evs := append(ixEvents(t, fx, "interactive-start"), ixEvents(t, fx, "interactive-end")...); len(evs) != 0 { |
| + t.Fatalf("audit rows for a session that never started: %+v", evs) |
| + } |
| +} |
| + |
| +// TestEndedSessionsArePruned (fix round 1, P2 3): an ended id is kept only |
| +// until its pickup window has passed, then forgotten. The box door's lookup |
| +// (door rule 2) finds only an active session, so an ended id, a pruned one |
| +// and an unknown one all get the same answer. |
| +func TestEndedSessionsArePruned(t *testing.T) { |
| + fx := newIxFx(t, "code-ix-prune") |
| + h := fx.srv.interactive |
| + const unknown = "tty_cccccccccccccccccccccccccc" |
| + var ids []string |
| + for i := 0; i < 3; i++ { |
| + ws := ixStart(t, fx, fx.alice, ixOpenArgv()) |
| + rec, _ := ixRecord(t, fx, ixHost) |
| + id := rec.Interactive.ID |
| + if h.activeSession(id) == nil { |
| + t.Fatalf("the waiting session %s is not found active", id) |
| + } |
| + ws.Close() |
| + ixWaitEvents(t, fx, "interactive-end", i+1) |
| + ixWaitHub(t, h, id, "ended") |
| + ids = append(ids, id) |
| + } |
| + for _, id := range append(ids, unknown) { |
| + if h.activeSession(id) != nil { |
| + t.Fatalf("%s is found active", id) |
| + } |
| + } |
| + // Within the window the ids are kept. |
| + ixSweepAt(fx, time.Now()) |
| + if st := hubState(h, ids[0]); st != "ended" { |
| + t.Fatalf("hub: %q before the window passed, want ended", st) |
| + } |
| + ixSweepAt(fx, time.Now().Add(interactive.PickupWindow+time.Minute)) |
| + h.mu.Lock() |
| + n, p := len(h.ended), len(h.endPending) |
| + h.mu.Unlock() |
| + if n != 0 || p != 0 { |
| + t.Fatalf("after the windows passed the hub keeps %d ended and %d pending ids, want none", n, p) |
| + } |
| + for _, id := range ids { |
| + if st := hubState(h, id); st != "" || h.activeSession(id) != nil { |
| + t.Fatalf("%s is still %q", id, st) |
| + } |
| + } |
| +} |
diff --git a/internal/console/interactivehub.go b/internal/console/interactivehub.go| index c8f745bac..27bc1fa0f 100644 |
| --- a/internal/console/interactivehub.go |
| +++ b/internal/console/interactivehub.go |
| @@ -25,20 +25,32 @@ import ( |
| // reserve -> (record CAS) -> activate -> end |
| // \-> release (the admission failed: no session started) |
| // |
| -// end moves an id to ended, or to endPending when the gate refused its end |
| -// step (the sweep finishes it, then it is ended). An id is live, for |
| -// another admission's CAS, while it is reserved or active; never once |
| -// ended or pending. |
| +// end moves an id to ended, or to endPending when its end step did not |
| +// finish (the gate refused it while draining, or a write failed): the sweep |
| +// finishes it, then it is ended. An admission whose grant cleanup did not |
| +// finish within OpenWait is pending too, for the grant alone. An id is |
| +// live, for another admission's CAS, while it is reserved or active; never |
| +// once ended or pending. |
| +// |
| +// Memory is bounded (fix round 1, P2 3): an ended id is kept only until its |
| +// pickup window has passed, then pruned (on each end and each sweep pass). |
| +// No tombstone is needed past that: the box door finds only an active |
| +// session (activeSession), so an ended id and an unknown one get the same |
| +// 404. |
| type interactiveHub struct { |
| mu sync.Mutex |
| byID map[string]*hubSession // reserved or active |
| byHost map[string]string // host -> its active session's id |
| - ended map[string]bool |
| - // endPending is every ended session whose end step the gate refused |
| - // (draining): the sweep makes its writes with this End, then ends it. |
| + // ended is each ended id and when it may be pruned: its PickupBy. |
| + ended map[string]time.Time |
| + // endPending is every ended session whose end step did not finish: the |
| + // sweep makes its writes with this End, then ends it. |
| endPending map[string]interactive.End |
| // pendingSess is the session of each endPending id, for those writes. |
| pendingSess map[string]*hubSession |
| + // grantOnly marks an endPending id that never became a session: only |
| + // its grant is left to delete, and it writes no row. |
| + grantOnly map[string]bool |
| // sessions counts the session goroutines running, so a test (or a |
| // shutdown) can wait for every end's writes. |
| @@ -90,9 +102,10 @@ func newInteractiveHub() *interactiveHub { |
| return &interactiveHub{ |
| byID: map[string]*hubSession{}, |
| byHost: map[string]string{}, |
| - ended: map[string]bool{}, |
| + ended: map[string]time.Time{}, |
| endPending: map[string]interactive.End{}, |
| pendingSess: map[string]*hubSession{}, |
| + grantOnly: map[string]bool{}, |
| } |
| } |
| @@ -170,22 +183,39 @@ func (h *interactiveHub) release(id string) { |
| } |
| } |
| -// end takes id out of the live set and marks it ended. Called as a session |
| -// ends, before its end step's writes: from then on no admission finds it |
| -// live and the box door finds nothing to attach to. |
| +// activeSession is the session id names when it is active, else nil: the |
| +// box door's lookup (Task 11, door rule 2). A reserved id, an ended or |
| +// pending one, a pruned one and an unknown one are all nil, so they all get |
| +// the same 404. |
| +func (h *interactiveHub) activeSession(id string) *hubSession { |
| + h.mu.Lock() |
| + defer h.mu.Unlock() |
| + if sess, ok := h.byID[id]; ok && sess.active { |
| + return sess |
| + } |
| + return nil |
| +} |
| + |
| +// end takes id out of the live set and marks it ended until its PickupBy. |
| +// Called as a session ends, before its end step's writes: from then on no |
| +// admission finds it live and the box door finds nothing to attach to. |
| func (h *interactiveHub) end(id string) { |
| h.mu.Lock() |
| defer h.mu.Unlock() |
| + now := time.Now() |
| + h.pruneLocked(now) |
| + until := now |
| if sess, ok := h.byID[id]; ok { |
| if h.byHost[sess.host] == id { |
| delete(h.byHost, sess.host) |
| } |
| delete(h.byID, id) |
| + until = sess.pickupBy |
| } |
| - h.ended[id] = true |
| + h.ended[id] = until |
| } |
| -// pend records that the gate refused sess's end step: the sweep finishes it |
| +// pend records that sess's end step did not finish: the sweep finishes it |
| // with e. |
| func (h *interactiveHub) pend(sess *hubSession, e interactive.End) { |
| h.mu.Lock() |
| @@ -195,22 +225,60 @@ func (h *interactiveHub) pend(sess *hubSession, e interactive.End) { |
| h.pendingSess[sess.id] = sess |
| } |
| -// pending is a snapshot of the endPending sessions and their Ends. |
| -func (h *interactiveHub) pending() map[*hubSession]interactive.End { |
| +// pendGrant records that a refused admission's grant was not deleted within |
| +// OpenWait: the sweep deletes it. No session started, so it is never ended |
| +// and writes no row. |
| +func (h *interactiveHub) pendGrant(sess *hubSession) { |
| + h.mu.Lock() |
| + defer h.mu.Unlock() |
| + h.endPending[sess.id] = interactive.End{Reason: interactive.EndRefused} |
| + h.pendingSess[sess.id] = sess |
| + h.grantOnly[sess.id] = true |
| +} |
| + |
| +// pendingWork is one endPending entry for the sweep. |
| +type pendingWork struct { |
| + sess *hubSession |
| + end interactive.End |
| + grantOnly bool |
| +} |
| + |
| +// pending is a snapshot of the endPending work. |
| +func (h *interactiveHub) pending() []pendingWork { |
| h.mu.Lock() |
| defer h.mu.Unlock() |
| - out := make(map[*hubSession]interactive.End, len(h.endPending)) |
| + out := make([]pendingWork, 0, len(h.endPending)) |
| for id, e := range h.endPending { |
| - out[h.pendingSess[id]] = e |
| + out = append(out, pendingWork{sess: h.pendingSess[id], end: e, grantOnly: h.grantOnly[id]}) |
| } |
| return out |
| } |
| -// finished marks an endPending id ended once the sweep made its writes. |
| +// finished drops an endPending id once the sweep made its writes. A session |
| +// is then ended until its PickupBy; a grant-only entry is simply gone. |
| func (h *interactiveHub) finished(id string) { |
| h.mu.Lock() |
| defer h.mu.Unlock() |
| + sess, only := h.pendingSess[id], h.grantOnly[id] |
| delete(h.endPending, id) |
| delete(h.pendingSess, id) |
| - h.ended[id] = true |
| + delete(h.grantOnly, id) |
| + if !only && sess != nil { |
| + h.ended[id] = sess.pickupBy |
| + } |
| +} |
| + |
| +// prune forgets every ended id whose pickup window has passed by now. |
| +func (h *interactiveHub) prune(now time.Time) { |
| + h.mu.Lock() |
| + defer h.mu.Unlock() |
| + h.pruneLocked(now) |
| +} |
| + |
| +func (h *interactiveHub) pruneLocked(now time.Time) { |
| + for id, until := range h.ended { |
| + if !until.After(now) { |
| + delete(h.ended, id) |
| + } |
| + } |
| } |
diff --git a/internal/console/runsweep.go b/internal/console/runsweep.go| index a6fa0b7a1..eca0ff870 100644 |
| --- a/internal/console/runsweep.go |
| +++ b/internal/console/runsweep.go |
| @@ -85,7 +85,7 @@ func (s *Server) runSweepPass(ctx context.Context, now time.Time, housekeeping b |
| defer cancel() |
| // Interactive sessions whose end the draining gate refused (rootfs |
| // apps Plan 6, PF-4): their writes, under this pass's unit. |
| - s.finishPendingInteractiveEnds(ctx) |
| + s.finishPendingInteractiveEnds(ctx, now) |
| src, viewed := s.recordWalkStore(ctx, s.cfg.Releases) |
| recs, complete, err := provision.ListAllRecordsTolerantJudged(ctx, src, provision.JudgeFrom(ctx)) |
| if err != nil { |
diff --git a/internal/provision/rootfs_apps_6_census_test.go b/internal/provision/rootfs_apps_6_census_test.go| index 988a4c38d..8cb7a327d 100644 |
| --- a/internal/provision/rootfs_apps_6_census_test.go |
| +++ b/internal/provision/rootfs_apps_6_census_test.go |
| @@ -37,6 +37,10 @@ package provision |
| // literals, no record touched); NewServer mints bootID and the hub (no |
| // record touched); consolePerfSkip names the session door's path |
| // ("/instances/" is a URL segment, not a record key). |
| +// - Task 10 fix round 1: admitSession's grant cleanup and endWrites's |
| +// grant delete now return a failure (so an end stays pending and the |
| +// row is written once, after every write). The same one |
| +// SetRecordInteractive in each; re-pinned. |
| func rootfsApps6WriteCensusInventory() []censusEntry { |
| entries := append([]censusEntry(nil), fleetPublicRoutesWriteCensusInventory()...) |
| replacements := []censusEntry{ |
| @@ -63,8 +67,8 @@ func rootfsApps6WriteCensusInventory() []censusEntry { |
| added := []censusEntry{ |
| {"internal/provision/interactive.go", "SetRecordInteractive", "record-writer", "key:SetRecordInteractive,key:updateRecord", "f2f8c0ad8647cc048a0be0fa3edc051ddd8e3157eba038123f9e5bab79f4530e"}, |
| {"internal/appconfig/interactive.go", "claimRawSetInteractive", "nonrecord", "sel:Put", "d4c768601d1b9bdb44c0fd0c2c6f058c18c1434856e357f00628d2f600ce039b"}, |
| - {"internal/console/apiinteractive.go", "Server.admitSession", "record-writer", "key:SetRecordInteractive", "232eea1eddee8d6abc87ed9462c148ee70cb1ed64e7c123106c27678abc7d8ca"}, |
| - {"internal/console/apiinteractive.go", "Server.endWrites", "record-writer", "key:SetRecordInteractive", "fd4136620a996f3aff3beb0dab03a84056d80694c123cdc839c5ecab7a4e1731"}, |
| + {"internal/console/apiinteractive.go", "Server.admitSession", "record-writer", "key:SetRecordInteractive", "c81a6696dfbeb6c4f4746cec9487e099b27506c5c5555b30cbff0c0b2f6ce5f1"}, |
| + {"internal/console/apiinteractive.go", "Server.endWrites", "record-writer", "key:SetRecordInteractive", "6a183621b60073e45f698e046a177adca2f536b47a365bdb66de5548257b9fea"}, |
| {"internal/console/apiinteractive.go", "Server.putGrant", "nonrecord", "sel:Put", "22a18ef45051da8cfb8a6a62ad8891afd28e3bf71cc26a541639cd2a1dc0a1ff"}, |
| {"internal/console/server.go", "consolePerfSkip", "key-site", "lit:instances", "d4b65473d3702b49d79a799d46f078e4491b6e86a59a101dd4c1ce4d50f698e7"}, |
| } |