unicorn Ruby/Rack server user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <bofh@yhbt.net>
To: unicorn-public@yhbt.net
Subject: [PATCH] httpdate: favor gettimeofday(2) over time(2) for correctness
Date: Mon,  5 Jun 2023 09:15:35 +0000	[thread overview]
Message-ID: <20230605091535.296006-1-bofh@yhbt.net> (raw)

While scanning the git@vger.kernel.org mailing list, I've
learned time(2) may return the wrong value in the first 1 to 2.5
ms of every second.  While I'm not sure if the Date: response
header matters to anyone, returning the correct time seems

Link: https://lore.kernel.org/git/20230320230507.3932018-1-gitster@pobox.com/
Link: https://inbox.sourceware.org/libc-alpha/20230306160321.2942372-1-adhemerval.zanella@linaro.org/T/
Link: https://sourceware.org/bugzilla/show_bug.cgi?id=30200
 ext/unicorn_http/httpdate.c | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/ext/unicorn_http/httpdate.c b/ext/unicorn_http/httpdate.c
index 3f512dd..27a8f51 100644
--- a/ext/unicorn_http/httpdate.c
+++ b/ext/unicorn_http/httpdate.c
@@ -1,5 +1,5 @@
 #include <ruby.h>
-#include <time.h>
+#include <sys/time.h>
 #include <stdio.h>
 static const size_t buf_capa = sizeof("Thu, 01 Jan 1970 00:00:00 GMT");
@@ -43,13 +43,24 @@ static struct tm * my_gmtime_r(time_t *now, struct tm *tm)
 static VALUE httpdate(VALUE self)
 	static time_t last;
-	time_t now = time(NULL); /* not a syscall on modern 64-bit systems */
+	struct timeval now;
 	struct tm tm;
-	if (last == now)
+	/*
+	 * Favor gettimeofday(2) over time(2), as the latter can return the
+	 * wrong value in the first 1 .. 2.5 ms of every second(!)
+	 *
+	 * https://lore.kernel.org/git/20230320230507.3932018-1-gitster@pobox.com/
+	 * https://inbox.sourceware.org/libc-alpha/20230306160321.2942372-1-adhemerval.zanella@linaro.org/T/
+	 * https://sourceware.org/bugzilla/show_bug.cgi?id=30200
+	 */
+	if (gettimeofday(&now, NULL))
+		rb_sys_fail("gettimeofday");
+	if (last == now.tv_sec)
 		return buf;
-	last = now;
-	gmtime_r(&now, &tm);
+	last = now.tv_sec;
+	gmtime_r(&now.tv_sec, &tm);
 	/* we can make this thread-safe later if our Ruby loses the GVL */
 	snprintf(buf_ptr, buf_capa,

             reply	other threads:[~2023-06-05  9:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-05  9:15 Eric Wong [this message]
2023-06-05  9:28 ` [PATCH 2/1] httpdate: fix build with Ruby 2.7 (at least) Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

  List information: https://yhbt.net/unicorn/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230605091535.296006-1-bofh@yhbt.net \
    --to=bofh@yhbt.net \
    --cc=unicorn-public@yhbt.net \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).