GNOME Bugzilla – Bug 754994
g_date_time_get_second () sometimes returns an off-by-one result
Last modified: 2017-10-06 11:47:32 UTC
There a guard, introduced in 472dee39097881502bb593eb2effe37b3b6064a2, which is supposed to protect against this. It sometimes doesn't work though - see https://objectstorage.prodstack4-5.canonical.com/v1/AUTH_77e2ada1e7a84929a74ba3b87153c0ac/autopkgtest-wily/wily/ppc64el/g/glib2.0/20150913_170536@/log.gz (search for 'assertion failed') GLib:ERROR:/build/glib2.0-7SKaCg/glib2.0-2.45.7/./glib/tests/gdatetime.c:699:test_GDateTime_now_utc: assertion failed (tm.tm_sec == g_date_time_get_second (dt)): (36 == 37) Debugging at this stage shows that the time_t values *are* the same, and yet comparing the second component of the "tm" returned by time() as well as the result of g_date_time_get_second() on the GDateTime shows they are different. So it seems like g_date_time_get_second() has the bug here, unless maybe we're tripping on a microsecond precision issue. This patch fixes the test for some reason that I don't understand. It'd be better to figure out the initial problem though, before deciding whether to fix the test or GDateTime itself.
Created attachment 311276 [details] [review] tests/gdatetime: Use g_get_current_time () instead of time() to avoid timing issues Resolves test failure when seconds tick over during the test.
Created attachment 311277 [details] reproducer and here is a program which reproduces this. It does the same thing as the testsuite: 1. time() 2. g_date_time_new_now_utc() 3. time() Check that if 1 == 3, then g_date_time_get_second(2) == seconds(1).
Created attachment 313375 [details] [review] GDateTime test: fix occasional failures We were using the time() library call to get the current time from the system in order to compare it to the time returned by g_date_time_new_now(). Of course, we took care to ensure that the time (in seconds) didn't change in the middle of this process by checking the before and after value of the system time. Unfortunately, the system time as measured by time() was being taken from a less-accurate clock source than the time used by GDateTime. As a result, we could have GDateTime already into the next second while the "seconds" value of the time returned by time() was still in the last one, even when checked "after". Avoid the problem by using the same ultimate source for time -- g_get_real_time(). This is based on a similar patch from Iain Lane, but it uses g_get_real_time() instead of g_get_current_time().
Review of attachment 313375 [details] [review]: LGTM, and thanks for taking the time to fix this.
(In reply to Colin Walters from comment #4) > thanks for taking the time iswydt... Attachment 313375 [details] pushed as 419f571 - GDateTime test: fix occasional failures
*** Bug 693721 has been marked as a duplicate of this bug. ***