fix context canceled error while propagate context from http.Request
@@ -9,6 +9,7 @@ package ghttp
import (
"context"
"github.com/gogf/gf/v2/container/gvar"
+ "github.com/gogf/gf/v2/os/gctx"
)
// RequestFromCtx retrieves and returns the Request object from context.
@@ -24,7 +25,10 @@ func RequestFromCtx(ctx context.Context) *Request {
// See GetCtx.
func (r *Request) Context() context.Context {
if r.context == nil {
- r.context = r.Request.Context()
+ // DO NOT use the http context as it will be canceled after request done,
+ // which makes the asynchronous goroutine encounter "context canceled" error.
+ // r.context = r.Request.Context()
+ r.context = gctx.New()
}
// Inject Request object into context.
if RequestFromCtx(r.context) == nil {
