-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sam local invoke (go) not honoring timeout in context #2510
Comments
I've got the same issue too. OS: macOS 10.15.7 hello.go package main
import (
"context"
"fmt"
"time"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
)
type Response events.APIGatewayProxyResponse
func Handler(ctx context.Context) (Response, error) {
fmt.Printf("%+v\n", ctx)
deadline, _ := ctx.Deadline()
fmt.Printf("now: %+v\n", time.Now().Format(time.RFC3339Nano))
fmt.Printf("deadline: %+v\n", deadline.Format(time.RFC3339Nano))
select {
case <-ctx.Done():
fmt.Printf("ctx.Err(): %+v\n", ctx.Err())
default:
}
return Response{
StatusCode: 200,
IsBase64Encoded: false,
Headers: map[string]string{
"Content-Type": "application/json",
},
}, nil
}
func main() {
lambda.Start(Handler)
} I found that Here's the result of the code above. It shows the context deadline had been expired then it outputs
I looked up the cause. It seems that the newly updated AWS Lambda runtime API has become different as before. I didn't set the option below. (ref: https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-cli-command-reference-sam-local-start-api.html )
I found the value of (ref: https://docs.aws.amazon.com/lambda/latest/dg/runtimes-api.html ) |
Could this be related to PR #2383 that resolved issue #239? I'm seeing the same $ sam --version
SAM CLI, version 1.15.0
$ cat /etc/issue
Ubuntu 20.04.1 LTS \n \l |
To further the investigation, I ran with both sam local start-api --warm-containers EAGER
sam local start-api --warm-containers LAZY I can't figure out if there's a way to disable the warm containers to see if it starts working without it. @moelasmar is there a way to invoke with your "warm container" improvement disabled? |
If you do not set warm containers, they aren't used. This is likely related to #2519 |
Cleaning up duplicates; this was narrowed down in #2519 as being introduced in v1.13.0. |
I'm experiencing this issue as well while it was working earlier on when I had v1.15. I've tried upgrading to v1.18.1 but that didn't help. |
I've just spent quite a bit of time trying to debug this since locally none of my mysql queries that use PrepareContext() were working. After some searching came across this bug. Sam local 1.18.2, Ubuntu 20.04 with latest docker repo. |
I rolled back to |
It appears that SAM CLI, version 1.18.0 will execute properly |
SAM CLI version 1.18.2 appears to be affected by this issue. |
For anyone wanting to know how to downgrade, you can use Pip3:
This version seems to be working for me. |
Any updates on this please? It's an issue for us and downgrading to 1.12.0 is unfortunate as the lazy stuff in newer SAM CLI local is really useful for us. |
I now have to switch between CLI versions because nodejs14.x is not supported until 1.17. This would be a great issue to get repaired. Here are the commands that I use to switch between versions in case it's useful to anyone (on Ubuntu). Dowgnrade |
Experienced this bug today in SAM v1.29.0 |
++, got same bug now |
This issue appears to be fixed in the latest version: Or at least my use case appears to be bug free. A non-expired Go code: package main
import (
"context"
"log"
"os"
"github.com/aws/aws-lambda-go/lambda"
)
type handler struct {
logger *log.Logger
}
// Invoke implements the lambda.Handler interface and shows context.Context information.
func (h handler) Invoke(ctx context.Context, _ []byte) (response []byte, err error) {
h.logger.Printf("%+v", ctx)
select {
case <-ctx.Done():
h.logger.Println("Context expired.")
err := ctx.Err()
h.logger.Printf("Error: %s", err.Error())
response = []byte(`{"statusCode":200,"headers":null,"multiValueHeaders":null,"body":"Failed due to expired context on invocation."}`)
default:
h.logger.Println("Context not expired.")
response = []byte(`{"statusCode":200,"headers":null,"multiValueHeaders":null,"body":"Successfully returned response."}`)
}
return response, nil
}
func main() {
logger := log.New(os.Stdout, "", log.LstdFlags)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
h := handler{
logger: logger,
}
lambda.StartHandlerWithContext(ctx, h)
}
AWSTemplateFormatVersion: "2010-09-09"
Description: "An example Golang AWS SAM Lambda project."
Transform: "AWS::Serverless-2016-10-31"
Parameters:
VaultToken:
Type: "String"
Globals:
Function:
Handler: "main"
Timeout: 10
Runtime: "go1.x"
Resources:
exampleOne:
Type: "AWS::Serverless::Function"
Properties:
CodeUri: "cmd/one"
Events:
ApiEvent:
Type: "HttpApi"
Api:
Type: "Api"
Properties:
Path: "/one"
Method: "get" Output: $ sam --version && date && sam build && sam local invoke
SAM CLI, version 1.37.0
Sat 15 Jan 2022 04:19:18 PM EST
Building codeuri: /home/micah/GolandProjects/go-aws-sam-lambda-example/cmd/one runtime: go1.x metadata: {} architecture: x86_64 functions: ['exampleOne']
Running GoModulesBuilder:Build
Build Succeeded
Built Artifacts : .aws-sam/build
Built Template : .aws-sam/build/template.yaml
Commands you can use next
=========================
[*] Invoke Function: sam local invoke
[*] Test Function in the Cloud: sam sync --stack-name {stack-name} --watch
[*] Deploy: sam deploy --guided
Invoking main (go1.x)
Skip pulling image and use local one: public.ecr.aws/sam/emulation-go1.x:rapid-1.37.0-x86_64.
Mounting /home/micah/GolandProjects/go-aws-sam-lambda-example/.aws-sam/build/exampleOne as /var/task:ro,delegated inside runtime container
START RequestId: ab0e8d22-adfb-4b9f-9d0e-bccea54000b9 Version: $LATEST
2022/01/15 21:19:22 context.Background.WithCancel.WithDeadline(2022-01-15 21:19:32.075598487 +0000 UTC [9.998813556s]).WithValue(type *lambdacontext.key, val <not Stringer>).WithValue(type string, val )
2022/01/15 21:19:22 Context not expired.
{"statusCode":200,"headers":null,"multiValueHeaders":null,"body":"Successfully returned response."}END RequestId: ab0e8d22-adfb-4b9f-9d0e-bccea54000b9
REPORT RequestId: ab0e8d22-adfb-4b9f-9d0e-bccea54000b9 Init Duration: 0.20 ms Duration: 7.44 ms Billed Duration: 8 ms Memory Size: 128 MB Max Memory Used: 128 MB Notice the This is the same behavior with |
This was already fixed in RIE and integrated with SAM CLI. Looks like we just forgot to close this out and therefore closing. |
Description:
When running a golang lambda function with
sam local invoke
, and making a request (in the function) using the context results inRequestCanceled: request context canceled caused by: context deadline exceeded
.When using ListBuckets (
without WithContext
) results in a successful result.Steps to reproduce:
hello.go
template.yml
Observed result:
RequestCanceled: request context canceled caused by: context deadline exceeded
Expected result:
Additional environment details (Ex: Windows, Mac, Amazon Linux etc)
sam --version
: SAM CLI, version 1.15.Add --debug flag to command you are running
The text was updated successfully, but these errors were encountered: