Finish flushing buffer before exiting process (fix #333)