Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.
This repository was archived by the owner on May 21, 2025. It is now read-only.

Fiber proxy sets context to cancelled #206

Open
@0ndermijner

Description

@0ndermijner

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

  • API Gateway: Regional rest API
  • Lambda runtime: Amazon Linux 2023; arn:aws:lambda:eu-west-1::runtime:394919924951bb234aa650732bf2a1cc74b5e6aa1d263b5f333fbd029cc8e978
  • Go version: 1.23.1
  • Fiber version: github.com/gofiber/fiber/v2 v2.52.5
  • Lambda proxy version: github.com/awslabs/aws-lambda-go-api-proxy v0.16.2
  • Lambda go version: github.com/aws/aws-lambda-go v1.47.0

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 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions