Skip to content

armgit / laya

Public Imported from GitHub
0

Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.

router lifecycle: Preload keeps its lease across on_load; drop unreachable finish guards

Adeshina Lasisi6aa3fbc8bfb3
diff --git a/laya-go/router/hooks.go b/laya-go/router/hooks.go
index b71263cf..c2fba434 100644
--- a/laya-go/router/hooks.go
+++ b/laya-go/router/hooks.go
@@ -20,7 +20,8 @@ package router
// A hook may replace ctx.Decision; the replacement is what Route returns. Route does not check
// it: a Model that names no checkpoint is the hook's business, and the caller that loads it
// sees the error. A hook that overruns WithHookTimeout is abandoned, not stopped (see
-// agent/hooks.go).
+// agent/hooks.go). After a timeout the call gives its lease back, so the agent in an abandoned
+// OnLoad hook's context may be closed while that hook still runs.
import (
"context"
diff --git a/laya-go/router/lifecycle.go b/laya-go/router/lifecycle.go
index 2a4a3a32..90c8e4b1 100644
--- a/laya-go/router/lifecycle.go
+++ b/laya-go/router/lifecycle.go
@@ -238,12 +238,7 @@ func (l *lifecycle) create(ctx context.Context, key Checkpoint) (*agent.Agent, e
// doBuild is the build this caller owns: mine is the handle the others wait on.
func (l *lifecycle) doBuild(ctx context.Context, key Checkpoint, mine *inflight, out *outcome) (built *slot, err error) {
var toClose []*agent.Agent
- finished := false
- finish := func() { // mu is held; idempotent
- if finished {
- return
- }
- finished = true
+ finish := func() { // mu is held; runs once: nothing after it can panic (closes are safeClose)
if l.loading[key] == mine {
delete(l.loading, key)
}
@@ -257,9 +252,7 @@ func (l *lifecycle) doBuild(ctx context.Context, key Checkpoint, mine *inflight,
if err != nil {
built = nil
l.mu.Lock()
- if mine.err == nil && !finished {
- mine.err = err
- }
+ mine.err = err
finish()
l.mu.Unlock()
}
@@ -410,7 +403,8 @@ func (r *Router) Lease(ctx context.Context, name string) (*Lease, error) {
// Load returns the agent for a checkpoint, building it on first use. Concurrent callers share one
// build. The agent is valid only while it stays resident: eviction and Unload close an agent the
// Router built, so a reference held across either is a closed session. Use Lease, which holds the
-// agent open, unless the caller manages the lifetime itself. If a hook fails Load returns the
+// agent open, unless the caller manages the lifetime itself. If an OnLoad hook unloads, or closes
+// the Router, for the same checkpoint, Load returns an agent that is already closed and no error. If a hook fails Load returns the
// error and no agent, and the checkpoint stays resident.
func (r *Router) Load(ctx context.Context, name string) (*agent.Agent, error) {
key, err := NormaliseName(name)
@@ -512,8 +506,8 @@ func (r *Router) Preload(ctx context.Context, names ...string) error {
if err != nil {
return err
}
- rerr := l.release(s)
- if herr := r.dispatchOutcome(key, out); herr != nil || rerr != nil {
+ herr := r.dispatchOutcome(key, out) // the lease is held across the hooks, as in Load
+ if rerr := l.release(s); herr != nil || rerr != nil {
return errors.Join(herr, rerr)
}
}
diff --git a/laya-go/router/lifecycle_review_test.go b/laya-go/router/lifecycle_review_test.go
index 7c23e104..d1ea8006 100644
--- a/laya-go/router/lifecycle_review_test.go
+++ b/laya-go/router/lifecycle_review_test.go
@@ -210,3 +210,26 @@ func TestStressLeaseAccountingWithCancellation(t *testing.T) {
}
}
}
+
+func TestPreloadKeepsItsLeaseAcrossTheLoadHook(t *testing.T) {
+ f := newFactory(t)
+ r := newRouter(t, f)
+ var sawClosed atomic.Bool
+ var once sync.Once
+ r.AddHook(&router.HookFuncs{OnLoadFunc: func(c *router.LoadContext) {
+ once.Do(func() {
+ go r.Unload(context.Background(), "english") // frees the checkpoint under the hook
+ b := f.of(c.Agent)
+ for i := 0; i < 100 && b.sess.closes.Load() == 0; i++ {
+ time.Sleep(5 * time.Millisecond)
+ }
+ sawClosed.Store(b.sess.closes.Load() > 0)
+ })
+ }})
+ if err := r.Preload(context.Background(), "english"); err != nil {
+ t.Fatal(err)
+ }
+ if sawClosed.Load() {
+ t.Fatal("OnLoad held an agent that a concurrent Unload closed")
+ }
+}