Make restricted artifact reads cooperatively cancellable
This commit is contained in:
@@ -28,6 +28,49 @@ type fakeRunner struct {
|
||||
|
||||
type runnerFunc func(context.Context, promptkit.RunRequest) (*promptkit.RunResult, error)
|
||||
|
||||
type cancelingArtifactReader struct {
|
||||
cancel context.CancelFunc
|
||||
reader promptkit.ArtifactReader
|
||||
}
|
||||
|
||||
func (r cancelingArtifactReader) Read(ctx context.Context, ref promptkit.ArtifactRef) (*promptkit.Artifact, error) {
|
||||
r.cancel()
|
||||
return r.reader.Read(ctx, ref)
|
||||
}
|
||||
|
||||
func TestHandlerCancellationDuringRestrictedArtifactAcquisition(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := os.WriteFile(filepath.Join(root, "input.txt"), []byte("input"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reader, err := NewRestrictedArtifactReader(root, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
client := &blockingLLMClient{started: make(chan struct{}, 1), release: make(chan struct{})}
|
||||
close(client.release)
|
||||
engine := newHandlerEngineWithOptions(t, promptkit.WithArtifactReader(cancelingArtifactReader{cancel: cancel, reader: reader}), promptkit.WithLLMClient(client))
|
||||
var runErr error
|
||||
h := NewHandler(runnerFunc(func(ctx context.Context, req promptkit.RunRequest) (*promptkit.RunResult, error) {
|
||||
result, err := engine.Run(ctx, req)
|
||||
runErr = err
|
||||
return result, err
|
||||
}))
|
||||
w := httptest.NewRecorder()
|
||||
h.ServeHTTP(w, httptest.NewRequestWithContext(ctx, http.MethodPost, "/v1/runs", strings.NewReader(`{"prompt_id":"p","inputs":{"input":{"type":"file","uri":"input.txt"}}}`)))
|
||||
assertHTTPErrorCode(t, w, http.StatusRequestTimeout, "request_canceled")
|
||||
if !errors.Is(runErr, context.Canceled) {
|
||||
t.Fatalf("engine lost cancellation identity: %v", runErr)
|
||||
}
|
||||
select {
|
||||
case <-client.started:
|
||||
t.Fatal("provider was dispatched after cancellation")
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
func (run runnerFunc) Run(ctx context.Context, req promptkit.RunRequest) (*promptkit.RunResult, error) {
|
||||
return run(ctx, req)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user