Channel response appends JSON null after context timeout

Đang mở Phù hợp với người mới
#71 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

@awais-ur-rehman đang làm issue này rồi.

Từ ngày 4/11/2026.

  • #73 của @awais-ur-rehman — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
72/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
go
Lĩnh vực
api

Hướng nghiên cứu

Lỗi nằm trong responder.go: channelIntoSlice ghi phản hồi 504 bằng http.Error khi context bị hủy và trả về nil, nhưng DefaultResponder vẫn gọi JSON(w, r, nil). Hãy tái hiện lỗi bằng đoạn mã trong issue, sau đó kiểm tra nhánh channel của DefaultResponder để xem nó nên dừng lại thế nào sau khi hết thời gian chờ. Hoàn thành khi body chính xác là Server Timeout\n và các test responder hiện có vẫn pass.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Go
Star
340
Fork
69
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của go-chi/render

Tất cả issue của go-chi/render

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.