This avoids the 3-lock deadlock described in
https://github.com/canonical/pebble/issues/314. Other goroutines
may be holding the state lock and waiting for the services lock, so
it's problematic to acquire both locks at once. Break that part of the cycle.
We could do this inside serviceForStart/serviceForStop by manually
calling Unlock() sooner, but that's error-prone, so continue using defer,
but have the caller write the task log (which needs the state lock) after
the services lock is released.
This avoids the 3-lock deadlock described in https://github.com/canonical/pebble/issues/314. Other goroutines may be holding the state lock and waiting for the services lock, so it's problematic to acquire both locks at once. Break that part of the cycle.
We could do this inside serviceForStart/serviceForStop by manually calling Unlock() sooner, but that's error-prone, so continue using defer, but have the caller write the task log (which needs the state lock) after the services lock is released.
This is in preference to the more invasive change in https://github.com/canonical/pebble/pull/356