Skip to content

Shutdown must ALWAYS be called for shutdownIfNotStarted=true #106

@fabianfett

Description

@fabianfett

To use Lifecycle in lambda runtime, we need to find a way that it can shutdown instances that don't need a startup. Example handler:

import AWSLambdaRuntime
import AsyncHTTPClient

// introductory example, the obligatory "hello, world!"
@main
struct HelloWorldHandler: AsyncLambdaHandler {
    typealias In = String
    typealias Out = String

    let httpClient: HTTPClient

    init(context: Lambda.InitializationContext) async throws {
        self.httpClient = HTTPClient(eventLoopGroupProvider: .shared(context.eventLoop))
        context.lifecycle.register(label: "shutdown", start: .none, shutdown: .async {
            self.httpClient.shutdown($0)
        }, shutdownIfNotStarted: true)

        throw MyError()
    }

    func handle(event: String, context: Lambda.Context) async throws -> String {
        "hello, world"
    }
}

In this case the httpClient still needs a call to shutdown. This is currently not happening. The use case that we should enable is abstracted below:

Failing test case

func testShutdownIfNotStarted() {
    var shutdownCalled = false
    
    let lifecycle = ComponentLifecycle(label: "test")
    lifecycle.register(label: "shutdown", start: .none, shutdown: .sync {
        shutdownCalled = true
    }, shutdownIfNotStarted: true)
    
    lifecycle.shutdown()
    XCTAssertTrue(shutdownCalled)
}

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