xonecas.io
6312
Ga

go-opencode-ai-zen-sdk / commit c28731c

fix: another fix with timeouts...

xonecas2026-02-19 c28731c633ea493bbdc27fade4f9b01ce233807d parent 7bc66d7

3 files changed

client.go+28−1
@@ -3,8 +3,18 @@ package zen
33 import (
44 "context"
55 "net/http"
6+ "time"
67 )
78
9+// defaultResponseHeaderTimeout is applied to the transport of the internally
10+// created HTTP client when the caller does not supply their own *http.Client.
11+// It guards against a server that accepts the TCP connection but never sends
12+// response headers (hung server / network black-hole). It deliberately does
13+// NOT use http.Client.Timeout, which is a total round-trip deadline that fires
14+// while the response body is still being read and therefore kills long-running
15+// SSE streams.
16+const defaultResponseHeaderTimeout = 30 * time.Second
17+
818 type Client struct {
919 cfg Config
1020 httpClient *http.Client
@@ -17,7 +27,24 @@ func NewClient(cfg Config) (*Client, error) {
1727
1828 httpClient := cfg.HTTPClient
1929 if httpClient == nil {
20- httpClient = &http.Client{Timeout: cfg.Timeout}
30+ // Clone the default transport so we can set ResponseHeaderTimeout
31+ // without mutating the global. Use a comma-ok assertion so we don't
32+ // panic if http.DefaultTransport has been replaced (e.g. by test
33+ // helpers or instrumentation libraries such as go-vcr / otelhttp).
34+ var transport *http.Transport
35+ if t, ok := http.DefaultTransport.(*http.Transport); ok {
36+ transport = t.Clone()
37+ } else {
38+ transport = &http.Transport{}
39+ }
40+ transport.ResponseHeaderTimeout = defaultResponseHeaderTimeout
41+ httpClient = &http.Client{
42+ Transport: transport,
43+ // cfg.Timeout is 0 by default (unlimited). If the caller sets it
44+ // explicitly they accept that it will terminate the body read —
45+ // document this clearly in Config.Timeout's godoc.
46+ Timeout: cfg.Timeout,
47+ }
2148 }
2249
2350 return &Client{
config.go+13−4
@@ -19,10 +19,19 @@ type Config struct {
1919 APIKey string
2020 BaseURL string
2121 // Timeout sets http.Client.Timeout on the internal HTTP client.
22- // Leave at 0 (the default) for streaming calls — http.Client.Timeout is a
23- // total round-trip deadline that fires while the response body is still being
24- // read, which breaks SSE streams that run longer than the timeout.
25- // Use the request context to enforce deadlines on streaming calls instead.
22+ //
23+ // WARNING: http.Client.Timeout is a total round-trip deadline — it starts
24+ // when the request is sent and fires while the response body is still being
25+ // read. Setting a non-zero value WILL kill SSE/streaming responses that
26+ // run longer than the deadline with "context deadline exceeded".
27+ //
28+ // Leave at 0 (the default) for streaming calls and enforce deadlines via
29+ // the request context instead (e.g. context.WithTimeout / WithDeadline).
30+ //
31+ // The internal transport always applies a 30-second
32+ // ResponseHeaderTimeout independently of this field, protecting against
33+ // servers that accept the connection but never send headers. This field
34+ // has no effect when HTTPClient is supplied by the caller.
2635 Timeout time.Duration
2736 UserAgent string
2837 HTTPClient *http.Client
stream_test.go+81−0
@@ -6,6 +6,7 @@ import (
66 "net/http/httptest"
77 "strings"
88 "testing"
9+ "time"
910 )
1011
1112 func TestStreamParsing(t *testing.T) {
@@ -54,3 +55,83 @@ func TestStreamParsing(t *testing.T) {
5455 t.Fatalf("event data mismatch: %s", string(events[0].Data))
5556 }
5657 }
58+
59+// TestDefaultClientHasNoBodyTimeout asserts that the http.Client constructed
60+// by NewClient has Timeout == 0 (unlimited body read) and that its transport
61+// carries a ResponseHeaderTimeout. This is the regression test for the bug
62+// where a 60-second http.Client.Timeout killed long-running SSE streams.
63+func TestDefaultClientHasNoBodyTimeout(t *testing.T) {
64+ c, err := NewClient(Config{APIKey: "key"})
65+ if err != nil {
66+ t.Fatalf("NewClient: %v", err)
67+ }
68+
69+ if c.httpClient.Timeout != 0 {
70+ t.Fatalf("expected http.Client.Timeout == 0 (unlimited), got %v", c.httpClient.Timeout)
71+ }
72+
73+ transport, ok := c.httpClient.Transport.(*http.Transport)
74+ if !ok {
75+ t.Fatalf("expected *http.Transport, got %T", c.httpClient.Transport)
76+ }
77+
78+ if transport.ResponseHeaderTimeout == 0 {
79+ t.Fatal("expected ResponseHeaderTimeout to be set on transport, got 0")
80+ }
81+}
82+
83+// TestSlowStreamNotKilledByClientTimeout verifies that a streaming response
84+// whose events are separated by pauses longer than the old 60-second default
85+// (simulated here with a short delay to keep the test fast) is fully received
86+// without error.
87+func TestSlowStreamNotKilledByClientTimeout(t *testing.T) {
88+ const delay = 200 * time.Millisecond
89+
90+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
91+ flusher, ok := w.(http.Flusher)
92+ if !ok {
93+ http.Error(w, "streaming unsupported", http.StatusInternalServerError)
94+ return
95+ }
96+ w.Header().Set("Content-Type", "text/event-stream")
97+ w.WriteHeader(http.StatusOK)
98+
99+ for i := 0; i < 3; i++ {
100+ _, _ = w.Write([]byte("data: {\"i\":" + string(rune('0'+i)) + "}\n\n"))
101+ flusher.Flush()
102+ time.Sleep(delay)
103+ }
104+ _, _ = w.Write([]byte("data: [DONE]\n\n"))
105+ flusher.Flush()
106+ }))
107+ defer server.Close()
108+
109+ // No Config.Timeout is set (defaults to 0 = unlimited). The test
110+ // confirms that the default client configuration does not kill a stream
111+ // body that is actively delivering events over an extended duration.
112+ client, err := NewClient(Config{
113+ APIKey: "key",
114+ BaseURL: server.URL,
115+ })
116+ if err != nil {
117+ t.Fatalf("NewClient: %v", err)
118+ }
119+
120+ stream, err := client.startStream(context.Background(), "POST", "/responses", []byte("{}"))
121+ if err != nil {
122+ t.Fatalf("startStream: %v", err)
123+ }
124+ defer func() { _ = stream.Close() }()
125+
126+ var events []StreamEvent
127+ for ev := range stream.Events {
128+ events = append(events, ev)
129+ }
130+
131+ if stream.Err != nil {
132+ t.Fatalf("stream terminated with error: %v", stream.Err)
133+ }
134+ if len(events) != 3 {
135+ t.Fatalf("expected 3 events, got %d", len(events))
136+ }
137+}