Fix timeout issues related to UI sync.

The sample code I provided should exhibit the exact same behaviour as your code.