valyala / fasthttp

Fast HTTP package for Go. Tuned for high performance. Zero memory allocations in hot paths. Up to 10x faster than net/http
MIT License
21.91k stars 1.76k forks source link

Context is canceled on a newly initialized fasthttp.RequestCtx in 1.56.0 #1879

Closed oschwald closed 3 weeks ago

oschwald commented 1 month ago

Given the following code, a "context canceled" error is printed out in 1.56.0 but not 1.55.0.

package main

import (
    "fmt"

    "github.com/valyala/fasthttp"
)

func main() {
    var r fasthttp.Request

    r.Header.SetMethod("GET")
    r.SetRequestURI("http://example/")

    var requestCtx fasthttp.RequestCtx
    requestCtx.Init(&r, nil, nil)
    fmt.Println(requestCtx.Err())
}

I was able to bisect this to a7d488a9.

We use code similar to this in our tests to create a fasthttp.RequestCtx to pass to the handler. This code has been working unchanged for many years.

dza89 commented 1 month ago

I am seeing the same issue. Reverting to 1.55 solves the issue.

done <- struct{}{}

This line signals the task is done, resulting in a cancelled context

Fix:

done := ctx.s.done

    if done == nil {
        done = make(chan struct{})
    }
    return done

unfamiliar with the original issue though.

@erikdubbelboer @byte0o

byte0o commented 1 month ago

我遇到了同样的问题。恢复到1.55即可解决问题。

done <- struct{}{}

此行表示任务已完成,导致上下文被取消

使固定:

done := ctx.s.done

  if done == nil {
      done = make(chan struct{})
  }
  return done

尽管不熟悉原始问题。

@erikdubbelboer @byte0o

I am seeing the same issue. Reverting to 1.55 solves the issue.

done <- struct{}{}

This line signals the task is done, resulting in a cancelled context

Fix:

done := ctx.s.done

  if done == nil {
      done = make(chan struct{})
  }
  return done

unfamiliar with the original issue though.

@erikdubbelboer @byte0o

@dza89 This change is to address the https://github.com/valyala/fasthttp/pull/1662 issue,Now the semantics of the Err function need to be reconsidered.

erikdubbelboer commented 3 weeks ago

Should be fixed in https://github.com/valyala/fasthttp/pull/1890, I'll publish a release tomorrow.

oschwald commented 3 weeks ago

Thank you!

dza89 commented 3 weeks ago

Graat work, thanks a lot!