Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add kill timeout after 10 seconds #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ClangFormat.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
32ABEDDD187EFA4F00D4E980 /* TRVSPreferences.m in Sources */ = {isa = PBXBuildFile; fileRef = 32ABEDDB187EFA4F00D4E980 /* TRVSPreferences.m */; };
32D2236C187C28E1008D12A3 /* clang-format in Resources */ = {isa = PBXBuildFile; fileRef = 32D2236B187C28E1008D12A3 /* clang-format */; };
32F745E418823A57007541E1 /* NSDocument+TRVSClangFormat.m in Sources */ = {isa = PBXBuildFile; fileRef = 32F745E318823A57007541E1 /* NSDocument+TRVSClangFormat.m */; };
5DDD748D1925B38E0020CAC6 /* NSTask+TRVSClangFormat.m in Sources */ = {isa = PBXBuildFile; fileRef = 5DDD748C1925B38E0020CAC6 /* NSTask+TRVSClangFormat.m */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -49,6 +50,8 @@
32D2236B187C28E1008D12A3 /* clang-format */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.executable"; name = "clang-format"; path = "bin/clang-format"; sourceTree = SOURCE_ROOT; };
32F745E218823A57007541E1 /* NSDocument+TRVSClangFormat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDocument+TRVSClangFormat.h"; sourceTree = "<group>"; };
32F745E318823A57007541E1 /* NSDocument+TRVSClangFormat.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSDocument+TRVSClangFormat.m"; sourceTree = "<group>"; };
5DDD748B1925B38E0020CAC6 /* NSTask+TRVSClangFormat.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSTask+TRVSClangFormat.h"; sourceTree = "<group>"; };
5DDD748C1925B38E0020CAC6 /* NSTask+TRVSClangFormat.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSTask+TRVSClangFormat.m"; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -114,6 +117,8 @@
32ABEDC9187EF90500D4E980 /* TRVSXcode.m */,
32F745E218823A57007541E1 /* NSDocument+TRVSClangFormat.h */,
32F745E318823A57007541E1 /* NSDocument+TRVSClangFormat.m */,
5DDD748B1925B38E0020CAC6 /* NSTask+TRVSClangFormat.h */,
5DDD748C1925B38E0020CAC6 /* NSTask+TRVSClangFormat.m */,
32A57EEA187C05A4002DEC9D /* Supporting Files */,
);
path = ClangFormat;
Expand Down Expand Up @@ -208,6 +213,7 @@
32A57EF1187C05A4002DEC9D /* TRVSClangFormat.m in Sources */,
3228E445187F200D0037DECF /* TRVSCodeFragment.m in Sources */,
32ABEDDC187EFA4F00D4E980 /* TRVSFormatter.m in Sources */,
5DDD748D1925B38E0020CAC6 /* NSTask+TRVSClangFormat.m in Sources */,
32F745E418823A57007541E1 /* NSDocument+TRVSClangFormat.m in Sources */,
32ABEDCA187EF90500D4E980 /* TRVSXcode.m in Sources */,
32ABEDDD187EFA4F00D4E980 /* TRVSPreferences.m in Sources */,
Expand Down
15 changes: 15 additions & 0 deletions ClangFormat/NSTask+TRVSClangFormat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// NSTask+TRVSClangFormat.h
// ClangFormat
//
// Created by Seth Delackner on 5/16/14.
// Copyright (c) 2014 Travis Jeffery. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface NSTask (TRVSClangFormat)

- (void) killIfNotDoneBy:(NSDate *) killDate;

@end
23 changes: 23 additions & 0 deletions ClangFormat/NSTask+TRVSClangFormat.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// NSTask+TRVSClangFormat.m
// ClangFormat
//
// Created by Seth Delackner on 5/16/14.
// Copyright (c) 2014 Travis Jeffery. All rights reserved.
//

#import "NSTask+TRVSClangFormat.h"

@implementation NSTask (TRVSClangFormat)

- (void) killIfNotDoneBy:(NSDate *) killDate {
while ([self isRunning]) {
if ([[NSDate date] laterDate:killDate] != killDate) {
NSLog(@"Error: task took too long. killing.");
[self terminate];
}
[NSThread sleepForTimeInterval:1.0];
}
}

@end
4 changes: 3 additions & 1 deletion ClangFormat/TRVSCodeFragment.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "TRVSCodeFragment.h"
#import "NSTask+TRVSClangFormat.h"

@interface TRVSCodeFragment ()

Expand Down Expand Up @@ -65,8 +66,9 @@ - (void)formatWithStyle:(NSString *)style

[outputPipe.fileHandleForReading readToEndOfFileInBackgroundAndNotify];

NSDate *killDate = [NSDate dateWithTimeIntervalSinceNow:10.0];
[task launch];
[task waitUntilExit];
[task killIfNotDoneBy:killDate];

NSData *errorData = [errorPipe.fileHandleForReading readDataToEndOfFile];

Expand Down
5 changes: 4 additions & 1 deletion ClangFormat/TRVSFormatter.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "TRVSXcode.h"
#import "TRVSCodeFragment.h"
#import "NSDocument+TRVSClangFormat.h"
#import "NSTask+TRVSClangFormat.h"

@interface TRVSFormatter ()

Expand Down Expand Up @@ -221,8 +222,10 @@ - (void)fragmentsOfContinuousLineRanges:(NSArray *)continuousLineRanges
task.launchPath = shellString;
task.arguments = @[ @"-l", @"-c", @"which clang-format" ];

NSDate *killDate = [NSDate dateWithTimeIntervalSinceNow:10.0];
[task launch];
[task waitUntilExit];
[task killIfNotDoneBy: killDate];

[errorPipe.fileHandleForReading readDataToEndOfFile];
NSData *outputData = [outputPipe.fileHandleForReading readDataToEndOfFile];
NSString *outputPath = [[NSString alloc] initWithData:outputData
Expand Down