Potential bug due to U32 ms counter resetting to 0 after a maximum of 49.71 days
Normal text is written by Petru. Italic bold text is written by Olivier.
The API might currently contain a small bug. The source of the bug is that the Tick Count (ms) primitive and the Time event data node are used as the source of timing (ms) information. Those two LabVIEW primitives output the number of ms as a U32 number, which can represent a maximum of 4,294,967,296 ms = 4,294,967.296 seconds = 71,582.78826 minutes = 1,193.04647 hours = 49.71 days. This means that the ms counter will eventually be reset to 0. When this happens, the ms counter appears to “go back in time”.
Some quick testing has confirmed that both the Tick Count (ms) primitive and the Time event data node are reading from the same U32 ms counter. I don’t know whether the ms counter is managed by the operating system (Windows in my case) or whether it’s even lower-level, perhaps implemented in the CPU hardware.
Preliminary testing suggests that this counter does NOT start from 0 when the computer boots up or restarts. If the U32 ms counter is proven not to start from 0 on computer boot-up, it would mean that the counter would reset to 0 after at most 49.7 days, but could occur much earlier, even as early as 1 second or 1 minute after the LabVIEW application starts running. In other words, the ms counter “going back in time” could happen any time. If the U32 counter starts from 0 when computer boots up, then the reset to 0 would occur only after 49.71 days of computer up-time. So the issue would affect only computers that are kept on all the time. It would not affect computers that are shut down overnight or over the weekend. But, unfortunately, I think it could affect LabVIEW apps that run on any computer, even those that are regularly shut down and restarted.
I believe that when the ms counter resets to 0, the current behaviour of the Get Timeout.vi would be to:
- Incorrectly flag the last iteration as Late
- Output a Timeout (ms) of 0 ms, which would make the next iteration occur instantly after the previous iteration. So there would be two iterations happening with virtually no time in between.
I totally agree with your conclusion
This might have little or no effect on most applications that use the API, but it might have an important effect on some applications. Perhaps even a safety-critical effect, depending on what the application uses the API for.
I believe that the issue could be solved by either:
- Inside Get Timeout.vi using a Less? node to detect whether the ms counter value output by Tick Count (ms) is less than that from Last Execution. If this is true, then the ms counter must have just been reset to 0. Then the rest of the Get Timeout.vi would need to be modified to better handle this situation. I feel this is the correct way to implement it. The difficult part is to not brake the current behavior when starting the HL or modifying the period. I have unit test in the project (not provided by the package). I'll need to start creating the failing test and then implement the improvement. I like the challenge.
- The whole API could rely on either the Get Date/Time In Seconds or the High Resolution Relative Seconds.vi functions, which never reset their output values to 0, instead of on the Tick Count (ms) and the Time event data node. This solution would add some shift in the last execution time value with a greater risk of incorrect implementation by developer if the call of the function in Timeout case is not done properly
Solution 1 seems to be simpler and cleaner to implement than solution 2. Yes