Fixed a subtle infinite loop in a background thread