<div dir="ltr">In a HTTP-centric universe, FastCGI or load balancer software are great solutions, but ceding control to an opaque external system means any somewhat stateful network traffic that isn&#39;t HTTP is going to have to build an ad-hoc system for dealing with a process that can be brought down by a logic error. <div><br></div><div><span style="color:rgb(0,0,0);font-size:12.8px">In a world where you&#39;re dealing with continuous byte streams, for example some sort of video encoder or multiplexer (main point: dependent on a finite hardware resource) where you can&#39;t just scale the worker processes really high, tighter coupling between the server process and its recovery mechanism seems like it&#39;d be important. </span><div class="gmail-yj6qo gmail-ajU" style="color:rgb(0,0,0);font-size:12.8px"></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Nov 24, 2016 at 2:26 AM, Alex Blewitt <span dir="ltr">&lt;<a href="mailto:alblue@apple.com" target="_blank">alblue@apple.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><br><div><span class=""><blockquote type="cite"><div>On 23 Nov 2016, at 19:01, Andrew Akira Toulouse via swift-server-dev &lt;<a href="mailto:swift-server-dev@swift.org" target="_blank">swift-server-dev@swift.org</a>&gt; wrote:</div><br class="m_5793311982001053742Apple-interchange-newline"><div><div dir="ltr">Has there been any thoughts to strategies for keeping servers written in Swift running? People are certain to write bugs resulting in crashes from time to time, and I think that natively and gracefully handling process-death and relaunching workers would be something that probably gets standardized – if not by the provided APIs, then by whatever project springs up to fill that gap first.</div></div></blockquote><div><br></div></span><div>At the moment, a force unwrap of a nil value results in triggering a ud2 instruction which brings the process crashing down, taking all dispatch queues with it. This is a sensible mechanism for single-user applications and (for example) preserves data from corruption, but having a multi-user server it&#39;s not as desirable.</div><div><br></div><div>The problem is that when a crash occurs, even if you could disable it by pausing instead, would result in leaked memory. Any reference types (or protocol backed value types which have been promoted to the heap) will now never be cleaned up. Since there isn&#39;t any form of background garbage collection, that memory just stays there until the process terminates.</div><div><br></div><div>As such, the only way to deal with this at the moment is to have a front end dispatcher/load balancer that spawns multiple children, and each child handles one request. You can use built-in CGI systems – Kitura has a FastCGI implementation <a href="https://github.com/IBM-Swift/Kitura-net/tree/master/Sources/KituraNet/FastCGI" target="_blank">https://github.<wbr>com/IBM-Swift/Kitura-net/tree/<wbr>master/Sources/KituraNet/<wbr>FastCGI</a> for example – and have the web server spawn off single-use processes each time. One process per request is potentially not as performant as re-using processes for subsequent requests though.</div><div><br></div><div>Of course you can use launchd or equivalent to spawn up a number of worker processes and have them statically/dynamically registered with the load balancer/front end, and has the advantage that the processes can then be distributed across multiple hosts – micro services are often deployed in this way.</div><span class=""><br><blockquote type="cite"><div dir="ltr"><div>Spitballing: Is there perhaps an IPC abstraction that can work together with Swift and libdispatch? IIRC, XPC is dispatch-based, and I&#39;m particularly fond of its API, but I&#39;m not knowledgeable enough to evaluate it or how much of it can be made to work without the availability of Mach ports. I recall that some part of it uses launchd, but I&#39;m not certain what it&#39;s used for or the degree to which that integration is required.</div></div></blockquote></span></div><br><div>It is possible to write your own to do this, but if you&#39;re fielding HTTP traffic then it makes just as much sense to use existing HTTP load balancer technology to be able to perform this for you, and gives you an advantage that you can distribute over multiple hosts instead of a single host. There are some downsides – you end up parsing the HTTP request twice, for example (though in practice load balancers are typically sufficiently finely tuned that their parsing isn&#39;t a significant overhead in the processing of a request). Of course you can use something like gRPC <a href="https://github.com/grpc/grpc-swift" target="_blank">https://github.com/grpc/grpc-<wbr>swift</a> to perform a pre-parsed XPC request from a client to server.</div><span class="HOEnZb"><font color="#888888"><div><br></div><div>Alex</div></font></span></div></blockquote></div><br></div>