apt-get dist-upgrade freezes at reading package lists due to appstreamcli in Ubuntu

Today, when I attempted to update the Ubuntu system to install the latest patches, the system hanged infinitely at the sudo apt-get update command. This appears to be a bug, but nonetheless, system patching is our priority and this is the workaround, till it gets fixed. To type the commands below, open a linux terminal window.

The computer just displayed the “Reading package lists” and only consumes CPU power. I pressed Ctrl-C to exit the process, the following error is displayed. After a few retries, the below error displayed.
** (appstreamcli:6416): WARNING **: Exception: Unable to get write lock on /var/cache/app-info/xapian/default: already locked
After probing the process list and the Internet, found that appstreamcli was misbehaving.

To prevent this error, firstly stop the update from hanging infinitely:
$ ps -fael | grep "appstream" <enter>
0 R root      5130     1 99  85   5 - 36202 -      10:42 ?        00:25:11 appstreamcli refresh

The process ID 5130 is the reason for the hang. To resolve, do following:
$ kill -9 5130 <enter>
$ sudo /bin/rm -rf /var/cache/app-info/xapian/default <enter>
$ sudo rm -vf /var/lib/apt/lists/* <enter>

Now try to update the system using:
$ sudo apt-get clean <enter>
$ sudo apt-get update <enter>
$ sudo apt-get dist-upgrade <enter>

It no longer displays the error.