Fix potential stack overflow and deadlock under heavy load