swift-aws-lambda-runtime icon indicating copy to clipboard operation
swift-aws-lambda-runtime copied to clipboard

[core] user can create multiple instances of the Lambda Runtime

Open sebsto opened this issue 8 months ago • 1 comments

Now that user controls the main function with the runtime v2, nothing prevents users to create multiple runtimes.

I tested the code below on Lambda and it works. Any of the two runtimes can pickup the invocation and respond. In my exemple, they're booth taking the same input parameter and there is not too much consequences. But if the user codes the two handler with different input types, the Lambda function will crash when the "incorrect" runtime picks up the event.

We should enforce the runtime as a singleton.

demo code

import AWSLambdaRuntime

let runtime1 = LambdaRuntime {
    (event: String, context: LambdaContext) in
    "Hello1 \(event)"
}

let runtime2 = LambdaRuntime {
    (event: String, context: LambdaContext) in
    "Hello2 \(event)"
}

try await withThrowingTaskGroup(of: Void.self) { group in
    group.addTask {
        try await runtime1.run()
    }
    group.addTask {
        try await runtime2.run()
    }

    try await group.waitForAll()
}

sebsto avatar Mar 14 '25 06:03 sebsto

I proposed a fix in this PR https://github.com/swift-server/swift-aws-lambda-runtime/pull/508

sebsto avatar Mar 14 '25 12:03 sebsto

Fixed by https://github.com/swift-server/swift-aws-lambda-runtime/pull/508 Thank you @fabianfett and @adam-fowler for your feedback and patience.

sebsto avatar Jul 04 '25 13:07 sebsto