Breaking the Loop: Detecting and Mitigating Denial-of-Service Vulnerabilities in Large Language Models