fix(19.7): RA-WR-04 LRU eviction spares stateless servers that are serving
Each stateless request now carries an entry-use record on its context; getServer counts the entry it hands out as active (under the path lock) and ServeHTTP releases it when the request ends, so a subscriptions/listen stream keeps its entry active for its whole life. Capacity eviction walks the LRU from the back and drops only idle entries, so it no longer ends a quiet listener or cancels a tool call in flight, and a revocation still finds every open stream. While every entry is busy the LRU stays above capacity until requests end. TestStatelessFanOut's LRU subtest now asserts that the listener's server is spared when the binding past capacity arrives (the oldest idle server goes instead) and that a revocation then still ends the stream.
This commit is contained in:
@@ -28,6 +28,10 @@ type statelessEntry struct {
|
||||
binding string
|
||||
server *mcp.Server
|
||||
elem *list.Element
|
||||
// active counts the requests being served on server (a
|
||||
// subscriptions/listen stream for its whole life); guarded by
|
||||
// statelessPath.mu. The LRU never evicts an active entry (RA-WR-04).
|
||||
active int
|
||||
|
||||
// mu serializes re-filtering and guards fingerprint, widened, tools and
|
||||
// prompts (see localSession); identity has its own box.
|
||||
@@ -61,6 +65,16 @@ type statelessPath struct {
|
||||
// LRU capacity.
|
||||
const statelessCapacityFactor = 16
|
||||
|
||||
// entryUseKey keys the entryUse of one stateless request on its context.
|
||||
type entryUseKey struct{}
|
||||
|
||||
// entryUse lists the entries one stateless request is served by, so
|
||||
// ServeHTTP releases them when the request ends; guarded by
|
||||
// statelessPath.mu.
|
||||
type entryUse struct {
|
||||
entries []*statelessEntry
|
||||
}
|
||||
|
||||
func newStatelessPath(p *Pier) *statelessPath {
|
||||
sp := &statelessPath{
|
||||
p: p,
|
||||
@@ -84,6 +98,9 @@ func newStatelessPath(p *Pier) *statelessPath {
|
||||
// every recheck interval and ends when the bearer no longer resolves to
|
||||
// the binding that opened it (A-WR-06).
|
||||
func (sp *statelessPath) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
use := &entryUse{}
|
||||
r = r.WithContext(context.WithValue(r.Context(), entryUseKey{}, use))
|
||||
defer sp.release(use)
|
||||
if r.Method != http.MethodPost || r.Body == nil {
|
||||
sp.handler.ServeHTTP(w, r)
|
||||
return
|
||||
@@ -135,7 +152,7 @@ func (sp *statelessPath) getServer(r *http.Request) *mcp.Server {
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
if e := sp.hit(id.Binding); e != nil {
|
||||
if e := sp.hit(r, id.Binding); e != nil {
|
||||
sp.refresh(r.Context(), e, id)
|
||||
return e.server
|
||||
}
|
||||
@@ -147,34 +164,39 @@ func (sp *statelessPath) getServer(r *http.Request) *mcp.Server {
|
||||
sp.mu.Lock()
|
||||
if cur, ok := sp.entries[id.Binding]; ok {
|
||||
sp.order.MoveToFront(cur.elem)
|
||||
sp.track(r, cur)
|
||||
sp.mu.Unlock()
|
||||
sp.refresh(r.Context(), cur, id)
|
||||
return cur.server
|
||||
}
|
||||
e.elem = sp.order.PushFront(e)
|
||||
sp.entries[id.Binding] = e
|
||||
sp.track(r, e)
|
||||
// Past capacity the least recently used idle entries go. An entry that
|
||||
// is serving a request (a quiet subscriptions/listen stream, a tool call
|
||||
// in flight) is never evicted, so capacity never cuts a stream or a call
|
||||
// off and a revocation still finds every open stream (A-WR-06, RA-WR-04);
|
||||
// while every entry is busy the LRU stays above capacity.
|
||||
var evicted []*statelessEntry
|
||||
for sp.order.Len() > sp.capacity {
|
||||
last := sp.order.Back()
|
||||
if last == nil {
|
||||
break
|
||||
for el := sp.order.Back(); el != nil && sp.order.Len() > sp.capacity; {
|
||||
prev := el.Prev()
|
||||
if old := el.Value.(*statelessEntry); old.active == 0 {
|
||||
sp.order.Remove(el)
|
||||
delete(sp.entries, old.binding)
|
||||
evicted = append(evicted, old)
|
||||
}
|
||||
old := last.Value.(*statelessEntry)
|
||||
sp.order.Remove(last)
|
||||
delete(sp.entries, old.binding)
|
||||
evicted = append(evicted, old)
|
||||
el = prev
|
||||
}
|
||||
sp.mu.Unlock()
|
||||
// An evicted server's listen streams end with it, so none outlives the
|
||||
// entry a later revocation would look for (A-WR-06).
|
||||
for _, old := range evicted {
|
||||
closeSessions(old.server)
|
||||
}
|
||||
return srv
|
||||
}
|
||||
|
||||
// hit answers the entry of a binding, most recently used now, or nil.
|
||||
func (sp *statelessPath) hit(binding string) *statelessEntry {
|
||||
// hit answers the entry of a binding, most recently used now and counted as
|
||||
// serving r, or nil.
|
||||
func (sp *statelessPath) hit(r *http.Request, binding string) *statelessEntry {
|
||||
sp.mu.Lock()
|
||||
defer sp.mu.Unlock()
|
||||
e, ok := sp.entries[binding]
|
||||
@@ -182,9 +204,32 @@ func (sp *statelessPath) hit(binding string) *statelessEntry {
|
||||
return nil
|
||||
}
|
||||
sp.order.MoveToFront(e.elem)
|
||||
sp.track(r, e)
|
||||
return e
|
||||
}
|
||||
|
||||
// track counts e as serving r until ServeHTTP releases r's entries; the
|
||||
// caller holds sp.mu. A request reaching getServer without ServeHTTP's
|
||||
// entryUse is not counted.
|
||||
func (sp *statelessPath) track(r *http.Request, e *statelessEntry) {
|
||||
use, _ := r.Context().Value(entryUseKey{}).(*entryUse)
|
||||
if use == nil || slices.Contains(use.entries, e) {
|
||||
return
|
||||
}
|
||||
e.active++
|
||||
use.entries = append(use.entries, e)
|
||||
}
|
||||
|
||||
// release ends the request's use of its entries.
|
||||
func (sp *statelessPath) release(use *entryUse) {
|
||||
sp.mu.Lock()
|
||||
for _, e := range use.entries {
|
||||
e.active--
|
||||
}
|
||||
use.entries = nil
|
||||
sp.mu.Unlock()
|
||||
}
|
||||
|
||||
// refresh stores the request's identity on an entry and, when the
|
||||
// identity's fingerprint is new to the entry, adds the tools and prompts
|
||||
// it allows (A-WR-04). It never removes any (RA-WR-02): one server serves
|
||||
|
||||
Reference in New Issue
Block a user