MQTT threading bug

HA’s mqtt fails to resubscribe quite frequently after it loses a connection on my Pi B+.

After some judicious logging, I have found that the problem is that in routine MQTT.async_subscribe (mqtt/init.py line 520) the topic is put in the array self.progress[mid] after calling subscribe, but sometimes the on_subscribe callback is reached before this, so the callback fails to find the topic, and it is not subscribed to again.

The thread needs to be stopped until the return from the subscribe has been processed, but I’m not familiar with asyncio, so I have no idea how that should be done.

I could just raise a bug, but hopefully someone can help me actually find a solution, rather than a problem.

@gpbenton In fact I think there is a PR merged to 0.58 that addresses just this problem: https://github.com/home-assistant/home-assistant/pull/10133.