|
| 1 | +package grpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "sync" |
| 7 | + "sync/atomic" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/mudler/LocalAI/pkg/grpc/base" |
| 11 | + pb "github.com/mudler/LocalAI/pkg/grpc/proto" |
| 12 | + . "github.com/onsi/ginkgo/v2" |
| 13 | + . "github.com/onsi/gomega" |
| 14 | +) |
| 15 | + |
| 16 | +var errGenCancelled = errors.New("generation cancelled") |
| 17 | + |
| 18 | +// cancellableBackend implements AIModel + AIModelRich + Cancellable. Its |
| 19 | +// rich predict paths optionally block until Cancel fires (blockUntilCancel), |
| 20 | +// which lets the specs prove the server's context.AfterFunc plumbing: a |
| 21 | +// cancelled request context must reach Cancel and unblock the generation. |
| 22 | +type cancellableBackend struct { |
| 23 | + base.SingleThread |
| 24 | + |
| 25 | + blockUntilCancel bool |
| 26 | + |
| 27 | + started chan struct{} // closed when a predict call is in flight |
| 28 | + startOnce sync.Once |
| 29 | + cancelled chan struct{} // closed by Cancel |
| 30 | + cancelOnce sync.Once |
| 31 | + cancelCalls atomic.Int32 |
| 32 | +} |
| 33 | + |
| 34 | +func newCancellableBackend(blockUntilCancel bool) *cancellableBackend { |
| 35 | + return &cancellableBackend{ |
| 36 | + blockUntilCancel: blockUntilCancel, |
| 37 | + started: make(chan struct{}), |
| 38 | + cancelled: make(chan struct{}), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func (c *cancellableBackend) Cancel() { |
| 43 | + c.cancelCalls.Add(1) |
| 44 | + c.cancelOnce.Do(func() { close(c.cancelled) }) |
| 45 | +} |
| 46 | + |
| 47 | +func (c *cancellableBackend) run() error { |
| 48 | + c.startOnce.Do(func() { close(c.started) }) |
| 49 | + if !c.blockUntilCancel { |
| 50 | + return nil |
| 51 | + } |
| 52 | + select { |
| 53 | + case <-c.cancelled: |
| 54 | + return errGenCancelled |
| 55 | + case <-time.After(30 * time.Second): |
| 56 | + // Backstop so a regression (Cancel never wired) fails the spec |
| 57 | + // instead of hanging the suite. |
| 58 | + return errors.New("cancellableBackend: Cancel never fired") |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func (c *cancellableBackend) PredictRich(*pb.PredictOptions) (*pb.Reply, error) { |
| 63 | + if err := c.run(); err != nil { |
| 64 | + return nil, err |
| 65 | + } |
| 66 | + return &pb.Reply{Message: []byte("done")}, nil |
| 67 | +} |
| 68 | + |
| 69 | +func (c *cancellableBackend) PredictStreamRich(_ *pb.PredictOptions, out chan<- *pb.Reply) error { |
| 70 | + out <- &pb.Reply{Message: []byte("first")} |
| 71 | + return c.run() |
| 72 | +} |
| 73 | + |
| 74 | +func (c *cancellableBackend) Predict(*pb.PredictOptions) (string, error) { |
| 75 | + return "", errors.New("cancellableBackend: legacy Predict should not have been called") |
| 76 | +} |
| 77 | + |
| 78 | +func (c *cancellableBackend) PredictStream(*pb.PredictOptions, chan string) error { |
| 79 | + return errors.New("cancellableBackend: legacy PredictStream should not have been called") |
| 80 | +} |
| 81 | + |
| 82 | +var _ AIModelRich = (*cancellableBackend)(nil) |
| 83 | +var _ Cancellable = (*cancellableBackend)(nil) |
| 84 | + |
| 85 | +var _ = Describe("Cancellable capability", func() { |
| 86 | + It("PredictStream: cancelling the request context fires Cancel and ends the stream with the backend's error", func() { |
| 87 | + backend := newCancellableBackend(true) |
| 88 | + addr := "test://cancel-stream" |
| 89 | + Provide(addr, backend) |
| 90 | + c := NewClient(addr, true, nil, false) |
| 91 | + |
| 92 | + ctx, cancel := context.WithCancel(context.Background()) |
| 93 | + defer cancel() |
| 94 | + |
| 95 | + errCh := make(chan error, 1) |
| 96 | + go func() { |
| 97 | + defer GinkgoRecover() |
| 98 | + errCh <- c.PredictStream(ctx, &pb.PredictOptions{}, func(*pb.Reply) {}) |
| 99 | + }() |
| 100 | + |
| 101 | + // Only cancel once the generation is provably in flight; cancelling |
| 102 | + // earlier would race the AfterFunc registration in the server. |
| 103 | + Eventually(backend.started, "5s").Should(BeClosed()) |
| 104 | + cancel() |
| 105 | + |
| 106 | + var err error |
| 107 | + Eventually(errCh, "5s").Should(Receive(&err)) |
| 108 | + Expect(err).To(MatchError(errGenCancelled)) |
| 109 | + Expect(backend.cancelCalls.Load()).To(BeNumerically(">=", 1)) |
| 110 | + }) |
| 111 | + |
| 112 | + It("Predict: cancelling the request context fires Cancel and unblocks the call", func() { |
| 113 | + backend := newCancellableBackend(true) |
| 114 | + addr := "test://cancel-predict" |
| 115 | + Provide(addr, backend) |
| 116 | + c := NewClient(addr, true, nil, false) |
| 117 | + |
| 118 | + ctx, cancel := context.WithCancel(context.Background()) |
| 119 | + defer cancel() |
| 120 | + |
| 121 | + errCh := make(chan error, 1) |
| 122 | + go func() { |
| 123 | + defer GinkgoRecover() |
| 124 | + _, err := c.Predict(ctx, &pb.PredictOptions{}) |
| 125 | + errCh <- err |
| 126 | + }() |
| 127 | + |
| 128 | + Eventually(backend.started, "5s").Should(BeClosed()) |
| 129 | + cancel() |
| 130 | + |
| 131 | + var err error |
| 132 | + Eventually(errCh, "5s").Should(Receive(&err)) |
| 133 | + Expect(err).To(MatchError(errGenCancelled)) |
| 134 | + Expect(backend.cancelCalls.Load()).To(BeNumerically(">=", 1)) |
| 135 | + }) |
| 136 | + |
| 137 | + It("does not call Cancel when the request completes normally", func() { |
| 138 | + backend := newCancellableBackend(false) |
| 139 | + addr := "test://cancel-clean" |
| 140 | + Provide(addr, backend) |
| 141 | + c := NewClient(addr, true, nil, false) |
| 142 | + |
| 143 | + ctx, cancel := context.WithCancel(context.Background()) |
| 144 | + |
| 145 | + var replies []*pb.Reply |
| 146 | + err := c.PredictStream(ctx, &pb.PredictOptions{}, func(r *pb.Reply) { |
| 147 | + replies = append(replies, r) |
| 148 | + }) |
| 149 | + Expect(err).ToNot(HaveOccurred()) |
| 150 | + Expect(replies).To(HaveLen(1)) |
| 151 | + |
| 152 | + // Cancelling AFTER completion must not reach the backend: the |
| 153 | + // deferred AfterFunc stop de-registered the hook, so a shared or |
| 154 | + // reused context cannot abort someone else's later generation. |
| 155 | + cancel() |
| 156 | + Consistently(backend.cancelCalls.Load, "200ms").Should(BeZero()) |
| 157 | + }) |
| 158 | +}) |
0 commit comments