Skip to content

Commit

Permalink
Fix race condition leading to app crash
Browse files Browse the repository at this point in the history
  • Loading branch information
opa334 committed Feb 21, 2024
1 parent 32a8987 commit 8a40553
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions Application/Dopamine/UI/DOUIManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ NS_ASSUME_NONNULL_BEGIN
{
DOPreferenceManager *_preferenceManager;
NSDictionary *_fallbackLocalizations;
NSLock *_logLock;
}

@property (nonatomic, retain) NSObject<DOLogViewProtocol> *logView;
Expand Down
16 changes: 12 additions & 4 deletions Application/Dopamine/UI/DOUIManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ - (id)init
if (self = [super init]){
_preferenceManager = [DOPreferenceManager sharedManager];
_logRecord = [NSMutableArray new];
_logLock = [NSLock new];
}
return self;
}
Expand Down Expand Up @@ -209,12 +210,17 @@ - (void)sendLog:(NSString*)log debug:(BOOL)debug update:(BOOL)update
{
if (!self.logView || !log)
return;


[_logLock lock];

[self.logRecord addObject:log];

BOOL isDebug = self.logView.class == DODebugLogView.class;
if (debug && !isDebug)
if (debug && !isDebug) {
[_logLock unlock];
return;
}


if (update) {
if ([self.logView respondsToSelector:@selector(updateLog:)]) {
Expand All @@ -224,6 +230,7 @@ - (void)sendLog:(NSString*)log debug:(BOOL)debug update:(BOOL)update
else {
[self.logView showLog:log];
}
[_logLock unlock];
}

- (void)sendLog:(NSString*)log debug:(BOOL)debug
Expand Down Expand Up @@ -273,6 +280,9 @@ - (void)startLogCapture

while ((bytes_read = read(stdout_pipe[0], buffer, sizeof(buffer) - 1)) > 0) {
@autoreleasepool {
// Tee: Write back to the original standard output
write(stdout_orig[1], buffer, bytes_read);

buffer[bytes_read] = '\0'; // Null terminate to handle as string
for (int i = 0; i < bytes_read; ++i) {
if (buffer[i] == '\n') {
Expand All @@ -286,8 +296,6 @@ - (void)startLogCapture
}
}
}
// Tee: Write back to the original standard output
write(stdout_orig[1], buffer, bytes_read);
}
}
close(stdout_pipe[0]);
Expand Down

0 comments on commit 8a40553

Please sign in to comment.