ref_transaction_commit(): fix atomicity and avoid fd exhaustion