Sudo apt purge lxd hangs

On Ubuntu 18.04, I’m trying to completely uninstall lxd so I can do lxd init again (a la https://blog.simos.info/how-to-initialize-lxd-again/ ). Alas, ‘sudo apt purge lxd’ is hanging at 9% progress, and pstree shows

sh───sudo───apt───dpkg───lxd.prerm───systemctl───systemd-tty-ask

pstree -a shows

  │   │   │       └─sudo apt purge lxd lxd-client
  │   │   │           └─apt purge lxd lxd-client
  │   │   │               └─dpkg --status-fd 49 --no-triggers --force-depends --remove lxd:amd64 lxd-client:amd64
  │   │   │                   └─lxd.prerm /var/lib/dpkg/info/lxd.prerm remove
  │   │   │                       └─systemctl stop lxd-containers.service
  │   │   │                           └─systemd-tty-ask --watch

There’s no output. Evidently something is squiffy about how lxd.prerm is asking that question.

And hilariously, killing that lxd.prerm moved on to another hang:

  │   │   │       └─sudo apt purge lxd lxd-client
  │   │   │           └─apt purge lxd lxd-client
  │   │   │               └─dpkg --status-fd 49 --no-triggers --force-depends --remove lxd:amd64 lxd-client:amd64
  │   │   │                   └─lxd.postinst /var/lib/dpkg/info/lxd.postinst abort-remove
  │   │   │                       └─systemctl start lxd-containers.service
  │   │   │                           └─systemd-tty-ask --watch

https://bugs.launchpad.net/ubuntu/+source/systemd/+bug/1456789 looks vaguely similar, and suggests that sudo systemctl daemon-reload may work around a similar hang.

I was getting such hangs when I was repeatedly installing and uninstalling LXD. That was the reason I wrote the post.

The lessons I got from repeated installs and uninstalls, is that lxd init performs an initialization that is not atomic. That is, if an lxd init is interrupted by an error, whatever was done in the previous steps, stays there and does not get rolled back. You would need to do any clean-up. This is understandable and OK.

The other lesson is that when you uninstall, you are hitting on issues that may be bugs relating to systemd as well. You get a hang at systemd-tty-ask, https://www.freedesktop.org/software/systemd/man/systemd-tty-ask-password-agent.html That looks like it is waiting for a password (or it asks some question) but somehow you do not get the prompt to type it. When you kill a process that is hang, you end up having systemd ask for the password (or some prompt) for the next step (and so on).
Therefore, if you kill a process to get the uninstall to continue, you end up going into a more complicated state. It’s OK to kill the processes so that the uninstall gets completed and then you have the change to manually clean up any leftovers. If you want to diagnose the initial systemd-tty-ask, you would need to look into cases that systemd displays a prompt but you do not get to view the prompt.