Error removing snaps after refresh core snap

I have seen similar errors removing snaps on travis. Initially it was happening with lxd snap but currently other snaps are failing too with the same error.

The error is easy to reproduce following the steps:

  1. Install core snap from stable
  2. Install lxd snap
  3. remove lxd snap
  4. refresh core snap to beta/candidate
  5. Install lxd snap
  6. Remove lxd snap (error is displayed and then the snap cannot be removed anymore)

This are two links showing the error on travis for different snaps:
https://travis-ci.org/snapcore/spread-cron/builds/434881402#L2110
https://travis-ci.org/snapcore/spread-cron/builds/434881402#L2669

This are the steps which I followed to reproduce the error with the lxd snap on a google machine:
sergio@cachiomachine:~/workspace/snapcore/spread-images$ spread -shell google:ubuntu-18.04-64:tasks/google/start-instance
2018-09-25 11:39:37 Project content is packed for delivery (380.77KB).
2018-09-25 11:39:37 If killed, discard servers with: spread -reuse-pid=24854 -discard
2018-09-25 11:39:37 Allocating google:ubuntu-18.04-64…
2018-09-25 11:39:56 Waiting for google:ubuntu-18.04-64 (sep251439-709408) to boot at 35.231.76.188…
2018-09-25 11:40:28 Allocated google:ubuntu-18.04-64 (sep251439-709408).
2018-09-25 11:40:28 Connecting to google:ubuntu-18.04-64 (sep251439-709408)…
2018-09-25 11:40:30 Connected to google:ubuntu-18.04-64 (sep251439-709408) at 35.231.76.188.
2018-09-25 11:40:30 Sending project content to google:ubuntu-18.04-64 (sep251439-709408)…
2018-09-25 11:40:38 Preparing google:ubuntu-18.04-64:tasks/google/…
2018-09-25 11:40:51 Executing google:ubuntu-18.04-64:tasks/google/start-instance (1/1)…
2018-09-25 11:40:51 Starting shell instead of executing google:ubuntu-18.04-64:tasks/google/start-instance…
google:ubuntu-18.04-64 …/tasks/google/start-instance# snap info core
name: core
summary: snapd runtime environment
publisher: Canonical✓
license: unset
description: |
The core runtime environment for snapd
type: core
snap-id: 99T7MUlRhtI3U0QFgl5mXXESAiSwt776
tracking: stable
refresh-date: yesterday at 05:09 UTC
channels:
stable: 16-2.35 (5328) 92MB -
candidate: 16-2.35.2 (5548) 92MB -
beta: 16-2.35.2 (5548) 92MB -
edge: 16-2.35.2 (5571) 92MB -
installed: 16-2.35 (5328) 92MB core
google:ubuntu-18.04-64 …/tasks/google/start-instance# snap install lxd
lxd 3.5 from Canonical✓ installed
google:ubuntu-18.04-64 …/tasks/google/start-instance# snap remove lxd
lxd removed
google:ubuntu-18.04-64 …/tasks/google/start-instance# snap refresh core --beta
2018-09-25T14:42:56Z INFO Waiting for restart…
core (beta) 16-2.35.2 from Canonical✓ refreshed
google:ubuntu-18.04-64 …/tasks/google/start-instance# snap install lxd
lxd 3.5 from Canonical✓ installed
google:ubuntu-18.04-64 …/tasks/google/start-instance# snap remove lxd
error: cannot perform the following tasks:
- Stop snap “lxd” services ([start snap.lxd.daemon.service] failed with exit status 5: Failed to start snap.lxd.daemon.service: Unit snap.lxd.daemon.service not found.
)
- Remove data for snap “lxd” (8774) (remove /var/snap/lxd/common/ns/shmounts: device or resource busy)
google:ubuntu-18.04-64 …/tasks/google/start-instance# snap services
Service Startup Current
lxd.daemon disabled inactive
google:ubuntu-18.04-64 …/tasks/google/start-instance# snap logs lxd.daemon
2018-09-25T14:43:14Z lxd.daemon[3433]: 11: fd: 17: name=systemd
2018-09-25T14:43:14Z lxd.daemon[3433]: 12: fd: 18: unified
2018-09-25T14:43:14Z lxd.daemon[3433]: => Starting LXD
2018-09-25T14:43:14Z lxd.daemon[3433]: lvl=warn msg=“CGroup memory swap accounting is disabled, swap limits will be ignored.” t=2018-09-25T14:43:14+0000
2018-09-25T14:43:16Z systemd[1]: Stopping Service for snap application lxd.daemon…
2018-09-25T14:43:17Z lxd.daemon[3658]: => Stop reason is: crashed
2018-09-25T14:43:17Z systemd[1]: Stopped Service for snap application lxd.daemon.
2018-09-25T14:43:21Z lxd.daemon[3433]: lvl=eror msg=“Failed to start the daemon: failed to load TLS certificate: Failed to open /var/snap/lxd/common/lxd/server.crt for writing: open /var/snap/lxd/common/lxd/server.crt: no such file or directory” t=2018-09-25T14:43:21+0000
2018-09-25T14:43:21Z lxd.daemon[3433]: lvl=eror msg=“Failed to cleanly shutdown daemon: open /var/snap/lxd/common/lxd/cache/simplestreams.yaml: no such file or directory” t=2018-09-25T14:43:21+0000
2018-09-25T14:43:21Z lxd.daemon[3433]: Error: failed to load TLS certificate: Failed to open /var/snap/lxd/common/lxd/server.crt for writing: open /var/snap/lxd/common/lxd/server.crt: no such file or directory