I am facing an issue with NSTask (Process), NSPipe (Pipe) and NSFileHandle (FileHandle).
I have an app that executes a child process (command line utility) and gets its output via Pipe instances set as standardI/O of Process instance.
I can observe two logic flows:
Once the child process exits, -[Process readabilityHandler] gets called where I gather the $0.availableData.
Once the child process generates output (i.e. print() is executed by the child process), -[Process readabilityHandler] also gets called which makes sense. This is the case when exit() is not called by the child process (i.e. continuous execution).
Wrong behaviour also happens if I don't rely on readabilityHandler, but instead use Notification to receive changes in data availability. In such case, notification gets called continuously with empty data (even though child process does generate the output).
If I don't create Pipe instance for standardI/O of the Process, then readabilityHandler does get called, but with an empty data continuously.
I have created a DTS #756462540, since this doesn't make any sense.
Prerequisites*
app is not sandboxed
Big Sur 11.0.1 (20B29)
Xcode 12.3 (12C33)
Unfortunately I did everything I found online, including checking out blog posts from 2009.
What is actually happening is that due to stdout buffering, data is not being written to that stdout. What helped was to add fflush() calls right after the Swift.print() calls in the child process.
I am writing down a series of blog posts related to this, however, what actually helped was a TSI response from an Apple Engineer.