Cherry-pick erikhuda/thor@c06a272.

Fixes #2734 (non-atomic line endings in output).

Supersedes #2764. Note that this is on the `1-5-stable` branch.

I'll open a pull request for updating all of Thor to the latest master once the 1.8.7 compatibility bug discussed in #2764 has been fixed.