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:
parent
409c78dd14
commit
40bd176799
|
@ -227,9 +227,26 @@ public actor Server {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Blocking function that starts the HTTP server.
|
/// Blocking function that starts the HTTP server.
|
||||||
public func run() throws {
|
public nonisolated func run() async throws {
|
||||||
defer { app.shutdown() }
|
// Explicitly hop to another thread to avoid blocking the thread that is running the actor executor
|
||||||
try app.run()
|
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 {
|
for conn in connections {
|
||||||
try conn.close().wait()
|
try conn.close().wait()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue