Debugging a critical local storage Heisenbug

September 21, 2016

The German theoretical physicist Werner Heisenberg first asserted the observer effect in quantum mechanics, which the act of observing a system causes its state to alter. This gave rise to the computer programming term Heisenbug for a bug which disappears or alter the program behavior when one attempts to debug it, which can be a very frustrating experience. When a customer was doing some testing before rolling launching their Cloneder based app we received an email saying the chat messages stopped updating after a few messages. First launching the server on the local dev environment and a Chrome browser for the Ionic framework based app, a quick check didn't show any problems, the data kept synchronizing from the server ok. Next step is to try on a device so I pushed a build onto my HTC M9 Android phone and after a few chat messages back and forward the problem manifested itself. So step 1 was complete - replicating the issue. Replicating the issues went on a few times, messaging with another developer over Skype to keep sending me more messages. It the logging looked normal, the app was trying to sync from the server from the the time of the last chat message it has received. But nothing was returning when clearly it should, as it does in Chrome. So something must be up with the query. The last date it passed to the sync query which is used in a Parse API called on the Query object Maybe its not a date anymore? But Why? So I put in some logging to check the type of the 'date' and push a new build onto the phone. As the app starts up it logs that its of type **string**. My hunch was correct! I log out and back in which clears the value, and then then the first sync the logging statement prints it as an **object** (of which it would be a date). Back to normal! Now sending a bunch of messages and having it sync a few times now, it doesn't manifest the problem. It keeps on working and syncing. Uh, did that log statement just fix it? I remove the log statement, push a build to the phone, send some messages and the bug arises again. I add the log statement, push a build to the phone, send some messages and it keeps working ok. And there's our Heisenbug! How do these type of bugs possibly happen? You might think there wouldn't be much difference between Chrome and the current Android system browser. The V8 JavaScript engine, like other interpreted language runtimes, perform analysis of the running code to optimise the generated code. Possibly it is a bug in the optimizer, and the presence of the logging statement affected the heuristics of the optimizer for it to not change the type of the value. That's the fun we get to deal with a software engineers. If you want to focus on the business value of your Ionic mobile app and less on dealing with annoying technical issues then our Cloneder [mobile app source code](/cloneder) could be just what you're looking for.