Ensure metadata file gets closed

Fixes: https://bugs.launchpad.net/juju-core/+bug/1392514