Wait for all messages to be sent before disconnect (#13529)

Change-Id: Ic9d5d4ac3a3f19bb906cba64cf752461d4b16ba5