Exec through an exception to make sure everything is closed properly

Closes #1535

Also sets the CLOEXEC flag on locks, to make sure they're not inherited