Fix hunged watcher (#307)

Actor isolated methods must not block the current thread, so Vapor
server's start method is not compatible in Concurrency context.
This commit is contained in:
Yuta Saito 2022-04-02 20:25:25 +09:00 committed by GitHub
parent 409c78dd14
commit 40bd176799
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 3 deletions

View File

@ -227,9 +227,26 @@ public actor Server {
}
/// Blocking function that starts the HTTP server.
public func run() throws {
defer { app.shutdown() }
try app.run()
public nonisolated func run() async throws {
// Explicitly hop to another thread to avoid blocking the thread that is running the actor executor
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
Thread {
Task {
do {
defer { self.app.shutdown() }
try self.app.run()
try await self.closeSockets()
continuation.resume()
} catch {
continuation.resume(with: .failure(error))
}
}
}
.start()
}
}
func closeSockets() throws {
for conn in connections {
try conn.close().wait()
}