Skip to content

Timeout behavior regression in v1.2.1 #86

@Takashicc

Description

@Takashicc

Summary

There appears to be a regression in github.com/gin-contrib/timeout starting from v1.2.1.
The timeout middleware does not terminate the request at the configured timeout duration and instead waits until the handler completes.

Environment

  • github.com/gin-contrib/timeout: v1.2.1
  • github.com/gin-gonic/gin: v1.12.0

Reproduction

  1. Use the following test code
package main

import (
	"log"
	"net/http"
	"time"

	"github.com/gin-contrib/timeout"
	"github.com/gin-gonic/gin"
)

func testResponse(c *gin.Context) {
	c.String(http.StatusRequestTimeout, "timeout")
}

func timeoutMiddleware() gin.HandlerFunc {
	return timeout.New(
		timeout.WithTimeout(time.Second),
		timeout.WithResponse(testResponse),
	)
}

func main() {
	r := gin.New()
	r.Use(timeoutMiddleware())
	r.GET("/slow", func(c *gin.Context) {
		time.Sleep(5 * time.Second)
		c.Status(http.StatusOK)
	})
	if err := r.Run(":8080"); err != nil {
		log.Fatal(err)
	}
}
  1. Run the server
$ go run main.go
  1. Call the endpoint
$ curl http://localhost:8080/slow -w "%{time_total} sec\n"

Results

Using gin-contrib/timeout v1.2.1:

5.001918 sec

Using gin-contrib/timeout v1.2.0:

1.001925 sec

Expected Behavior

When a timeout of 1 second is configured, the request should be terminated after ~1 second, and the timeout response should be returned.

Actual Behavior

  • With v1.2.1, the request completes after ~5 seconds (the handler duration), not respecting the timeout.
  • With v1.2.0, the timeout works correctly and returns after ~1 second.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions