Skip to content

Commit

Permalink
helper: send SIGKILL if process is unresponsive
Browse files Browse the repository at this point in the history
QEMU can refuse to react to SIGTERM due to a deadlock bug or for any other
reason. If it does not gracefully quit in 1 second, then we send SIGKILL.

Fixes #5769
  • Loading branch information
osy committed Oct 9, 2023
1 parent 2ea42d8 commit d915953
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion QEMUHelper/QEMUHelper.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#import "QEMUHelper.h"
#import "QEMUHelperDelegate.h"
#import <stdio.h>
#import <signal.h>

static const int64_t kProcessTerminateTimeoutSeconds = 1;

@interface QEMUHelper ()

Expand Down Expand Up @@ -150,8 +153,21 @@ - (void)startQemuTask:(NSString *)binName standardOutput:(NSFileHandle *)standar
}

- (void)terminate {
[self.childTask terminate];
NSTask *childTask = self.childTask;
self.childTask = nil;
if (childTask) {
void (^terminationHandler)(NSTask *) = childTask.terminationHandler;
dispatch_semaphore_t terminatedEvent = dispatch_semaphore_create(0);
childTask.terminationHandler = ^(NSTask *task) {
terminationHandler(task);
dispatch_semaphore_signal(terminatedEvent);
};
[childTask terminate];
if (childTask.isRunning && dispatch_semaphore_wait(terminatedEvent, dispatch_time(DISPATCH_TIME_NOW, kProcessTerminateTimeoutSeconds*NSEC_PER_SEC)) != 0) {
NSLog(@"Process %d failed to respond to SIGTERM, sending SIGKILL...", childTask.processIdentifier);
kill(childTask.processIdentifier, SIGKILL);
}
}
[self invalidateToken];
}

Expand Down

0 comments on commit d915953

Please sign in to comment.