Fixed a race condition during migration finalization on master node. Also fixes #1317.