Skip to content
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

[SVLS-4690] Handling panic case #166

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions internal/wrapper/wrap_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"encoding/json"
"errors"
"fmt"

"github.com/DataDog/datadog-lambda-go/internal/extension"
"github.com/DataDog/datadog-lambda-go/internal/logger"
"github.com/aws/aws-lambda-go/lambda"
Expand Down Expand Up @@ -133,10 +132,17 @@ func validateHandler(handler interface{}) error {
return nil
}

func callHandler(ctx context.Context, msg json.RawMessage, handler interface{}) (interface{}, error) {
ev, err := unmarshalEventForHandler(msg, handler)
if err != nil {
return nil, err
func callHandler(ctx context.Context, msg json.RawMessage, handler interface{}) (response interface{}, errResponse error) {
defer func() {
if r := recover(); r != nil {
response = nil
errResponse = fmt.Errorf("Panic: %v", r)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned that since we do not re-panic, we're actually changing how a panicking function functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh, I have the very same concern.
I think the alternative would be to re-panic after we send the trace.

defer func() {
        if r := recover(); r != nil {
            // send the trace somehow 
            panic(r)
        }
    }()

}()

ev, errResponse := unmarshalEventForHandler(msg, handler)
if errResponse != nil {
return nil, errResponse
}
handlerType := reflect.TypeOf(handler)

Expand All @@ -160,9 +166,6 @@ func callHandler(ctx context.Context, msg json.RawMessage, handler interface{})
handlerValue := reflect.ValueOf(handler)
output := handlerValue.Call(args)

var response interface{}
var errResponse error

if len(output) > 0 {
// If there are any output values, the last should always be an error
val := output[len(output)-1].Interface()
Expand Down
16 changes: 16 additions & 0 deletions internal/wrapper/wrap_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,22 @@ func TestWrapHandlerReturnsErrorOnly(t *testing.T) {
assert.Equal(t, nil, response)
}

func TestWrapHandlerReturnsErrorWhenPanic(t *testing.T) {
called := false
panicRecoverErr := errors.New("Panic: some panic")

handler := func(request mockNonProxyEvent) error {
called = true
panic("some panic")
}

_, response, err := runHandlerWithJSON(t, "../testdata/non-proxy-no-headers.json", handler)

assert.True(t, called)
assert.Equal(t, panicRecoverErr, err)
assert.Equal(t, nil, response)
}

func TestWrapHandlerReturnsOriginalHandlerIfInvalid(t *testing.T) {

var handler interface{} = func(arg1, arg2, arg3 int) (int, error) {
Expand Down
6 changes: 3 additions & 3 deletions tests/integration_tests/hello/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ toolchain go1.21.5
require (
github.com/DataDog/datadog-lambda-go v1.14.0
github.com/aws/aws-lambda-go v1.46.0
gopkg.in/DataDog/dd-trace-go.v1 v1.60.0
gopkg.in/DataDog/dd-trace-go.v1 v1.60.3
)

require (
github.com/DataDog/appsec-internal-go v1.4.0 // indirect
github.com/DataDog/appsec-internal-go v1.4.1 // indirect
github.com/DataDog/datadog-agent/pkg/obfuscate v0.50.2 // indirect
github.com/DataDog/datadog-agent/pkg/remoteconfig/state v0.50.2 // indirect
github.com/DataDog/datadog-go/v5 v5.5.0 // indirect
Expand Down Expand Up @@ -67,7 +67,7 @@ require (
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240125205218-1f4bbc51befe // indirect
google.golang.org/grpc v1.61.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
inet.af/netaddr v0.0.0-20230525184311-b8eac61e914a // indirect
)

Expand Down
3 changes: 3 additions & 0 deletions tests/integration_tests/hello/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ github.com/DataDog/appsec-internal-go v1.0.2 h1:Z+YWPlkQN+324zIk+BzKlPA1/6guKgGm
github.com/DataDog/appsec-internal-go v1.0.2/go.mod h1:+Y+4klVWKPOnZx6XESG7QHydOaUGEXyH2j/vSg9JiNM=
github.com/DataDog/appsec-internal-go v1.4.0 h1:KFI8ElxkJOgpw+cUm9TXK/jh5EZvRaWM07sXlxGg9Ck=
github.com/DataDog/appsec-internal-go v1.4.0/go.mod h1:ONW8aV6R7Thgb4g0bB9ZQCm+oRgyz5eWiW7XoQ19wIc=
github.com/DataDog/appsec-internal-go v1.4.1/go.mod h1:rmZ+tpq5ZPKmeOUMYjWFg+q1mRd13mxZwSLBG+xa1ik=
github.com/DataDog/datadog-agent/pkg/obfuscate v0.48.0 h1:bUMSNsw1iofWiju9yc1f+kBd33E3hMJtq9GuU602Iy8=
github.com/DataDog/datadog-agent/pkg/obfuscate v0.48.0/go.mod h1:HzySONXnAgSmIQfL6gOv9hWprKJkx8CicuXuUbmgWfo=
github.com/DataDog/datadog-agent/pkg/obfuscate v0.50.2 h1:y08IzbpFM/HBaKfgayFZe1FpcbZn6bVPXoZ++93vxv8=
Expand Down Expand Up @@ -316,10 +317,12 @@ google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cn
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/DataDog/dd-trace-go.v1 v1.58.0 h1:ixIUarsu0RrOt7xfdrE5YSFvjgaWsP3cC3G342jTIuw=
gopkg.in/DataDog/dd-trace-go.v1 v1.58.0/go.mod h1:SmnEjjV9ZQr4MWRSUYEpoPyNtmtRK5J6UuJdAma+Yxw=
gopkg.in/DataDog/dd-trace-go.v1 v1.60.0 h1:hjDiU6PWRgMoUeSJkXdtimUP76cFzREPIGIIQJD0mYU=
gopkg.in/DataDog/dd-trace-go.v1 v1.60.0/go.mod h1:6aArYrAHjnuaofJ3lKuSRQbhrBx1LcSpiEYCIScJE5Y=
gopkg.in/DataDog/dd-trace-go.v1 v1.60.3/go.mod h1:XF/Y0lFGnmgedNXnltCm6hXkt9iwyeVVVFbKhJvVwtY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down
Loading