Fix the race condition in totalPendingTasks