Cherry-pick erikhuda/thor@c06a272.

Fixes #2734 (non-atomic line endings in output).
Merge pull request #2771 from TimMoore/thor-line-ending-backport

Cherry-pick erikhuda/thor@c06a272.