Why isn't unregister_instance disposing instances with asynchronous publication?

Setting the PUBLISH_MODE QoS Policy's kind to ASYNCHRONOUS_PUBLISH_MODE allows you to make non-blocking write() calls. This means that instead of sending a sample before returning from write(), the data is enqueued and sent according to a flow controller. The HISTORY QoS Policy determines the size of this queue. For instance, with kind=KEEP_LAST and depth=1, the queue will have space for a single message. This can cause issues when trying to unregister an instance with autodispose_unregistered_instance set to true. If this is the case, the DataWriter will try to send two messages: first a dispose message and then an unregister message. Since the queue allows only one message and we are making non-blocking calls, there can be a situation in which only the latest message is sent, because it overrides the previous one inside the cache.

To fix this situation, you should either increase the HISTORY QoS policy's depth to 2, or set the PUBLISH_MODE QoS Policy's kind to SYNCHRONOUS_PUBLISH_MODE.