Fix potential race condition in timeout handling