#4553's panic took the process down because it ran on a goroutine no recovery covered. Every goroutine on the path now hands a panic back: - pii's worker calls classify through classifySafely, which recovers, logs the stack, and returns "the local masking model failed reading this request (...)" as that request's error. It sets no retry hold-back, so the next request is tried as usual. Warm in astrlink.go and serve.go goes through it too. - guard.Model.Find's per-window workers and bert.go's parallel carry a panic back to the caller (type carried), and give(procs) is deferred so a panicking window doesn't keep its cores. - masking.find's goroutine recovers into the same error. The gateway's mask calls already run in gwmw.Safely; the pf-serve model runs in its own process. Tests: TestGuardPanicFailsOnlyItsRequest (the error, the next request masks its phone, Now().Failed stays empty), TestParallelCarriesAPanic, TestFindCarriesAWindowsPanic (real model, a cut word table makes a window worker panic; the caller sees it and no core is held), TestModelPanicIsTheRequestsError (masking). Broken by hand, each red: no classifySafely, the pii test binary crashes; no recover in masking.find, the masking binary crashes; no carry in parallel, TestParallelCarriesAPanic fails; no carry in Find, the guard binary crashes (slice bounds [:5736192] capacity 384); give not deferred, "3 cores still held after the panic". Run: as in the stack's first commit (mote: gofmt, vet x3, the pii, masking and gateway packages at -timeout 45m; Windows: pii, masking, gateway Embeddings). Not tried: a panic injected on the CoreML path (macOS, its own binding); the full ./... suite. Co-authored-by: SunnyRx <7306148+SunnyRx@users.noreply.github.com>
137 lines
3.2 KiB
Go
137 lines
3.2 KiB
Go
package guard
|
|
|
|
import (
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
// A window takes a core, waiting while every one is held, and of more
|
|
// only those free: Finds at once never read on more than Cores()
|
|
// between them (yyhhyyyyyy's requests at once, xinvxueyuan's 13 cores).
|
|
func TestTakeHoldsOnlyWhatIsFree(t *testing.T) {
|
|
c := cap(cores)
|
|
if c != Cores() {
|
|
t.Fatalf("%d cores to share, Cores() %d", c, Cores())
|
|
}
|
|
held := 0
|
|
for range c - 1 {
|
|
held += take(1)
|
|
}
|
|
got := make(chan int, 1)
|
|
go func() { got <- take(c) }()
|
|
select {
|
|
case n := <-got:
|
|
if n != 1 {
|
|
t.Fatalf("took %d with one free", n)
|
|
}
|
|
case <-time.After(10 * time.Second):
|
|
t.Fatal("waited for more than the one free")
|
|
}
|
|
held++
|
|
go func() { got <- take(c) }()
|
|
select {
|
|
case n := <-got:
|
|
t.Fatalf("took %d with none free", n)
|
|
case <-time.After(50 * time.Millisecond):
|
|
}
|
|
give(1)
|
|
held--
|
|
if n := <-got; n != 1 {
|
|
t.Fatalf("took %d of the one given back", n)
|
|
}
|
|
held++
|
|
give(held)
|
|
if len(cores) != 0 {
|
|
t.Fatalf("%d left held", len(cores))
|
|
}
|
|
if n := take(c); n != c {
|
|
t.Fatalf("took %d of %d free", n, c)
|
|
}
|
|
give(c)
|
|
}
|
|
|
|
// Texts read at once, sharing the cores, find what each finds alone.
|
|
func TestFindsAtOnceReadAsOneAtATime(t *testing.T) {
|
|
m, err := Load(dir(t))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m.ml = nil
|
|
texts := make([]string, 8)
|
|
for i := range texts {
|
|
var b strings.Builder
|
|
for k := range 3 + 5*i {
|
|
fmt.Fprintf(&b, "第%d位联系人:张三,电话 138%08d,邮箱 user%d@example.com。\n", k, (i+1)*k*7919, k)
|
|
}
|
|
texts[i] = b.String()
|
|
}
|
|
alone := make([][]Span, len(texts))
|
|
for i, s := range texts {
|
|
alone[i] = m.Find(s)
|
|
}
|
|
at := make([][]Span, len(texts))
|
|
var wg sync.WaitGroup
|
|
for i, s := range texts {
|
|
wg.Go(func() { at[i] = m.Find(s) })
|
|
}
|
|
wg.Wait()
|
|
for i := range texts {
|
|
if len(alone[i]) == 0 || !reflect.DeepEqual(at[i], alone[i]) {
|
|
t.Fatalf("text %d: at once %d spans, alone %d", i, len(at[i]), len(alone[i]))
|
|
}
|
|
}
|
|
if len(cores) != 0 {
|
|
t.Fatalf("%d cores left held", len(cores))
|
|
}
|
|
}
|
|
|
|
// A panic on a goroutine the read starts is panicked again on the one
|
|
// that waits, where Find's caller recovers it: left on its own it ended
|
|
// magpie (#4553). Every part ends first.
|
|
func TestParallelCarriesAPanic(t *testing.T) {
|
|
var ran sync.WaitGroup
|
|
ran.Add(4)
|
|
got := func() (r any) {
|
|
defer func() { r = recover() }()
|
|
parallel(4, 8, func(a, b int) {
|
|
defer ran.Done()
|
|
if a == 0 {
|
|
panic("part 0")
|
|
}
|
|
})
|
|
return nil
|
|
}()
|
|
if got != "part 0" {
|
|
t.Fatalf("recovered %v, want part 0's panic", got)
|
|
}
|
|
ran.Wait()
|
|
}
|
|
|
|
// A window whose read panics (here the network's word table cut to its
|
|
// first row, so a window's ids run past it) panics on Find's caller, not
|
|
// on the window's goroutine, and gives back the cores it held, so the
|
|
// reads after it still get them.
|
|
func TestFindCarriesAWindowsPanic(t *testing.T) {
|
|
m, err := Load(dir(t))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
m.ml = nil
|
|
m.net.word = m.net.word[:hidden:hidden]
|
|
got := func() (r any) {
|
|
defer func() { r = recover() }()
|
|
m.Find(strings.Repeat("hello world ", 400)) // windows at once
|
|
return nil
|
|
}()
|
|
if got == nil {
|
|
t.Fatal("Find didn't panic on its caller")
|
|
}
|
|
if n := len(cores); n != 0 {
|
|
t.Fatalf("%d cores still held after the panic", n)
|
|
}
|
|
}
|