Issue
I have a REST endpoint on spring mvc which has a potential chance of going into an infinite loop if the user enters wrong data. What are the best practices to kill off the infinite loop after n seconds? Is there any other suggestion on how to prevent it? On my local machine temperatures skyrocketed after the controller got stuck in an infinite loop, though it could still take in new requests.
Solution
The first and best practice is to add a loop-exit condition to avoid the infinite loop. If this may be caused by data entered by user - validate that data and whether return an error in case of invalid data or set default correct values so that you will avoid the infinite loop.
Not good, but still working could be to run your loop in the separate thread with timeout or to limit the loop by specific number - i.e. put a condition that after 100 loops you always exit out of the loop. As mentioned I don't recommend last2 options, so try to implement validation, default values, and proper exit conditions.
Also, I think your case is irrelevant to spring MVC - your case can happen in java in general, so nothing specific to spring.
Answered By - GiorgosDev