Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix(models): honour context cancellation in driver HTTP requests (#21643)

CCLUCKY29903c86f5d5
diff --git a/internal/entity/models/astraflow.go b/internal/entity/models/astraflow.go
index 8b06c4d807..90927a0488 100644
--- a/internal/entity/models/astraflow.go
+++ b/internal/entity/models/astraflow.go
@@ -201,7 +201,7 @@ func (a *AstraflowModel) Embed(ctx context.Context, modelName *string, request E
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
+ req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
@@ -283,7 +283,7 @@ func (a *AstraflowModel) Rerank(ctx context.Context, modelName *string, request
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
+ req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
@@ -374,7 +374,7 @@ func (a *AstraflowModel) AudioSpeech(ctx context.Context, modelName *string, aud
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
+ req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
diff --git a/internal/entity/models/base_model.go b/internal/entity/models/base_model.go
index 640b5ec8f9..f5d9c199b9 100644
--- a/internal/entity/models/base_model.go
+++ b/internal/entity/models/base_model.go
@@ -524,6 +524,32 @@ func ParseListModel(modelList ModelList) []ListModelResponse {
return FillMissingModelTypes(models)
}
+const (
+ // streamTTSErrorBodyBytes caps the part of a non-200 streaming-TTS response
+ // that is quoted in the error message, so a large error page cannot inflate
+ // the error string.
+ streamTTSErrorBodyBytes = 1024
+ // streamTTSErrorBodyDrainBytes bounds the follow-up read that lets a short
+ // error page release its connection for reuse.
+ streamTTSErrorBodyDrainBytes = 4 << 10
+)
+
+// readStreamTTSErrorBody captures the head of a failed streaming-TTS response
+// for the error message, then drains a bounded remainder so the transport can
+// reuse the connection. A read failure in either phase is returned with what was
+// captured so far: reading only the head can succeed on a body that is later
+// cut short.
+func readStreamTTSErrorBody(body io.Reader) ([]byte, error) {
+ head, err := io.ReadAll(io.LimitReader(body, streamTTSErrorBodyBytes))
+ if err != nil {
+ return head, err
+ }
+ if _, err := io.Copy(io.Discard, io.LimitReader(body, streamTTSErrorBodyDrainBytes)); err != nil {
+ return head, err
+ }
+ return head, nil
+}
+
// PostJSONRequest marshals body to JSON, creates a POST request to url
func PostJSONRequest(ctx context.Context, client *http.Client, url, auth string, body map[string]interface{}) (*http.Response, error) {
data, err := json.Marshal(body)
diff --git a/internal/entity/models/builtin.go b/internal/entity/models/builtin.go
index 2de9a4b1d7..13dc4e76d4 100644
--- a/internal/entity/models/builtin.go
+++ b/internal/entity/models/builtin.go
@@ -74,7 +74,7 @@ func (b *BuiltinModel) Embed(ctx context.Context, modelName *string, request Emb
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
+ req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, err
}
diff --git a/internal/entity/models/cometapi.go b/internal/entity/models/cometapi.go
index 9bfeae6ef6..1828d933ba 100644
--- a/internal/entity/models/cometapi.go
+++ b/internal/entity/models/cometapi.go
@@ -425,7 +425,7 @@ func (c *CometAPIModel) TranscribeAudio(ctx context.Context, modelName *string,
}
// build request
- req, err := http.NewRequest("POST", baseURL, &body)
+ req, err := http.NewRequestWithContext(ctx, "POST", baseURL, &body)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
@@ -500,7 +500,7 @@ func (c *CometAPIModel) AudioSpeech(ctx context.Context, modelName *string, audi
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
- req, err := http.NewRequest("POST", baseURL, bytes.NewBuffer(jsonData))
+ req, err := http.NewRequestWithContext(ctx, "POST", baseURL, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
diff --git a/internal/entity/models/context_cancel_test.go b/internal/entity/models/context_cancel_test.go
new file mode 100644
index 0000000000..696f0848eb
--- /dev/null
+++ b/internal/entity/models/context_cancel_test.go
@@ -0,0 +1,242 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package models
+
+import (
+ "context"
+ "errors"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "os"
+ "path/filepath"
+ "testing"
+ "time"
+)
+
+// TestDriverRequestsHonourContextCancellation pins that a cancelled run also
+// aborts the upstream HTTP call. A request built with http.NewRequest instead
+// of NewRequestWithContext keeps the connection open until the client timeout
+// even after the caller gave up, so a cancelled ingest or chat keeps billing
+// the provider. Drop a site from that pattern and its subtest hangs until the
+// server's bounded wait expires.
+func TestDriverRequestsHonourContextCancellation(t *testing.T) {
+ withSSRFBypass(t)
+
+ // The handler waits for the client to cancel its own request context, but
+ // bounded so a request that ignores cancellation cannot hang the suite.
+ // release lets cleanup end the handler immediately instead of waiting out
+ // the bound.
+ blocking := func(t *testing.T) *httptest.Server {
+ t.Helper()
+ release := make(chan struct{})
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ select {
+ case <-r.Context().Done():
+ case <-release:
+ case <-time.After(500 * time.Millisecond):
+ }
+ }))
+ t.Cleanup(srv.Close)
+ t.Cleanup(func() { close(release) })
+ return srv
+ }
+ run := func(t *testing.T, call func(context.Context) error) {
+ t.Helper()
+ ctx, cancel := context.WithCancel(t.Context())
+ defer cancel()
+ time.AfterFunc(50*time.Millisecond, cancel)
+ start := time.Now()
+ err := call(ctx)
+ if !errors.Is(err, context.Canceled) {
+ t.Fatalf("err = %v, want context.Canceled", err)
+ }
+ if elapsed := time.Since(start); elapsed > 400*time.Millisecond {
+ t.Fatalf("call returned after %v; the request ignored the cancelled context", elapsed)
+ }
+ }
+ apiConfig := func() *APIConfig {
+ return &APIConfig{ApiKey: ptr("test-key")}
+ }
+
+ t.Run("builtin embed", func(t *testing.T) {
+ srv := blocking(t)
+ m := NewBuiltinModel(srv.URL, "m")
+ run(t, func(ctx context.Context) error {
+ _, err := m.Embed(ctx, ptr("m"), EmbedRequest{Texts: []string{"hello"}}, apiConfig(), nil, nil)
+ return err
+ })
+ })
+
+ t.Run("xai transcribe audio", func(t *testing.T) {
+ srv := blocking(t)
+ m := NewXAIModel(map[string]string{"default": srv.URL}, URLSuffix{ASR: "asr"})
+ audio := filepath.Join(t.TempDir(), "clip.mp3")
+ if err := os.WriteFile(audio, []byte("audio"), 0o600); err != nil {
+ t.Fatalf("write audio fixture: %v", err)
+ }
+ run(t, func(ctx context.Context) error {
+ _, err := m.TranscribeAudio(ctx, ptr("m"), ptr(audio), apiConfig(), nil, nil)
+ return err
+ })
+ })
+
+ t.Run("groq audio speech", func(t *testing.T) {
+ srv := blocking(t)
+ m := NewGroqModel(map[string]string{"default": srv.URL}, URLSuffix{TTS: "tts"})
+ run(t, func(ctx context.Context) error {
+ _, err := m.AudioSpeech(ctx, ptr("voice"), ptr("hello"), apiConfig(), nil, nil)
+ return err
+ })
+ })
+
+ t.Run("togetherai rerank", func(t *testing.T) {
+ srv := blocking(t)
+ m := NewTogetherAIModel(map[string]string{"default": srv.URL}, URLSuffix{Rerank: "rerank"})
+ run(t, func(ctx context.Context) error {
+ _, err := m.Rerank(ctx, ptr("m"), RerankRequest{Query: "q", Documents: []string{"a"}}, apiConfig(), &RerankConfig{}, nil)
+ return err
+ })
+ })
+
+ t.Run("jina list models", func(t *testing.T) {
+ srv := blocking(t)
+ m := NewJinaModel(map[string]string{"default": srv.URL}, URLSuffix{Models: "models"})
+ run(t, func(ctx context.Context) error {
+ _, err := m.ListModels(ctx, apiConfig())
+ return err
+ })
+ })
+
+ t.Run("novita balance", func(t *testing.T) {
+ srv := blocking(t)
+ m := NewNovitaModel(map[string]string{"default": srv.URL}, URLSuffix{Balance: "balance"})
+ run(t, func(ctx context.Context) error {
+ _, err := m.Balance(ctx, apiConfig())
+ return err
+ })
+ })
+
+ // A non-200 body that fails mid-read must report that failure. Discarding the
+ // read error turns an interrupted body (or a cancellation) into a bare status
+ // line, which hides why the retry-worthy call actually failed.
+ truncatedError := func(t *testing.T) *httptest.Server {
+ t.Helper()
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ conn, buf, err := w.(http.Hijacker).Hijack()
+ if err != nil {
+ t.Errorf("hijack: %v", err)
+ return
+ }
+ defer conn.Close()
+ // Promise 64 bytes and send 7: the first read succeeds and only a read
+ // that runs to EOF sees the truncation, so a single-read driver reports
+ // the status line as if the body were complete.
+ buf.WriteString("HTTP/1.1 500 Internal Server Error\r\nContent-Length: 64\r\n\r\npartial")
+ buf.Flush()
+ }))
+ t.Cleanup(srv.Close)
+ return srv
+ }
+ noopSender := func(*string, *string) error { return nil }
+
+ t.Run("stepfun stream error body read failure", func(t *testing.T) {
+ srv := truncatedError(t)
+ m := NewStepFunModel(map[string]string{"default": srv.URL}, URLSuffix{TTS: "tts"})
+ err := m.AudioSpeechWithSender(t.Context(), ptr("voice"), ptr("hello"), &APIConfig{ApiKey: ptr("test-key")}, nil, nil, noopSender)
+ if !errors.Is(err, io.ErrUnexpectedEOF) {
+ t.Fatalf("err = %v, want io.ErrUnexpectedEOF", err)
+ }
+ })
+
+ t.Run("togetherai stream error body read failure", func(t *testing.T) {
+ srv := truncatedError(t)
+ m := NewTogetherAIModel(map[string]string{"default": srv.URL}, URLSuffix{TTS: "tts"})
+ err := m.AudioSpeechWithSender(t.Context(), ptr("voice"), ptr("hello"), &APIConfig{ApiKey: ptr("test-key")}, nil, nil, noopSender)
+ if !errors.Is(err, io.ErrUnexpectedEOF) {
+ t.Fatalf("err = %v, want io.ErrUnexpectedEOF", err)
+ }
+ })
+
+ t.Run("fishaudio stream error body read failure", func(t *testing.T) {
+ srv := truncatedError(t)
+ m := NewFishAudioModel(map[string]string{"default": srv.URL}, URLSuffix{TTS: "tts"})
+ err := m.AudioSpeechWithSender(t.Context(), ptr("voice"), ptr("hello"), &APIConfig{ApiKey: ptr("test-key")}, nil, nil, noopSender)
+ if !errors.Is(err, io.ErrUnexpectedEOF) {
+ t.Fatalf("err = %v, want io.ErrUnexpectedEOF", err)
+ }
+ })
+}
+
+// TestStreamTTSErrorBodyReadIsBounded pins that a non-200 streaming-TTS body
+// that stalls mid-read cannot pin the call. The transport only interrupts a
+// body read when the request context ends, so the drivers carry
+// streamCallTimeout; without it the read blocks until the caller cancels.
+func TestStreamTTSErrorBodyReadIsBounded(t *testing.T) {
+ withSSRFBypass(t)
+
+ stalledError := func(t *testing.T) *httptest.Server {
+ t.Helper()
+ release := make(chan struct{})
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ conn, buf, err := w.(http.Hijacker).Hijack()
+ if err != nil {
+ t.Errorf("hijack: %v", err)
+ return
+ }
+ defer conn.Close()
+ // Promise a body and send none: the header block completes, so the
+ // following read can only end when the request context does.
+ buf.WriteString("HTTP/1.1 500 Internal Server Error\r\nContent-Length: 64\r\n\r\n")
+ buf.Flush()
+ <-release
+ }))
+ t.Cleanup(srv.Close)
+ t.Cleanup(func() { close(release) })
+ return srv
+ }
+ noopSender := func(*string, *string) error { return nil }
+
+ cases := map[string]func(t *testing.T) error{
+ "stepfun": func(t *testing.T) error {
+ srv := stalledError(t)
+ m := NewStepFunModel(map[string]string{"default": srv.URL}, URLSuffix{TTS: "tts"})
+ return m.AudioSpeechWithSender(context.Background(), ptr("voice"), ptr("hello"), &APIConfig{ApiKey: ptr("test-key")}, nil, nil, noopSender)
+ },
+ "togetherai": func(t *testing.T) error {
+ srv := stalledError(t)
+ m := NewTogetherAIModel(map[string]string{"default": srv.URL}, URLSuffix{TTS: "tts"})
+ return m.AudioSpeechWithSender(context.Background(), ptr("voice"), ptr("hello"), &APIConfig{ApiKey: ptr("test-key")}, nil, nil, noopSender)
+ },
+ }
+
+ withTestTimeouts(t, time.Minute, 100*time.Millisecond)
+ for name, call := range cases {
+ t.Run(name, func(t *testing.T) {
+ done := make(chan error, 1)
+ go func() { done <- call(t) }()
+ select {
+ case err := <-done:
+ if err == nil {
+ t.Fatal("want an error from the stalled error body")
+ }
+ case <-time.After(5 * time.Second):
+ t.Fatal("stalled error body outlived the stream deadline")
+ }
+ })
+ }
+}
diff --git a/internal/entity/models/fishaudio.go b/internal/entity/models/fishaudio.go
index b11f5dbad9..24aef65e66 100644
--- a/internal/entity/models/fishaudio.go
+++ b/internal/entity/models/fishaudio.go
@@ -296,9 +296,11 @@ func (f *FishAudioModel) AudioSpeechWithSender(ctx context.Context, modelName *s
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
- buf := make([]byte, 1024)
- n, _ := resp.Body.Read(buf)
- return fmt.Errorf("FishAudio stream API error: %d - %s", resp.StatusCode, string(buf[:n]))
+ body, err := readStreamTTSErrorBody(resp.Body)
+ if err != nil {
+ return fmt.Errorf("FishAudio stream API error: %d - %s: %w", resp.StatusCode, string(body), err)
+ }
+ return fmt.Errorf("FishAudio stream API error: %d - %s", resp.StatusCode, string(body))
}
if _, err := ParseSSEStream[struct {
diff --git a/internal/entity/models/groq.go b/internal/entity/models/groq.go
index aac79123ac..78179d96d7 100644
--- a/internal/entity/models/groq.go
+++ b/internal/entity/models/groq.go
@@ -312,7 +312,7 @@ func (g *GroqModel) TranscribeAudio(ctx context.Context, modelName *string, file
}
// build request
- req, err := http.NewRequest("POST", url, &body)
+ req, err := http.NewRequestWithContext(ctx, "POST", url, &body)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
@@ -385,7 +385,7 @@ func (g *GroqModel) AudioSpeech(ctx context.Context, modelName *string, audioCon
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
+ req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
diff --git a/internal/entity/models/jina.go b/internal/entity/models/jina.go
index 0d7c282913..77638d5157 100644
--- a/internal/entity/models/jina.go
+++ b/internal/entity/models/jina.go
@@ -625,7 +625,7 @@ func (j *JinaModel) ListModels(ctx context.Context, apiConfig *APIConfig) ([]Lis
return nil, err
}
- req, err := http.NewRequest("GET", url, nil)
+ req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
diff --git a/internal/entity/models/novita.go b/internal/entity/models/novita.go
index 507231e7e7..7161687a14 100644
--- a/internal/entity/models/novita.go
+++ b/internal/entity/models/novita.go
@@ -595,7 +595,7 @@ func (n *NovitaModel) Balance(ctx context.Context, apiConfig *APIConfig) (map[st
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
- req, err := http.NewRequest("GET", url, bytes.NewBuffer(jsonData))
+ req, err := http.NewRequestWithContext(ctx, "GET", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
diff --git a/internal/entity/models/stepfun.go b/internal/entity/models/stepfun.go
index d35e89ff45..ecd86e0a8e 100644
--- a/internal/entity/models/stepfun.go
+++ b/internal/entity/models/stepfun.go
@@ -220,7 +220,7 @@ func (s *StepFunModel) AudioSpeech(ctx context.Context, modelName *string, audio
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
+ req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
@@ -281,7 +281,16 @@ func (s *StepFunModel) AudioSpeechWithSender(ctx context.Context, modelName *str
return fmt.Errorf("failed to marshal request: %w", err)
}
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
+ // The shared client sets no overall Timeout and the transport only
+ // interrupts a response-body read when the request ends, so a body that
+ // stalls mid-read would otherwise pin this call until the caller cancels.
+ // streamCallTimeout is the streaming ceiling, not a tight error-page bound:
+ // it ends the unbounded case, and the caller's context still ends it
+ // earlier. FishAudio's streaming TTS carries the same deadline.
+ ctx, cancel := context.WithTimeout(ctx, streamCallTimeout)
+ defer cancel()
+
+ req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
@@ -296,7 +305,10 @@ func (s *StepFunModel) AudioSpeechWithSender(ctx context.Context, modelName *str
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
- body, _ := io.ReadAll(resp.Body)
+ body, err := readStreamTTSErrorBody(resp.Body)
+ if err != nil {
+ return fmt.Errorf("StepFun stream TTS API error: %d (body: %q): %w", resp.StatusCode, string(body), err)
+ }
return fmt.Errorf("StepFun stream TTS API error: %d - %s", resp.StatusCode, string(body))
}
diff --git a/internal/entity/models/stream_tts_error_body_test.go b/internal/entity/models/stream_tts_error_body_test.go
new file mode 100644
index 0000000000..7e3a23c8f5
--- /dev/null
+++ b/internal/entity/models/stream_tts_error_body_test.go
@@ -0,0 +1,83 @@
+//
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+package models
+
+import (
+ "errors"
+ "io"
+ "strings"
+ "testing"
+ "testing/iotest"
+)
+
+func TestReadStreamTTSErrorBody(t *testing.T) {
+ t.Run("short body is captured whole", func(t *testing.T) {
+ body := strings.NewReader(strings.Repeat("a", 300))
+ head, err := readStreamTTSErrorBody(body)
+ if err != nil {
+ t.Fatalf("err = %v, want nil", err)
+ }
+ if len(head) != 300 {
+ t.Fatalf("len(head) = %d, want 300", len(head))
+ }
+ if body.Len() != 0 {
+ t.Fatalf("remaining bytes = %d, want 0", body.Len())
+ }
+ })
+
+ t.Run("large body is captured to the head limit and drained to the drain limit", func(t *testing.T) {
+ const bodyBytes = 10000
+ body := strings.NewReader(strings.Repeat("a", bodyBytes))
+ head, err := readStreamTTSErrorBody(body)
+ if err != nil {
+ t.Fatalf("err = %v, want nil", err)
+ }
+ if len(head) != streamTTSErrorBodyBytes {
+ t.Fatalf("len(head) = %d, want %d", len(head), streamTTSErrorBodyBytes)
+ }
+ wantRemaining := int64(bodyBytes - streamTTSErrorBodyBytes - streamTTSErrorBodyDrainBytes)
+ if int64(body.Len()) != wantRemaining {
+ t.Fatalf("remaining bytes = %d, want %d", body.Len(), wantRemaining)
+ }
+ })
+
+ t.Run("head read failure is returned with the captured bytes", func(t *testing.T) {
+ errBoom := errors.New("boom")
+ head, err := readStreamTTSErrorBody(iotest.ErrReader(errBoom))
+ if !errors.Is(err, errBoom) {
+ t.Fatalf("err = %v, want %v", err, errBoom)
+ }
+ if len(head) != 0 {
+ t.Fatalf("len(head) = %d, want 0", len(head))
+ }
+ })
+
+ t.Run("drain read failure is returned with the captured bytes", func(t *testing.T) {
+ errBoom := errors.New("boom")
+ body := io.MultiReader(
+ strings.NewReader(strings.Repeat("a", streamTTSErrorBodyBytes)),
+ iotest.ErrReader(errBoom),
+ )
+ head, err := readStreamTTSErrorBody(body)
+ if !errors.Is(err, errBoom) {
+ t.Fatalf("err = %v, want %v", err, errBoom)
+ }
+ if len(head) != streamTTSErrorBodyBytes {
+ t.Fatalf("len(head) = %d, want %d", len(head), streamTTSErrorBodyBytes)
+ }
+ })
+}
diff --git a/internal/entity/models/togetherai.go b/internal/entity/models/togetherai.go
index 43de0c9a91..8329d20d9b 100644
--- a/internal/entity/models/togetherai.go
+++ b/internal/entity/models/togetherai.go
@@ -319,7 +319,7 @@ func (t *TogetherAIModel) Rerank(ctx context.Context, modelName *string, request
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
+ req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
@@ -434,7 +434,7 @@ func (t *TogetherAIModel) TranscribeAudio(ctx context.Context, modelName *string
}
// request
- req, err := http.NewRequest("POST", url, &body)
+ req, err := http.NewRequestWithContext(ctx, "POST", url, &body)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
@@ -508,7 +508,7 @@ func (t *TogetherAIModel) AudioSpeech(ctx context.Context, modelName *string, au
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
+ req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
@@ -571,8 +571,17 @@ func (t *TogetherAIModel) AudioSpeechWithSender(ctx context.Context, modelName *
return fmt.Errorf("failed to marshal request: %w", err)
}
+ // The shared client sets no overall Timeout and the transport only
+ // interrupts a response-body read when the request ends, so a body that
+ // stalls mid-read would otherwise pin this call until the caller cancels.
+ // streamCallTimeout is the streaming ceiling, not a tight error-page bound:
+ // it ends the unbounded case, and the caller's context still ends it
+ // earlier. FishAudio's streaming TTS carries the same deadline.
+ ctx, cancel := context.WithTimeout(ctx, streamCallTimeout)
+ defer cancel()
+
// Build Request
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
+ req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
@@ -587,9 +596,11 @@ func (t *TogetherAIModel) AudioSpeechWithSender(ctx context.Context, modelName *
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
- buf := make([]byte, 1024)
- n, _ := resp.Body.Read(buf)
- return fmt.Errorf("TogetherAI stream API error: %d - %s", resp.StatusCode, string(buf[:n]))
+ body, err := readStreamTTSErrorBody(resp.Body)
+ if err != nil {
+ return fmt.Errorf("TogetherAI stream API error: %d - %s: %w", resp.StatusCode, string(body), err)
+ }
+ return fmt.Errorf("TogetherAI stream API error: %d - %s", resp.StatusCode, string(body))
}
if _, err := ParseSSEStream[struct {
diff --git a/internal/entity/models/xai.go b/internal/entity/models/xai.go
index 1ae8515fa9..df64124c94 100644
--- a/internal/entity/models/xai.go
+++ b/internal/entity/models/xai.go
@@ -260,7 +260,7 @@ func (x *XAIModel) TranscribeAudio(ctx context.Context, modelName *string, file
}
// build request
- req, err := http.NewRequest("POST", url, &body)
+ req, err := http.NewRequestWithContext(ctx, "POST", url, &body)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
@@ -336,7 +336,7 @@ func (x *XAIModel) AudioSpeech(ctx context.Context, modelName *string, audioCont
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
+ req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
diff --git a/internal/entity/models/xinference.go b/internal/entity/models/xinference.go
index ab0a8c145c..b27a125cb2 100644
--- a/internal/entity/models/xinference.go
+++ b/internal/entity/models/xinference.go
@@ -389,7 +389,7 @@ func (x *XinferenceModel) TranscribeAudio(ctx context.Context, modelName *string
}
// request
- req, err := http.NewRequest("POST", url, &body)
+ req, err := http.NewRequestWithContext(ctx, "POST", url, &body)
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
@@ -465,7 +465,7 @@ func (x *XinferenceModel) AudioSpeech(ctx context.Context, modelName *string, au
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
+ req, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(jsonData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}