awslabs / aws-lambda-go-api-proxy

lambda-go-api-proxy makes it easy to port APIs written with Go frameworks such as Gin (https://gin-gonic.github.io/gin/ ) to AWS Lambda and Amazon API Gateway.
Apache License 2.0
1.09k stars 200 forks source link

Fiber proxy sets context to cancelled #206

Open 0ndermijner opened 1 month ago

0ndermijner commented 1 month ago

When using the ProxyWithContext function from the Fiber proxy, the context is immediately set to cancelled once it transitions from the Lambda context in the handler function to the Fiber fiber.Ctx struct. This results in subsequent API calls to AWS services that rely on the context to fail, as the context is already cancelled.

Runtime and configuration

Reproducing the issue

Below a very simple fiber app that will fatal out with a error state.

Go code

package main

import (
    "context"
    "log"

    "github.com/aws/aws-lambda-go/events"
    "github.com/aws/aws-lambda-go/lambda"
    fiberAdapter "github.com/awslabs/aws-lambda-go-api-proxy/fiber"
    "github.com/gofiber/fiber/v2"
)

var fiberLambda *fiberAdapter.FiberLambda

func init() {
    app := fiber.New()

    app.Use(func(c *fiber.Ctx) error {
        if c.Context().Err() != nil {
            log.Fatalf("Fiber context is in error state: %v", c.Context().Err())
        }
        return c.SendString("Hello")
    })

    fiberLambda = fiberAdapter.New(app)
}

func handler(ctx context.Context, req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
    if ctx.Err() != nil {
        log.Fatalf("Lambda context is in error state: %v", ctx.Err())
    }
    return fiberLambda.ProxyWithContext(ctx, req)
}

func main() {
    lambda.Start(handler)
}

Go mod file

module reproduce-fiber-erorr

go 1.23.1

require (
    github.com/aws/aws-lambda-go v1.47.0
    github.com/awslabs/aws-lambda-go-api-proxy v0.16.2
    github.com/gofiber/fiber/v2 v2.52.5
)

require (
    github.com/andybalholm/brotli v1.1.0 // indirect
    github.com/google/uuid v1.6.0 // indirect
    github.com/klauspost/compress v1.17.10 // indirect
    github.com/mattn/go-colorable v0.1.13 // indirect
    github.com/mattn/go-isatty v0.0.20 // indirect
    github.com/mattn/go-runewidth v0.0.16 // indirect
    github.com/rivo/uniseg v0.4.7 // indirect
    github.com/valyala/bytebufferpool v1.0.0 // indirect
    github.com/valyala/fasthttp v1.56.0 // indirect
    github.com/valyala/tcplisten v1.0.0 // indirect
    golang.org/x/sys v0.25.0 // indirect
)

Logging output:

START RequestId: 5abeae99-e0c9-4edd-acde-d6773e636304 Version: $LATEST
2024/10/01 09:21:49 Fiber context is in error state: context canceled
RequestId: 5abeae99-e0c9-4edd-acde-d6773e636304 Error: Runtime exited with error: exit status 1 Runtime.ExitError
END RequestId: 5abeae99-e0c9-4edd-acde-d6773e636304
REPORT RequestId: 5abeae99-e0c9-4edd-acde-d6773e636304 Duration: 31.88 ms Billed Duration: 109 ms Memory Size: 128 MB Max Memory Used: 20 MB Init Duration: 76.84 ms