mirror of
https://github.com/nicoverbruggen/phpmon.git
synced 2026-04-02 01:30:07 +02:00
🐛 Cancellation of timeout task w/ serial queue in RealShell
This commit is contained in:
@@ -7,6 +7,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
import Foundation
|
import Foundation
|
||||||
|
@preconcurrency import Dispatch
|
||||||
|
|
||||||
class RealShell: ShellProtocol {
|
class RealShell: ShellProtocol {
|
||||||
var container: Container
|
var container: Container
|
||||||
@@ -235,15 +236,16 @@ class RealShell: ShellProtocol {
|
|||||||
// Guard against resuming the continuation twice (race between timeout and termination)
|
// Guard against resuming the continuation twice (race between timeout and termination)
|
||||||
var resumed = false
|
var resumed = false
|
||||||
|
|
||||||
// We are using GCD here because we're already using a serial queue anyway
|
// Use GCD; we're already using a serial queue so legacy concurrency approach is okay
|
||||||
let timeoutTaskTermination = DispatchWorkItem {
|
let timeoutTaskTermination = DispatchWorkItem {
|
||||||
if process.isRunning {
|
guard process.isRunning else { return }
|
||||||
process.terminationHandler = nil
|
|
||||||
process.terminate()
|
process.terminationHandler = nil
|
||||||
if !resumed {
|
process.terminate()
|
||||||
resumed = true
|
|
||||||
continuation.resume(throwing: ShellError.timedOut)
|
if !resumed {
|
||||||
}
|
resumed = true
|
||||||
|
continuation.resume(throwing: ShellError.timedOut)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -273,7 +275,9 @@ class RealShell: ShellProtocol {
|
|||||||
}
|
}
|
||||||
|
|
||||||
process.terminationHandler = { process in
|
process.terminationHandler = { process in
|
||||||
timeoutTaskTermination.cancel()
|
serialQueue.async {
|
||||||
|
timeoutTaskTermination.cancel()
|
||||||
|
}
|
||||||
|
|
||||||
// Clean up readability handlers
|
// Clean up readability handlers
|
||||||
outputPipe.fileHandleForReading.readabilityHandler = nil
|
outputPipe.fileHandleForReading.readabilityHandler = nil
|
||||||
|
|||||||
Reference in New Issue
Block a user