From 8f9e068bedaa29c9a62e168b071350d45a62ede6 Mon Sep 17 00:00:00 2001 From: Oliver Date: Tue, 5 Jun 2018 18:49:28 +1000 Subject: [PATCH] Fix bug where server may fail to start There is a race condition where the state may not be set before the async task checks its value. This causes the server to shutdown with no warning. This change will fix this bug. --- Sources/HttpServerIO.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/HttpServerIO.swift b/Sources/HttpServerIO.swift index ee312add..215da739 100644 --- a/Sources/HttpServerIO.swift +++ b/Sources/HttpServerIO.swift @@ -75,6 +75,7 @@ public class HttpServerIO { self.state = .starting let address = forceIPv4 ? listenAddressIPv4 : listenAddressIPv6 self.socket = try Socket.tcpSocketForListen(port, forceIPv4, SOMAXCONN, address) + self.state = .running DispatchQueue.global(qos: priority).async { [weak self] in guard let strongSelf = self else { return } guard strongSelf.operating else { return } @@ -93,7 +94,6 @@ public class HttpServerIO { } strongSelf.stop() } - self.state = .running } public func stop() {