master: Fix deadlock in master

If master was stopped while dumping metadata, it couldn't tell if the dumping
had finished and waited for it indefinitely.

This commit introduces a function that waits for the dump to finish and uses
it during master termination.

Change-Id: I7b4a587932f81869def4481792d977e02440d2ca