pii: a panic in the local model fails its request instead of ending magpie (#4553)
#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>
This commit is contained in:
@@ -10,6 +10,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
@@ -260,6 +261,14 @@ func find(ctx context.Context, m pii.Model, texts []string) ([][]redact.Found, e
|
||||
}
|
||||
ch := make(chan res, 1)
|
||||
go func() {
|
||||
// a panic in the model is this request's error: on a goroutine
|
||||
// of its own it would end magpie (#4553)
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("masking: the local model panicked: %v\n%s", r, debug.Stack())
|
||||
ch <- res{nil, fmt.Errorf("the local masking model failed reading this request (%v)", r)}
|
||||
}
|
||||
}()
|
||||
got, err := m.Find(ctx, texts)
|
||||
ch <- res{got, err}
|
||||
}()
|
||||
|
||||
@@ -348,3 +348,22 @@ func TestDefaultModelIsBounded(t *testing.T) {
|
||||
t.Fatal("turned on by the user, the model was asked Bounded")
|
||||
}
|
||||
}
|
||||
|
||||
// panics is a model whose Find panics, as AstrLink Guard's tokenizer did
|
||||
// on a stray byte (#4553).
|
||||
type panics struct{ fake }
|
||||
|
||||
func (panics) Find(context.Context, []string) ([][]redact.Found, error) {
|
||||
var s []int
|
||||
_ = s[3]
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// A model that panics is the request's error, not a panic on find's own
|
||||
// goroutine, which ended magpie on Windows (#4553).
|
||||
func TestModelPanicIsTheRequestsError(t *testing.T) {
|
||||
_, err := find(context.Background(), panics{fake{installed: true}}, []string{"hello \xff"})
|
||||
if err == nil || !strings.Contains(err.Error(), "failed reading this request") {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -371,7 +371,7 @@ func (guardModel) Warm() {
|
||||
go func() {
|
||||
guardBackend.turn <- struct{}{}
|
||||
defer func() { <-guardBackend.turn }()
|
||||
guardClassify([]string{"warm"})
|
||||
classifySafely(guardClassify, []string{"warm"})
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -366,3 +366,39 @@ func TestGuardFilesAreThePublishers(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// panicFinder panics on a text holding "boom", as the tokenizer did on
|
||||
// #4553's stray byte, and reads every other.
|
||||
type panicFinder struct{ fakeFinder }
|
||||
|
||||
func (f panicFinder) Find(text string) []guard.Span {
|
||||
if strings.Contains(text, "boom") {
|
||||
var s []int
|
||||
_ = s[3] // the slice bounds of #4553's panic
|
||||
}
|
||||
return f.fakeFinder.Find(text)
|
||||
}
|
||||
|
||||
// A model that panics reading one request fails that request, held back
|
||||
// as for a model that failed, instead of ending magpie from the read's
|
||||
// goroutine (#4553), and isn't counted as failed for the requests after
|
||||
// it, which it reads.
|
||||
func TestGuardPanicFailsOnlyItsRequest(t *testing.T) {
|
||||
guardSandbox(t)
|
||||
fakeGuardFiles(t)
|
||||
var reads atomic.Int32
|
||||
guardLoad = func(string) (spanFinder, error) {
|
||||
return panicFinder{fakeFinder{map[string]string{"13800138000": "phone"}, &reads}}, nil
|
||||
}
|
||||
_, err := Of(AstrLinkGuard).Find(context.Background(), []string{"boom \xff"})
|
||||
if err == nil || !strings.Contains(err.Error(), "failed reading this request") {
|
||||
t.Fatalf("err = %v", err)
|
||||
}
|
||||
got, err := Of(AstrLinkGuard).Find(context.Background(), []string{"call 13800138000"})
|
||||
if err != nil || len(got) != 1 || len(got[0]) != 1 || got[0][0].Label != "private_phone" {
|
||||
t.Fatalf("the next request: %v, %v", got, err)
|
||||
}
|
||||
if st := Of(AstrLinkGuard).Now(); st.Failed != "" {
|
||||
t.Fatalf("Now = %+v after one request's panic", st)
|
||||
}
|
||||
}
|
||||
|
||||
+19
-1
@@ -6,6 +6,8 @@ import (
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"runtime/debug"
|
||||
"slices"
|
||||
"sort"
|
||||
"strings"
|
||||
@@ -247,7 +249,7 @@ func (b *backend) find(ctx context.Context, texts []string) ([][]redact.Found, e
|
||||
done <- result{nil, fmt.Errorf("the local masking model has no text to read for %d texts", len(miss))}
|
||||
return
|
||||
}
|
||||
found, err := b.classify(chunks[:1])
|
||||
found, err := classifySafely(b.classify, chunks[:1])
|
||||
if err != nil {
|
||||
release()
|
||||
done <- result{nil, err}
|
||||
@@ -280,6 +282,22 @@ func (b *backend) find(ctx context.Context, texts []string) ([][]redact.Found, e
|
||||
}
|
||||
}
|
||||
|
||||
// classifySafely is classify(texts), with a panic in it this request's
|
||||
// error: it runs on a goroutine of the request's find, where a panic
|
||||
// would end magpie and every request it serves (#4553, a stray byte in
|
||||
// an embeddings input on Windows). The request is held back, as for a
|
||||
// model that failed, and the model isn't counted as failed for the
|
||||
// requests after it.
|
||||
func classifySafely(classify func([]string) ([][]redact.Found, error), texts []string) (out [][]redact.Found, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
log.Printf("pii: the local model panicked reading a request: %v\n%s", r, debug.Stack())
|
||||
out, err = nil, fmt.Errorf("the local masking model failed reading this request (%v)", r)
|
||||
}
|
||||
}()
|
||||
return classify(texts)
|
||||
}
|
||||
|
||||
// still is which of texts' miss are still missing, with got (this
|
||||
// call's answers) and what others' requests found meanwhile; out gets
|
||||
// what is known of the rest.
|
||||
|
||||
@@ -250,7 +250,8 @@ func (m *qmat) matmul(x []float32, rows, t int) []float32 {
|
||||
return out
|
||||
}
|
||||
|
||||
// parallel calls f over [0, n) in t parts at once.
|
||||
// parallel calls f over [0, n) in t parts at once; a panic in one is
|
||||
// panicked again here once all have ended.
|
||||
func parallel(t, n int, f func(a, b int)) {
|
||||
t = min(t, n)
|
||||
if t <= 1 {
|
||||
@@ -258,15 +259,18 @@ func parallel(t, n int, f func(a, b int)) {
|
||||
return
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
var c carried
|
||||
step := (n + t - 1) / t
|
||||
for a := 0; a < n; a += step {
|
||||
wg.Add(1)
|
||||
go func(a, b int) {
|
||||
defer wg.Done()
|
||||
defer c.catch()
|
||||
f(a, b)
|
||||
}(a, min(n, a+step))
|
||||
}
|
||||
wg.Wait()
|
||||
c.rethrow()
|
||||
}
|
||||
|
||||
// layerNorm sets each row of x to its layer norm (epsilon 1e-12).
|
||||
|
||||
@@ -88,3 +88,49 @@ func TestFindsAtOnceReadAsOneAtATime(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -198,16 +198,21 @@ func (m *Model) Find(text string) []Span {
|
||||
at := min(Cores(), len(starts))
|
||||
var next atomic.Int32
|
||||
var wg sync.WaitGroup
|
||||
var c carried
|
||||
for range at {
|
||||
wg.Go(func() {
|
||||
defer c.catch()
|
||||
for w := int(next.Add(1)) - 1; w < len(starts); w = int(next.Add(1)) - 1 {
|
||||
procs := take(max(1, Cores()/at))
|
||||
lgs[w] = m.net.logits(ids(w), procs)
|
||||
give(procs)
|
||||
func() {
|
||||
procs := take(max(1, Cores()/at))
|
||||
defer give(procs)
|
||||
lgs[w] = m.net.logits(ids(w), procs)
|
||||
}()
|
||||
}
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
c.rethrow()
|
||||
}
|
||||
lab := make([]int, n)
|
||||
prob := make([]float64, n)
|
||||
@@ -242,6 +247,26 @@ func (m *Model) Find(text string) []Span {
|
||||
return decode(toks, lab, prob)
|
||||
}
|
||||
|
||||
// carried is the first panic in the goroutines a read starts, panicked
|
||||
// again by rethrow on the goroutine that waited for them, where Find's
|
||||
// caller can recover it: a panic left on a goroutine of its own ends the
|
||||
// whole process (#4553 ended magpie on Windows).
|
||||
type carried struct{ p atomic.Pointer[any] }
|
||||
|
||||
// catch is deferred by each goroutine.
|
||||
func (c *carried) catch() {
|
||||
if r := recover(); r != nil {
|
||||
c.p.CompareAndSwap(nil, &r)
|
||||
}
|
||||
}
|
||||
|
||||
// rethrow is called once they have all ended.
|
||||
func (c *carried) rethrow() {
|
||||
if p := c.p.Load(); p != nil {
|
||||
panic(*p)
|
||||
}
|
||||
}
|
||||
|
||||
// decode reads the tokens' BIO tags (lab, by index into labels) into
|
||||
// spans: a B tag, or an I tag of another kind than the span before it,
|
||||
// starts one; an I tag of the same kind extends it; O ends it. A span's
|
||||
|
||||
@@ -375,7 +375,7 @@ func Warm() {
|
||||
go func() {
|
||||
turn <- struct{}{}
|
||||
defer func() { <-turn }()
|
||||
classify([]string{"warm"})
|
||||
classifySafely(classify, []string{"warm"})
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user