tests, master: Fix test_metadata_recovery run with valgrind

It was not possible to run this test using valgrind. After some fixes it
is possible, but valgrind found some memory leak in the master server, which
is also fixed in this commit.