swift-server / swift-aws-lambda-runtime

Swift implementation of AWS Lambda Runtime
Apache License 2.0
1.13k stars 102 forks source link

Local Server Immediate Exit code: 0 (Xcode 12 b3) #148

Closed IrvTheSwirv closed 4 years ago

IrvTheSwirv commented 4 years ago

Wondering if this is an Xcode 12 beta 3 issue:

Having had this issue with my own first attempt at a simple swift lambda I tried building and running the MyLambda example project here and had the same result. If I run the project without the LOCAL_LAMBDA_SERVER_ENABLED flag I get the expected:

2020-07-29T12:22:40+0100 info Lambda : lambda lifecycle starting with Configuration General(logLevel: info)) Lifecycle(id: 148308112161193, maxTimes: 0, stopSignal: TERM) RuntimeEngine(ip: 127.0.0.1, port: 7000, keepAlive: true, requestTimeout: nil 2020-07-29T12:22:40+0100 error Lambda : lifecycleIteration=0 could not fetch work from lambda runtime engine: badStatusCode(NIOHTTP1.HTTPResponseStatus.unprocessableEntity) 2020-07-29T12:22:40+0100 error Lambda : lifecycleIteration=0 lambda invocation sequence completed with error: badStatusCode(NIOHTTP1.HTTPResponseStatus.unprocessableEntity) 2020-07-29T12:22:40+0100 info Lambda : shutdown completed Program ended with exit code: 0

However, if I add the LOCAL_LAMBDA_SERVER_ENABLED flag and run, the process starts and finishes immediately and the only output is the following:

Program ended with exit code: 0

Some pointers on where to look to resolve this would be great.

IrvTheSwirv commented 4 years ago

I've managed to step through the LocalServer code and found that the error is occurring in EvenLoopFuture in the wait() function (Line 928). The error is:

(Result<(),Error>?) $R0 = failure { failure = { failureDescription = "bind(descriptor:ptr:bytes:)" error = errno (errno = 48) }

fabianfett commented 4 years ago

@IrvTheSwirv I hope this helps: https://fabianfett.de/getting-started-with-swift-aws-lambda-runtime#step-4-test-your-lambda-locally

IrvTheSwirv commented 4 years ago

@IrvTheSwirv I hope this helps: https://fabianfett.de/getting-started-with-swift-aws-lambda-runtime#step-4-test-your-lambda-locally

Hi. @fabianfett Thanks but the problem happens when I add that flag. I managed to trace it to an error occurring inside the code that sets up the local server (see above) but I'm a little stuck beyond that.

fabianfett commented 4 years ago

@IrvTheSwirv Interesting... IMHO the most interesting part so far is that you get an HTTPResponseStatus.unprocessableEntity status code from . I just had Xcode12b2 installed. Grabbing b3 right now to reproduce.

The error you're getting is originating here:

case (.GET, let url) where url.hasSuffix(Consts.getNextInvocationURLSuffix):
    // check if our server is in the correct state
    guard case .waitingForLambdaRequest = Self.invocationState else {
        self.logger.error("invalid invocation state \(Self.invocationState)")
        self.writeResponse(context: context, response: .init(status: .unprocessableEntity))
        return
    }

Do you have by accident two Lambda running in parallel?

tomerd commented 4 years ago

Do you have by accident two Lambda running in parallel?

or maybe the default port (7000) is already taken by another program?

IrvTheSwirv commented 4 years ago

@tomerd @fabianfett fresh eyes looking at it again this morning and you are correct Tomer, it was a process on another project I'm working on periodically listening on ports in a range around 7000 and happened to be on those ports (even after reboots) which now explains the

failureDescription = "bind(descriptor:ptr:bytes:)"

I was seeing when stepping through the code.

Would it be fair to say the fact that the library terminates with no message and exit code 0 if it can't bind to port 7000 should be considered a bug? That cost me a day and a half of time when a message would have helped me resolve the issue immediately.

tomerd commented 4 years ago

thanks @IrvTheSwirv yes lets track this is a bug to handle this kind of error better