swift-server/swift-service-lifecycle

Shutdown must ALWAYS be called for shutdownIfNotStarted=true

fabianfett opened this issue · 0 comments

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)
}