File tree Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Expand file tree Collapse file tree 1 file changed +12
-3
lines changed Original file line number Diff line number Diff 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];
You can’t perform that action at this time.
0 commit comments