How to be safe from infinite loops[Technique Tuesdays]
An approach you can use to avoid writing code that goes into infinite loops
Hey, it’s your favorite cult leader here 🦹♂️🦹♂️
On Tuesdays, I will cover problem-solving techniques that show up in software engineering, computer science, and Leetcode Style Questions📚📚📝📝.
To get access to all my articles and support my crippling chocolate milk addiction, consider subscribing if you haven’t already!
p.s. you can learn more about the paid plan here. If you like my writing, please share it with your crew. Using the button below will get you very cool referral benefits (free subscriptions, the secret to becoming a super saiyan, and …)-
Infinite loops are one of the most annoying problems you will deal with as a developer. They can cause all kinds of problems- from a bad user experience, to overutilization of resources, to even complete service shutdowns. What makes these particularly nasty is that detecting these loops can be a very challenging task, especially in software that deals with multiple processes or with many moving parts. Fortunately, there has been great work in detecting these infinite loops. The MIT paper- Detecting and escaping infinite loops with jolt- presents a great approach for detecting infinite loops in an automated way. Their approach is extremely powerful because automated checks meaningfully improve the performance and security of the system.
In this article, I will be going over a framework that you can use to write code that is infinite loop safe, based on this MIT paper. By leveraging this approach, you will be able to protect your systems getting infinite-Tsukuyomi’ed by your code (shoutout to those of that get this reference).
We evaluated Jolt by applying it to detect and escape eight infinite loops in five benchmark applications. Jolt was able to detect seven of the eight infinite loops (the eighth changes the state on every iteration). We also evaluated the effect of escaping an infinite loop as an alternative to terminating the application. In all of our benchmark applications, escaping an infinite loop produced a more useful output than terminating the application. Finally, we evaluated how well escaping from an infinite loop approximated the correction that the developers later made to the application. For two out of our eight loops, escaping the infinite loop produced the same output as the corrected version of the application.
-We will be basing our strategy around Jolt.
How to be safe from Infinite Loops
The Jolt Approach- Since infinite loops are a fairly basic idea, I will not waste your time talking about them. Instead, let’s directly talk about what the authors of the Jolt Program presented. The idea behind Jolt is relatively straightforward- At the user’s request, Jolt attaches to an application to monitor its progress. Specifically, Jolt records the program state at the start of each loop iteration. If two consecutive loop iterations produce the same state, Jolt reports to the user that the application is in an infinite loop. This simple approach works wonders.
Where Jolt failed- Jolt is great, but it has a glaring weakness- it struggles to detect infinite loops where the program cycles between a few different states (we get stuck in what is called a cycle in graph theory terms). In this case, two consecutive loop iterations produce different outcomes, which sneaks by the detection. We can sort this out by storing each state in a state and breaking if we come into a previously seen state (DFS algorithms use this technique). But this approach grows linearly with the length of the cycle. The code we write should account for this.
Be safe from infinite loops, Step 1: Make progress in each step- The first key principle to writing IL safe code is to ensure that your control variables always progress in some way. Look into incorporating logic that moves the program forward- whether by incrementing a counter, updating variables, or iterating through a data structure. This will curtail the looping potential of your code. Integrate multiple control variables to account for various possibilities, and your code gonna be tighter than Robbert Whittaker's wrestling defense.
Be safe from infinite loops, Step 2: Have an exit strategy- As with all complicated entanglements in life, you should have clearly defined exit strategies for your loops. This will help a lot with step 1, and also allow you to quickly understand where things are going wrong.
Be safe from infinite loops, Step 3: Use kill-switches- Writing multiple kill switches will allow you to constrain the costs from infinite loops to a minimum. A maximum iteration limit or timeout mechanism might seem crude and dumb but these safeguards are particularly useful in scenarios where unexpected errors or unforeseen conditions may cause the loop to get stuck.
Implement tons of logging- Ultimately infinite loops can be very sinister when they fly by under the radar. This is where logging and rigorous observable systems become a huge help. We have a post on the 3 pillars of observability over here.
Infinite loops can be vicious as they cycle costs out of control. Applying this framework to the implementation of loops will help you ensure that your code stays safe from itself.
That is it for this piece. I appreciate your time. As always, if you’re interested in working with me or checking out my other work, my links will be at the end of this email/post. If you like my writing, I would really appreciate an anonymous testimonial. You can drop it here. And if you found value in this write-up, I would appreciate you sharing it with more people. It is word-of-mouth referrals like yours that help me grow. If you refer people using the button below, you will get many cool benefits such as magic swords, dragons, and complimentary premium subscriptions
Save the time, energy, and money you would burn by going through all those videos, courses, products, and ‘coaches’ and easily find all your needs met in one place at ‘Tech Made Simple’! Stay ahead of the curve in AI, software engineering, and the tech industry with expert insights, tips, and resources. 20% off for new subscribers by clicking this link. Subscribe now and simplify your tech journey!
Using this discount will drop the prices-
800 INR (10 USD) → 640 INR (8 USD) per Month
8000 INR (100 USD) → 6400INR (80 USD) per year (533 INR /month)
Reach out to me
Use the links below to check out my other content, learn more about tutoring, reach out to me about projects, or just to say hi.
Small Snippets about Tech, AI and Machine Learning over here
AI Newsletter- https://artificialintelligencemadesimple.substack.com/
My grandma’s favorite Tech Newsletter- https://codinginterviewsmadesimple.substack.com/
Check out my other articles on Medium. : https://rb.gy/zn1aiu
My YouTube: https://rb.gy/88iwdd
Reach out to me on LinkedIn. Let’s connect: https://rb.gy/m5ok2y
My Instagram: https://rb.gy/gmvuy9
My Twitter: https://twitter.com/Machine01776819