Skip to content

Commit

Permalink
Merge pull request #142 from linkedin/fix-app-hang-nl
Browse files Browse the repository at this point in the history
Better fix for app hanging by printing a bunch of new lines
  • Loading branch information
oliverhu authored May 12, 2017
2 parents 214d2dc + 04bfeff commit 281fa3e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
5 changes: 0 additions & 5 deletions BPSampleApp/BPSampleAppHangingTests/BPSampleAppHangingTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ - (void)tearDown {
[super tearDown];
}

- (void)testAppHangingWithOutput {
while(TRUE){printf("\n");}
}

- (void)testAppHanging {
while(TRUE){};
}


@end
24 changes: 19 additions & 5 deletions Bluepill-cli/Bluepill-cli/Bluepill/Reporters/BPTreeParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,29 @@ - (void)writeHeader {
- (void)handleChunkData:(nonnull NSData *)chunk {
if ([chunk length] > 0) {
NSString *str = [[NSString alloc] initWithData:chunk encoding:NSUTF8StringEncoding];
if (!str) {
[BPUtils printInfo:WARNING withString:@"Failed to UTF8 decode chunk: %@", chunk];
str = [[NSString alloc] initWithData:chunk encoding:NSASCIIStringEncoding];
[BPUtils printInfo:WARNING withString:@"ASCII: %@", str];
}
if (!str) {
[BPUtils printInfo:ERROR withString:@"Failed to ASCII decode chunk: %@", chunk];
exit(1);
}
NSRange range = [str rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]];
unsigned long numLines = 0;
unsigned long maxLines = [chunk length];
while (range.location != NSNotFound) {
self.line = [self.line stringByAppendingString:[str substringToIndex:range.location] ?: @""];
[self.log writeLine:@"%@", self.line];
[self parseLine:self.line];
if (numLines++ > maxLines) {
[BPUtils printInfo:ERROR withString:@"Infinite Loop Averted!: range {%d, %d}, %d, %@",
range.length, range.location, [chunk length], str];
[BPUtils printInfo:ERROR withString:@"Data: %@", chunk];
// Fail hard.
exit(1);
}
self.line = @"";
str = [str substringFromIndex:range.location+range.length];
range = [str rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]];
Expand All @@ -108,15 +126,11 @@ - (void)handleChunkData:(nonnull NSData *)chunk {

- (void)parseLine:(nullable NSString *)line {
[BPUtils printInfo:DEBUGINFO withString:@"[OUTPUT] %@", line];

[self onOutputReceived:line];
if (!line || ![line length]) {
return;
}

// We've seen hangs where the app just prints endless \n's so we
// don't count \n's as output.
[self onOutputReceived:line];

NSRange lineRange = NSMakeRange(0, [line length]);
BOOL logLine = YES;
NSRegularExpression *regex;
Expand Down

0 comments on commit 281fa3e

Please sign in to comment.