Perhaps:
- the child process always exits with 0, even if it fails to produce output
- it fails unpredictably in this way (so your cut-and-paste works). Perhaps because of a bug or intermittent memory allocation failure due to system load?
One way to get more information (although it may make the whole thing run so slowly as to not reproduce the problem usefully) would be to run your script under
strace -f. When the script exits, you should see the exit code of the child process and also whether it died to to a signal or not.