Wait for tasks to finish before shutting down
Shutdown tasks before logout, fixes #297