Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sSN4T-006iPq-1X for pgsql-hackers@arkaria.postgresql.org; Fri, 12 Jul 2024 20:40:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sSN4P-00BWGD-Po for pgsql-hackers@arkaria.postgresql.org; Fri, 12 Jul 2024 20:40:45 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sSN4P-00BWG5-93 for pgsql-hackers@lists.postgresql.org; Fri, 12 Jul 2024 20:40:45 +0000 Received: from mail-yw1-x112d.google.com ([2607:f8b0:4864:20::112d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sSN4M-001kom-IR for pgsql-hackers@postgresql.org; Fri, 12 Jul 2024 20:40:44 +0000 Received: by mail-yw1-x112d.google.com with SMTP id 00721157ae682-643efaf0786so23372607b3.1 for ; Fri, 12 Jul 2024 13:40:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1720816842; x=1721421642; darn=postgresql.org; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=O2dZT/CK/AF0Wf+KBf9pxYcEmuiFkX+iotv8Fg0g+z8=; b=lvJrvINxxF+SFJTaOlPk8nDKR9wIav8mDor/kUkg1AusD+fqTqJF9tudsGFuq8sVn+ tmJyjw4pF9gNnnTRV3pUg/uiHEEUraFEf5+4vdvXTRoolNw2iLa/Ne+Qf3SAVlSdzWXD C9vmU+w1hBrzdhH9uKG+Cd3EmSHB906LyYxVE0/KM7HJHSZYYwc1oABeKRs679wSwdYG zaoiBtFwalQioIqWf+W0yFWXzm+FQLxhSvyYQISPouC21vJbHBSM5wF0E9srsHDYcROP 7ftqSg1jvAuFg1ObSfZyozf2Eitx2ZXP7gpi+BiilJsT2w02MTr9n8JaynzVDf0dasmu 8S2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720816842; x=1721421642; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=O2dZT/CK/AF0Wf+KBf9pxYcEmuiFkX+iotv8Fg0g+z8=; b=SPd2OPYnJibpi5Fe1P0DLhIZG6npkDsjFyoyk0XN1DDjLYAmeeevnfq1oJdAYtBteh Pd7SlZHqHrvhVNroUqhcYlR4LqQdW0bKDjZ+h9zK0nfp62SAZETnaHB+RUfE6E39WU3C uaNUIzUD8k9plj9huzCyMn9DbkenQ6xJDkW2US6OrnHeRyabqtf6p83P+6Wb3J7aQfPr TUb80M6VYwVUIFr5WnbLCl0BFVTuG+Wsm9jzptzLp+2FHnVPOyzJHgrtT5Ku3hdfGHNr 0dpBVB3Y8gMOMQj/soBLmUgZJ1I55UxZPf6uRAmeFlN1VQ9I0uVRI4q8Uor9lb1Mqs2i jVyQ== X-Forwarded-Encrypted: i=1; AJvYcCW5HenWr2I8/lenwl5RzbyrryjW5OknBJkmabr0dZ/Jrf+SCDy4MLNGyWlFaNLrBy4geWp2a9iA9Evfhpve6g2YAklemtDQQN8NAoJT X-Gm-Message-State: AOJu0Yz//Rx8vbj6DiyhuVQZh8XY7CuIA/Sml+dwJ9Cc2hARUB2NSnh0 TNhGDC4CY0dsCxyihYMbHwtyXFZ/J9iqtP+0URhzgdocuCq0e4cL X-Google-Smtp-Source: AGHT+IHKix7DLpKryDby8JnJQGeeS07eLltDDFVSw+BtJBSFDpv2+WK+JNt7Qh4kZFXN6IqYcZBEUA== X-Received: by 2002:a0d:ec48:0:b0:627:de5d:cf36 with SMTP id 00721157ae682-658f07d7575mr124543527b3.39.1720816841608; Fri, 12 Jul 2024 13:40:41 -0700 (PDT) Received: from smtpclient.apple (72-21-196-64.amazon.com. [72.21.196.64]) by smtp.gmail.com with ESMTPSA id af79cd13be357-79f18ff69basm436776785a.19.2024.07.12.13.40.39 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Jul 2024 13:40:41 -0700 (PDT) From: Sami Imseih Message-Id: <521A6F3B-77E1-4F3F-B15E-BCF26D52C698@gmail.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_D8E0CD93-F21D-4827-B187-C0EAD1D5D902" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3774.600.62\)) Subject: Re: Restart pg_usleep when interrupted Date: Fri, 12 Jul 2024 15:39:57 -0500 In-Reply-To: Cc: Bertrand Drouvot , Tom Lane , Robert Haas , "pgsql-hackers@postgresql.org" To: Nathan Bossart References: <373D199B-2312-431D-8018-F2808913F979@gmail.com> <3712E898-03DA-4E03-AF51-D3E24DE68216@gmail.com> <2F781744-69B8-4BC2-A6EF-3998DCCDB634@gmail.com> <01A15AEA-C35C-41DF-8E81-3B5A0B523939@gmail.com> X-Mailer: Apple Mail (2.3774.600.62) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_D8E0CD93-F21D-4827-B187-C0EAD1D5D902 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii > What does your testing show when you don't have > the extra check, i.e., >=20 > struct timespec delay; > struct timespec remain; >=20 > delay.tv_sec =3D microsec / 1000000L; > delay.tv_nsec =3D (microsec % 1000000L) * 1000; >=20 > while (nanosleep(&delay, &remain) =3D=3D -1 && errno =3D=3D = EINTR) > delay =3D remain; >=20 This is similar to the first attempt [1],=20 +pg_usleep_handle_interrupt(long microsec, bool force) { if (microsec > 0) { #ifndef WIN32 struct timespec delay; + struct timespec remaining; =20 delay.tv_sec =3D microsec / 1000000L; delay.tv_nsec =3D (microsec % 1000000L) * 1000; - (void) nanosleep(&delay, NULL); + + if (force) + while (nanosleep(&delay, &remaining) =3D=3D -1 && errno =3D=3D= EINTR) + delay =3D remaining; but Bertrand found long drifts [2[ which I could not reproduce. To safeguard the long drifts, continue to use the &remain time with an=20= additional safeguard to make sure the actual sleep does not exceed the=20= requested sleep time. [1] = https://www.postgresql.org/message-id/7D50DC5B-80C6-47B5-8DA8-A6C68A115EE5= %40gmail.com [2] = https://www.postgresql.org/message-id/ZoPC5IeP4k7sZpki%40ip-10-97-1-34.eu-= west-3.compute.internal Regards, Sami=20 --Apple-Mail=_D8E0CD93-F21D-4827-B187-C0EAD1D5D902 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii

What = does your testing show when you don't have
the extra check, = i.e.,

= struct timespec delay;
struct timespec = remain;

= delay.tv_sec =3D microsec / 1000000L;
= delay.tv_nsec =3D (microsec % 1000000L) * 1000;

while = (nanosleep(&delay, &remain) =3D=3D -1 && errno =3D=3D = EINTR)
= = delay =3D = remain;


This = is similar to the first attempt = [1], 

+pg_usleep_handle_interrupt(long = microsec, bool force)
 {
    if = (microsec > 0)
    {
 #ifndef = WIN32
        struct timespec = delay;
+       struct timespec = remaining;
 
        = delay.tv_sec =3D microsec / 1000000L;
      =   delay.tv_nsec =3D (microsec % 1000000L) * 1000;
- =       (void) nanosleep(&delay, = NULL);
+
+       if = (force)
+           while = (nanosleep(&delay, &remaining) =3D=3D -1 && errno =3D=3D = EINTR)
+               = delay =3D remaining;


but = Bertrand found long drifts [2[ which I could not reproduce.
To = safeguard the long drifts, continue to use the &remain time with = an 
additional safeguard to make sure the actual sleep = does not exceed the 
requested sleep = time.

[1] = https://www.postgresql.org/message-id/7D50DC5B-80C6-47B5-8DA8-A6C68A115EE5= %40gmail.com


Regards,

Sami&nb= sp;

= --Apple-Mail=_D8E0CD93-F21D-4827-B187-C0EAD1D5D902--