Debugging a Silly Mistake

Yes! It took more than a year for the whole adventure!

Debugging is like hunting the mistakes of your past. After finding the mistake begins the ordeal to identify what went wrong and how to correct it.

How it started

It was lockdown due to COVID19. I was annoyed by the advertisements everywhere. So I decided to set up an ad-blocker. I settled on AdGuardHome - A network-wide ads & trackers blocking DNS server. Installed it on my old android phone using the Termux Project. It has to be up almost 24/7. Without it, the internet would stop working. I wrote some scripts to make it work properly. I did changes to the scripts from time to time. A year went by with no hiccups.

One fine morning I was sleeping peacefully when my brother disrupted my sleep and woke me up abruptly saying, "What did you do? Internet is not working!".

Wrath of the WiFi

So I woke up hesitantly and started debugging. It was a strange error. The phone, where AdGuardHome was installed, was not connecting to the main router's wifi. It worked fine all year. What could go wrong?

Then I did what any sane android user/developer would do!! I restarted the phone. Aaaand... Nope! That didn't work. The phone's wifi was working. So I manually connected it to the main router's wifi and called it a day. But it wasn't enough.

Wrath of the WiFi continued

It continued to happen on random nights and days. Since it was not that troublesome, I fixed it manually every time. But the frequency of such occurrences kept increasing.

So one frustrating evening, I decided to hunt the issue to the end. Fortunately, I was able to identify the issue within an hour. The phone's wifi config files, where passwords were saved, were getting wiped randomly. So the fix was easy. Save the config files in a safe location and restore them when such an issue happens and soft restart the phone. I tested it manually and it worked. I then created a Cron job which would run every few minutes and check if the wifi is connecting properly.

Awesome! I was happy with the solution and went to sleep. Issue solved!!

Return of the Bug

Or at least that's what I thought! After a few months, the bug resurfaced again. I started debugging again. I checked the script line by line. I ran it multiple times. The solution was correct. It was working as intended! Now what?? What did I miss? Where could the issue be? I was frustrated for days.

Shebang and the silly mistake

After countless hours, it occurred to me to check if the cron job was even working or not. And there!! Found the silly mistake I did! I didn't write the Shebang at all!!!! (In this case, shebang was #!/data/data/com.termux/files/usr/bin/bash)

It never crossed my mind that it was necessary because the script ran fine manually. Later, I found out why I was under the impression that it would work fine without a shebang. Because all the boot scripts were written like a regular text files. Yes, it was my mistake! The shebang was required to run the script via cron, unlike the boot scripts. Finally, the bug was hunted!

The lessons learned

Later, I added simple logging to the script. If I had done that in the beginning, it would have helped in catching the cron issue sooner. Always try to do logging. Even if it seems overkill for small scripts/projects, it helps.

If I had documented all the changes I did, it would have been helpful while writing this article. Document all the changes you do if possible. It helps you remember what you did and why. I wrote what I fixed and future todos in a simple text file later.

Don't forget to revisit your code from time to time. It can help you spot something you can optimize.

Conclusion

It was really fun writing all the scripts and making things work as I wanted. It was even more fun discovering bugs and hunting them.

I wanted to write something for a long time. Finally, I wrote this debugging adventure. This is my first article. Hope you enjoyed this article and learned something! Thank you for reading!

This hunt is over but the Bugs will be back!

Feel free to ask any questions in the comments. I would also love to connect with you all on LinkedIn and Twitter.