Channel response appends JSON null after context timeout

Aperta Adatta ai principianti
#71 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
go
Ambito
api

Direzione di ricerca

Il bug è in responder.go: channelIntoSlice scrive un 504 con http.Error quando il contesto viene annullato e restituisce nil, mentre DefaultResponder continua comunque a chiamare JSON(w, r, nil). Riproduci il problema con lo snippet nella issue, poi controlla il ramo channel di DefaultResponder per capire come dovrebbe fermarsi dopo il timeout. È completato quando il body è esattamente Server Timeout\n e i test esistenti del responder continuano a passare.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

When DefaultResponder receives a channel value and the request context is canceled while it waits, it writes a 504 timeout response and appends JSON null to the body. The result is a mixed text/JSON response rather than the timeout body alone.

Reproduced on latest release v1.0.3 and current main commit 14f1cb3d5c2969d6e462632a205eacb6421eb4dc with Go 1.27.1:

package main

import (
    "context"
    "fmt"
    "net/http/httptest"

    "github.com/go-chi/render"
)

func main() {
    ctx, cancel := context.WithCancel(context.Background())
    cancel()
    request := httptest.NewRequest("GET", "/", nil).WithContext(ctx)
    request.Header.Set("Accept", "application/json")
    response := httptest.NewRecorder()
    channel := make(chan string) // open, with no available value
    render.DefaultResponder(response, request, channel)
    fmt.Printf("status=%d content-type=%q body=%q\n",
        response.Result().StatusCode,
        response.Result().Header.Get("Content-Type"),
        response.Body.String())
}

Observed:

status=504 content-type="text/plain; charset=utf-8" body="Server Timeout\nnull\n"

Expected: after sending the timeout response, no second payload should be written. The body should be "Server Timeout\n".

In responder.go, channelIntoSlice calls http.Error(..., 504) and returns nil on cancellation. DefaultResponder then continues to JSON(w, r, nil), which writes the extra null.

Lingua principale
Go
Stelle
341
Fork
68
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di go-chi/render

Tutte le issue di go-chi/render

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.