Repository: vapor/vapor
File: Sources/Vapor/Server/NIOServer.swift (Swift)

Code before:
            // start the actual HTTPServer
            return HTTPServer.start(
                hostname: hostname,
                port: port,
                responder: httpResponder,
                maxBodySize: config.maxBodySize,
                backlog: config.backlog,
                reuseAddress: config.reuseAddress,
                tcpNoDelay: config.tcpNoDelay,
                upgraders: upgraders,
                on: group
            ) { error in
                logger.report(error: error, verbose: !self.container.environment.isRelease)
            }.map { server in
                if let app = self.container as? Application {
                    app.runningServer = RunningServer(onClose: server.onClose, close: server.close)
                }
            }
        } catch {
            return container.eventLoop.newFailedFuture(error: error)
        }
    }

    /// Called when the server deinitializes.
    deinit {
        container.shutdownGracefully {
            if let error = $0 {
                ERROR("shutting down server event loop: \(error)")
            }
        }
    }
}

// MARK: Private

private struct NIOServerResponder: HTTPServerResponder {
    let containerCache: ThreadSpecificVariable<ThreadContainer>
    let responderCache: ThreadSpecificVariable<ThreadResponder>

    func respond(to http: HTTPRequest, on worker: Worker) -> EventLoopFuture<HTTPResponse> {
        guard let container = containerCache.currentValue?.container else {
            let error = VaporError(identifier: "serverContainer", reason: "Missing server container.")
            return worker.eventLoop.newFailedFuture(error: error)
        }
        let req = Request(http: http, using: container)
        guard let responder = responderCache.currentValue?.responder else {
            let error = VaporError(identifier: "serverResponder", reason: "Missing responder.")
            return worker.eventLoop.newFailedFuture(error: error)
        }
        do {
            // use #file to explicitly utilize NIO's non-throwing map

Review comment (suggestion):
This should still call `currentWorker?.shutdownGracefully`. We just need to actually set `currentWorker` haha

Code after:

            // http responder
            let httpResponder = NIOServerResponder(containerCache: containerCache, responderCache: responderCache)

            // start the actual HTTPServer
            return HTTPServer.start(
                hostname: hostname,
                port: port,
                responder: httpResponder,
                maxBodySize: config.maxBodySize,
                backlog: config.backlog,
                reuseAddress: config.reuseAddress,
                tcpNoDelay: config.tcpNoDelay,
                upgraders: upgraders,
                on: group
            ) { error in
                logger.report(error: error, verbose: !self.container.environment.isRelease)
            }.map { server in
                if let app = self.container as? Application {
                    app.runningServer = RunningServer(onClose: server.onClose, close: server.close)
                }
            }
        } catch {
            return container.eventLoop.newFailedFuture(error: error)
        }
    }

    /// Called when the server deinitializes.
    deinit {
        currentWorker?.shutdownGracefully {
            if let error = $0 {
                ERROR("shutting down server event loop: \(error)")
            }
        }
    }
}

// MARK: Private

private struct NIOServerResponder: HTTPServerResponder {
    let containerCache: ThreadSpecificVariable<ThreadContainer>
    let responderCache: ThreadSpecificVariable<ThreadResponder>

    func respond(to http: HTTPRequest, on worker: Worker) -> EventLoopFuture<HTTPResponse> {
        guard let container = containerCache.currentValue?.container else {
            let error = VaporError(identifier: "serverContainer", reason: "Missing server container.")
            return worker.eventLoop.newFailedFuture(error: error)
        }
        let req = Request(http: http, using: container)
        guard let responder = responderCache.currentValue?.responder else {
            let error = VaporError(identifier: "serverResponder", reason: "Missing responder.")