Skip to content

write_idle (v2.11.6) causes h2 streaming responses that are idle for the specified duration to fail #8118

Closed

Description

@WeidiDeng

Issue Details

The document for write_idle specifies

responses that pause between writes (like server-sent events), are not affected as long as each write makes progress.

However, it's only true for h1 and h3 requests, since both set the write deadline on the underlying connection and stream respectively.

https://github.com/golang/go/blob/6f5c275ebdc454197fff5f1496521c8f81e20eef/src/net/http/server.go#L511-L513

https://github.com/quic-go/quic-go/blob/6a7d632feee14c83d36fb5039c1b4f98d9335233/http3/response_writer.go#L356-L358

For h2, this will cause onWriteTimeout to fire

https://github.com/golang/net/blob/28247830e9fb37184c6be718f483a0c7d0949022/http2/server.go#L2724-L2748

which will cause h2 internal stream errors

https://github.com/golang/net/blob/28247830e9fb37184c6be718f483a0c7d0949022/http2/server.go#L1856-L1862

It can be reproduced with the following steps, (idle timeout reduced to 2s)

Caddyfile

{
        local_certs
        skip_install_trust
        servers :443 {
                timeouts {
                        write_idle 2s
                }
        }
}

https://127.0.0.1 {
        reverse_proxy 127.0.0.1:81
}

Backend

package main

import (
        "fmt"
        "net/http"
        "time"
)

func sseHandler(w http.ResponseWriter, r *http.Request) {
        // 1. Set SSE response headers
        w.Header().Set("Content-Type", "text/event-stream")
        w.Header().Set("Cache-Control", "no-cache")
        w.Header().Set("Connection", "keep-alive")
        w.Header().Set("Access-Control-Allow-Origin", "*")

        // 2. Initialize ResponseController for explicit flushing
        rc := http.NewResponseController(w)

        // 3. Stream data periodically until client disconnects
        ctx := r.Context()
        ticker := time.NewTicker(7 * time.Second)
        defer ticker.Stop()

        for {
                select {
                case <-ctx.Done():
                        fmt.Println("Client disconnected")
                        return
                case t := <-ticker.C:
                        fmt.Fprintf(w, "data: Server time is %s\n\n", t.Format(time.RFC3339))

                        if err := rc.Flush(); err != nil {
                                fmt.Println("Error flushing data:", err)
                                return
                        }
                }
        }
}

func main() {
        http.HandleFunc("/events", sseHandler)

        fmt.Println("SSE Server running on http://localhost:81/events")

        // Bind specifically to localhost on port 81
        if err := http.ListenAndServe("localhost:81", nil); err != nil {
                fmt.Println("Error starting server:", err)
        }
}

With curl -N https://127.0.0.1/events -k, we can observe one line of output before the h2 internal error.

data: Server time is 2026-10-03T03:22:26Z

curl: (92) HTTP/2 stream 1 was not closed cleanly: INTERNAL_ERROR (err 2)

Assistance Disclosure

No AI was used.

If AI was used, describe the extent to which it was used.

No response

Activity

  1. changed the title [-]Default write_idle (v2.11.6) causes h2 streaming responses that are idle for 60s to fail[/-] [+]write_idle (v2.11.6) causes h2 streaming responses that are idle for the specified duration to fail[/+] on Oct 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions