Use close event instead of exit event to ensure stdio streams are closed before returning the output.