Prevent orphaned dev processes on shutdown - #2
Conversation
|
I'm testing this locally a little more before I submit for review. |
|
@kevinrenskers I have been using this change for a few days and haven't found any issues. hope you will review and merge. |
|
Thanks for the PR! I only saw it now, for some reason GitHub didn't email me when you created it. I'll review it this weekend. |
|
Thanks for this. The diagnosis is correct and the bug is real. The serialized queue is the right fix. However, Ctrl-C now pauses for the length of the rebuild, with no feedback.
For a typical incremental rebuild that's a couple of seconds, so it's a papercut rather than a blocker. What makes it read as a hang rather than a delay is that it's unmaskable: the parent has At the very least we should move the shutdown message to something that isn't sitting behind the build. Even better would be for shutdown to not wait at all. Something like this? // stored properties on DevCoordinator (lifecycleQueue is currently a local in start()):
private let lifecycleQueue = DispatchQueue(label: "Saga.Lifecycle")
private let shuttingDown = OSAllocatedUnfairLock(initialState: false)
// SIGINT source back on its own queue:
sigintSrc.setEventHandler { [weak self] in
print("\nShutting down...")
self?.shuttingDown.withLock { $0 = true }
self?.lifecycleQueue.sync {} // barrier: let any in-flight relaunch finish
// ...terminate, stop, exit
}
// in recompileAndRelaunch, after swiftBuild() succeeds:
if shuttingDown.withLock({ $0 }) { return } |
|
I have a slightly different implementation, which I'd love for you to have a look at: #3. Your commit is included in that PR, so you'll end up with the credit you're due. |
What changed
Why
The SIGUSR1 and SIGINT handlers previously ran on different queues while both accessed
siteProcess. Ctrl-C during a source recompile could therefore terminate the old process and exit while the recompile handler installed a replacement, leaving the replacement orphaned and still watching the site.Serializing those two lifecycle operations makes their ordering explicit. If shutdown arrives during a recompile, it runs immediately afterward and terminates the replacement before the CLI exits.
Validation
swift buildsaga devagainst a fixture that deliberately delayed a source recompile, then sent Ctrl-C during that delay.