Avoid using closed handle and double free memory after replication failure