Skip to content

Commit de5f87c

Browse files
committed
Merge pull request #1 from pornel/master
Pipe stdin async
2 parents 3de28fb + 252ae7a commit de5f87c

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

Classes/Util/PBEasyPipe.m

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,24 @@ + (NSString*) outputForCommand:(NSString *) cmd
8686
}
8787

8888
NSFileHandle* handle = [[task standardOutput] fileHandleForReading];
89+
NSFileHandle *inHandle = nil;
8990

9091
if (input) {
9192
[task setStandardInput:[NSPipe pipe]];
92-
NSFileHandle *inHandle = [[task standardInput] fileHandleForWriting];
93-
[inHandle writeData:[input dataUsingEncoding:NSUTF8StringEncoding]];
94-
[inHandle closeFile];
93+
inHandle = [[task standardInput] fileHandleForWriting];
9594
}
9695

9796
[task launch];
97+
98+
if (input && inHandle) {
99+
// A large write could wait for stdout buffer to be flushed by the task,
100+
// which may not happen until the task is run. The task may similarly wait
101+
// for its stdout to be read before reading its stdin, causing a deadlock.
102+
dispatch_async(dispatch_get_global_queue(0, 0), ^{
103+
[inHandle writeData:[input dataUsingEncoding:NSUTF8StringEncoding]];
104+
[inHandle closeFile];
105+
});
106+
}
98107

99108
NSData* data = [handle readDataToEndOfFile];
100109
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

0 commit comments

Comments
 (0)