Gracefully handle failure to allocate memory for storing stdout of a task (2)

Extends #354.

@dpandiar