After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 754994 - g_date_time_get_second () sometimes returns an off-by-one result
g_date_time_get_second () sometimes returns an off-by-one result
Status: RESOLVED FIXED
Product: glib
Classification: Platform
Component: general
unspecified
Other All
: Normal normal
: ---
Assigned To: gtkdev
gtkdev
: 693721 (view as bug list)
Depends on:
Blocks:
 
 
Reported: 2015-09-14 13:05 UTC by Iain Lane
Modified: 2017-10-06 11:47 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
tests/gdatetime: Use g_get_current_time () instead of time() to avoid timing issues (3.44 KB, patch)
2015-09-14 13:05 UTC, Iain Lane
none Details | Review
reproducer (448 bytes, text/x-csrc)
2015-09-14 13:07 UTC, Iain Lane
  Details
GDateTime test: fix occasional failures (2.92 KB, patch)
2015-10-15 14:23 UTC, Allison Karlitskaya (desrt)
committed Details | Review

Description Iain Lane 2015-09-14 13:05:23 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.
Comment 1 Iain Lane 2015-09-14 13:05:27 UTC
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.
Comment 2 Iain Lane 2015-09-14 13:07:43 UTC
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).
Comment 3 Allison Karlitskaya (desrt) 2015-10-15 14:23:02 UTC
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().
Comment 4 Colin Walters 2015-10-15 15:45:11 UTC
Review of attachment 313375 [details] [review]:

LGTM, and thanks for taking the time to fix this.
Comment 5 Allison Karlitskaya (desrt) 2015-10-16 11:11:56 UTC
(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
Comment 6 Philip Withnall 2017-10-06 11:47:32 UTC
*** Bug 693721 has been marked as a duplicate of this bug. ***