Fixed race causing missed wakeups

Thanks to Roland and Viktor for finding the final cause.