Backport the fix for LP #1382751 to 1.20

See https://bugs.launchpad.net/juju-core/+bug/1382751. The PR for 1.22 is #1075.