Bug 1626734 Comment 3 Edit History

Note: The actual edited comment in the bug view page will always show the original commenter’s name and original timestamp.

So this is indeed a bug in the SystemTimeConverter code, specifically with the fact that it truncates fractional milliseconds [here](https://searchfox.org/mozilla-central/rev/158bac3df3a1890da55bdb6ffdaf9a7ffc0bfb0a/widget/SystemTimeConverter.h#202-203). Here's an example sequence I captured with printf logging:

- Call [`GetMessageTimeStamp(aEventTime=662853921)`](https://searchfox.org/mozilla-central/rev/158bac3df3a1890da55bdb6ffdaf9a7ffc0bfb0a/widget/windows/nsWindow.cpp#6339)
- This trickles down into [this call to IsNewerThanTimeStamp](https://searchfox.org/mozilla-central/rev/158bac3df3a1890da55bdb6ffdaf9a7ffc0bfb0a/widget/SystemTimeConverter.h#92)
- Inside `IsNewerThanTimestamp`, the `timeDelta` is `31` and the `timestampDelta` is `32.146563` from the reference time and timestamps, respectively. This causes the function to set `deltaFromNow=1` (because it truncates the `32.146563` and then subtracts `31`) and return `newer=false`.
- Back at the call site, we [subtract 1ms from `roughlyNow`](https://searchfox.org/mozilla-central/rev/158bac3df3a1890da55bdb6ffdaf9a7ffc0bfb0a/widget/SystemTimeConverter.h#120) and return it. This effectively returns a value that is `31.146563` ms ahead of the reference timestamp.
- Later, we again call `GetMessageTimeStamp` with the same `aEventTime=662853921`
- Again this trickles down into IsNewerThanTimeStamp, but this time `roughlyNow` is at a value `43.139479`ms ahead of the reference timestamp.
- Inside `IsNewerThanTimestamp`, we get `timeDelta` as `31` again, and `timestampDelta` as `43.139479`. So again the truncation/subtraction happens, and this function produces `deltaFromNow=12` and returns `newer=false`.
- Back at the call site, we subtract 12ms from `roughlyNow`, which returns a value that is `31.139479` ms ahead of the reference timestamp.

Note that the value returned in the second call to `GetMessageTimeStamp` is `0.007083`ms *behind* the value returned from the first call to `GetMessageTimeStamp`. We expect time to always go forward, but this code can result it in going backwards by a fractional millisecond.
So this is indeed a bug in the SystemTimeConverter code, specifically with the fact that it truncates fractional milliseconds [here](https://searchfox.org/mozilla-central/rev/158bac3df3a1890da55bdb6ffdaf9a7ffc0bfb0a/widget/SystemTimeConverter.h#202-203). Here's an example sequence I captured with printf logging:

- Call [`GetMessageTimeStamp(aEventTime=662853921)`](https://searchfox.org/mozilla-central/rev/158bac3df3a1890da55bdb6ffdaf9a7ffc0bfb0a/widget/windows/nsWindow.cpp#6339)
- This trickles down into [this call to IsNewerThanTimeStamp](https://searchfox.org/mozilla-central/rev/158bac3df3a1890da55bdb6ffdaf9a7ffc0bfb0a/widget/SystemTimeConverter.h#92)
- Inside `IsNewerThanTimestamp`, the `timeDelta` is `31` and the `timestampDelta` is `32.146563` from the reference time and timestamps, respectively. This causes the function to set `deltaFromNow=1` (because it truncates the `32.146563` and then subtracts `31`) and return `newer=false`.
- Back at the call site, we [subtract 1ms from `roughlyNow`](https://searchfox.org/mozilla-central/rev/158bac3df3a1890da55bdb6ffdaf9a7ffc0bfb0a/widget/SystemTimeConverter.h#120) and return it. This effectively returns a value that is `31.146563` ms after the reference timestamp.
- Later, we again call `GetMessageTimeStamp` with the same `aEventTime=662853921`
- Again this trickles down into IsNewerThanTimeStamp, but this time `roughlyNow` is at a value `43.139479`ms after the reference timestamp.
- Inside `IsNewerThanTimestamp`, we get `timeDelta` as `31` again, and `timestampDelta` as `43.139479`. So again the truncation/subtraction happens, and this function produces `deltaFromNow=12` and returns `newer=false`.
- Back at the call site, we subtract 12ms from `roughlyNow`, which returns a value that is `31.139479` ms after the reference timestamp.

Note that the value returned in the second call to `GetMessageTimeStamp` is `0.007083`ms *earlier* the value returned from the first call to `GetMessageTimeStamp`. We expect time to always go forward, but this code can result it in going backwards by a fractional millisecond.

Back to Bug 1626734 Comment 3