fix(pier): an ended session is never written back by a request still in flight

- persisting a session's protocol state after a request now rewrites an
  existing record only (Redis SET XX, the memory store likewise) and closes
  the local copy when the record is gone
- before, a request that finished after DELETE, a revoked or deleted subject
  event or the idle expiry put the record back, and the next request
  rehydrated the ended session
- found by plan 19.7-05's TestFanOut (revocation and deletion cases, about
  one run in a hundred)
This commit is contained in:
Jakub Zych
2026-10-23 13:55:04 +02:00
parent c29d2289d0
commit 1111828e8a
4 changed files with 69 additions and 1 deletions

View File

@@ -338,10 +338,24 @@ func (sr *sessionRouter) persist(ctx context.Context, ls *localSession) {
if err != nil {
return
}
if err := sr.p.store.Put(ctx, ls.id, Record{Binding: ls.binding, State: raw, Created: ls.created}, sr.p.cfg.IdleTTL); err != nil {
rec := Record{Binding: ls.binding, State: raw, Created: ls.created}
stored := true
// The record is only rewritten, never created here: a session that ended
// while this request ran (DELETE, revocation, deletion, expiry) stays
// ended instead of being written back by its last request.
if u, ok := sr.p.store.(updater); ok {
stored, err = u.Update(ctx, ls.id, rec, sr.p.cfg.IdleTTL)
} else {
err = sr.p.store.Put(ctx, ls.id, rec, sr.p.cfg.IdleTTL)
}
if err != nil {
logger().Warn("pier: session state not stored", "session", sessionPrefix(ls.id), "error", err)
return
}
if !stored {
sr.closeLocal(ls.id)
return
}
if complete {
ls.persisted.Store(true)
}