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.

agent: wait for a started hook, run OnError/OnPredictEnd after cancel or Close, catch Goexit

Adeshina Lasisiea31f986522e
diff --git a/laya-go/agent/hooks.go b/laya-go/agent/hooks.go
index 8e4e580b..b9af01e3 100644
--- a/laya-go/agent/hooks.go
+++ b/laya-go/agent/hooks.go
@@ -22,8 +22,13 @@ package agent
// WithHookTimeout bounds the wait for each hook. Go cannot stop a goroutine, so a hook that
// overruns keeps running after the call has returned an error: the timeout protects the request,
// not the process. Such a hook must not touch the PredictContext it was given (the call has moved
-// on and owns it), and it no longer holds the serial lock. Close does not wait for a running
-// hook: a call that is waiting on one gives up with ErrClosed.
+// on and owns it), and it no longer holds the serial lock. Each overrun leaks one goroutine until
+// the hook returns, so "a hook needs no locking" holds only while no timeout has fired. A started
+// hook is waited for until it returns or times out, whatever Close or the caller's context does,
+// so a hook that may block needs a timeout; OnError and OnPredictEnd still run after a cancelled
+// context. A hook that calls back into its own agent hangs on the serial lock, where Python's
+// lock is re-entrant.
+// A hook's MaxLen or HeadMaxLen of 0 or less means "no override", as Go has no None.
import (
"context"
@@ -429,10 +434,18 @@ func (a *Agent) dispatch(ctx context.Context, ev hookEvent, hooks []Hook, pc *Pr
func (a *Agent) callHook(ctx context.Context, ev hookEvent, h Hook, pc *PredictContext) error {
hk := a.hk
if hk.serial {
+ // OnError and OnPredictEnd report on work already done: they wait for the lock however
+ // the call ends, as Python's always run.
+ var closed <-chan struct{}
+ if ev.name == evStart.name {
+ closed = hk.done
+ } else {
+ ctx = context.WithoutCancel(ctx)
+ }
select {
case hk.sem <- struct{}{}:
defer func() { <-hk.sem }()
- case <-hk.done:
+ case <-closed:
return ErrClosed
case <-ctx.Done():
return ctx.Err()
@@ -440,8 +453,14 @@ func (a *Agent) callHook(ctx context.Context, ev hookEvent, h Hook, pc *PredictC
}
res := make(chan error, 1) // buffered: an abandoned hook still finishes without blocking
go func() {
+ returned := false
defer func() {
- if r := recover(); r != nil {
+ r := recover()
+ switch {
+ case returned:
+ case r == nil: // runtime.Goexit, e.g. t.FailNow: no return and no panic
+ res <- fmt.Errorf("laya: hook %s exited without returning", hookName(h))
+ default:
if err, ok := r.(error); ok {
res <- err
} else {
@@ -450,6 +469,7 @@ func (a *Agent) callHook(ctx context.Context, ev hookEvent, h Hook, pc *PredictC
}
}()
ev.call(h, pc)
+ returned = true
res <- nil
}()
var expired <-chan time.Time
@@ -458,15 +478,13 @@ func (a *Agent) callHook(ctx context.Context, ev hookEvent, h Hook, pc *PredictC
defer t.Stop()
expired = t.C
}
+ // Once started, a hook is waited for until it returns or times out: abandoning it on Close
+ // or on the caller's context would leave it racing the next hook and the call's results.
select {
case err := <-res:
return err
case <-expired:
return fmt.Errorf("laya: hook %s exceeded %gs", hookName(h), hk.timeout.Seconds())
- case <-hk.done:
- return ErrClosed
- case <-ctx.Done():
- return ctx.Err()
}
}
diff --git a/laya-go/agent/hooks_test.go b/laya-go/agent/hooks_test.go
index 1960bbbe..414ebf33 100644
--- a/laya-go/agent/hooks_test.go
+++ b/laya-go/agent/hooks_test.go
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"math"
+ "runtime"
"strings"
"sync"
"sync/atomic"
@@ -157,14 +158,66 @@ func TestCloseDuringRunningHookReturns(t *testing.T) {
closed := make(chan struct{})
go func() { a.Close(); close(closed) }()
select {
+ case err := <-done:
+ t.Fatalf("call returned %v while its hook still ran: it would race the next hook", err)
case <-closed:
+ t.Fatal("Close returned while a call was still running its hook")
+ case <-time.After(50 * time.Millisecond):
+ }
+ close(release)
+ for _, c := range []chan struct{}{closed} {
+ select {
+ case <-c:
+ case <-time.After(5 * time.Second):
+ t.Fatal("Close did not return once the hook did")
+ }
+ }
+ select {
+ case <-done:
case <-time.After(5 * time.Second):
- t.Fatal("Close deadlocked behind a running hook")
+ t.Fatal("call did not return once its hook did")
}
- if err := <-done; !errors.Is(err, ErrClosed) {
- t.Fatalf("call err = %v, want ErrClosed", err)
+}
+
+func TestGoexitInHookIsAnError(t *testing.T) {
+ h := &HookFuncs{OnStart: func(*PredictContext) { runtime.Goexit() }}
+ a := testAgent(t, options{raise: true, installed: []Hook{h}})
+ done := make(chan error, 1)
+ go func() {
+ _, err := a.PredictBatch(context.Background(), []any{"s"}, questionsOf("q"))
+ done <- err
+ }()
+ select {
+ case err := <-done:
+ if err == nil || !strings.Contains(err.Error(), "exited without returning") {
+ t.Fatalf("err = %v", err)
+ }
+ case <-time.After(5 * time.Second):
+ t.Fatal("a hook that called Goexit hung the call and held the serial lock")
+ }
+}
+
+func TestEndAndErrorHooksRunAfterCancelAndQuit(t *testing.T) {
+ var errRan, endRan atomic.Int32
+ h := &HookFuncs{OnErr: func(*PredictContext) { errRan.Add(1) }, OnEnd: func(*PredictContext) { endRan.Add(1) }}
+ var cancel context.CancelFunc
+ a := testAgent(t, options{raise: true, installed: []Hook{h}})
+ a.hk.stub = func(context.Context, []any, schema.Questions, call) ([]schema.Prediction, error) {
+ cancel()
+ a.hk.quit()
+ return nil, errors.New("inference failed")
+ }
+ for i := 0; i < 50; i++ {
+ errRan.Store(0)
+ endRan.Store(0)
+ var ctx context.Context
+ ctx, cancel = context.WithCancel(context.Background())
+ _, _ = a.PredictBatch(ctx, []any{"s"}, questionsOf("q"))
+ cancel()
+ if errRan.Load() != 1 || endRan.Load() != 1 {
+ t.Fatalf("run %d: OnError ran %d, OnPredictEnd ran %d, want 1 and 1", i, errRan.Load(), endRan.Load())
+ }
}
- close(release)
}
func TestValidateTimeout(t *testing.T) {
@@ -230,3 +283,28 @@ func TestCallablesRunInTheOrderGiven(t *testing.T) {
t.Fatalf("order = %v, want %s", got, want)
}
}
+
+func TestCancelledContextDoesNotAbandonARunningHook(t *testing.T) {
+ release, started := make(chan struct{}), make(chan struct{})
+ h := &HookFuncs{OnStart: func(*PredictContext) { close(started); <-release }}
+ a := testAgent(t, options{raise: true, installed: []Hook{h}})
+ ctx, cancel := context.WithCancel(context.Background())
+ done := make(chan error, 1)
+ go func() {
+ _, err := a.PredictBatch(ctx, []any{"s"}, questionsOf("q"))
+ done <- err
+ }()
+ <-started
+ cancel()
+ select {
+ case err := <-done:
+ t.Fatalf("call returned %v while its hook still ran", err)
+ case <-time.After(50 * time.Millisecond):
+ }
+ close(release)
+ select {
+ case <-done:
+ case <-time.After(5 * time.Second):
+ t.Fatal("call did not return once its hook did")
+ }
+}