All of lore.kernel.org
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox.com>
To: "Ævar Arnfjörð Bjarmason" <avarab@gmail.com>
Cc: git@vger.kernel.org, Johannes Schindelin <johannes.schindelin@gmx.de>
Subject: Re* jc/http-clear-finished-pointer
Date: Thu, 26 May 2022 12:37:31 -0700	[thread overview]
Message-ID: <xmqq7d68ytj8.fsf_-_@gitster.g> (raw)
In-Reply-To: <220526.86v8ts3z2k.gmgdl@evledraar.gmail.com> (=?utf-8?B?IsOG?= =?utf-8?B?dmFyIEFybmZqw7Zyw7A=?= Bjarmason"'s message of "Thu, 26 May 2022 20:51:23 +0200")

Ævar Arnfjörð Bjarmason <avarab@gmail.com> writes:

> On Thu, May 26 2022, Junio C Hamano wrote:
>
>> * jc/http-clear-finished-pointer (2022-05-24) 1 commit
>>  - http.c: clear the 'finished' member once we are done with it
>>
>>  Meant to go with js/ci-gcc-12-fixes
>>
>>  Will merge to 'next'?
>>  source: <xmqqczgqjr8y.fsf_-_@gitster.g>
>
> The end of the proposed commit message says:
>
>     [...]Clear the finished member before the control leaves the
>     function, which has a side effect of unconfusing compilers like
>     recent GCC 12 that is over-eager to warn against such an assignment.
>
> I cannot reproduce this suppressing the warning as noted in past
> exchanges, it's not affected by this "clear if we set it" pattern. It
> needs to be unconditionally cleared.

Interesting.  I still have conditional clearing in the tree, though
I was reasonably sure I got rid of the conditional and made it
always clear, when I rewrote that part of the log message.  After
all, I ran "commit --amend" so that I do not forget the issue after
sending https://lore.kernel.org/git/xmqqleurlt31.fsf@gitster.g/ X-<.

Thanks for catching.  What is queued is not what I intended to
queue.

But there is one thing that is puzzling.  Ever since this, together
with the three patches from Dscho for gcc12, got included in 'seen',
the branch started passing the Windows build that used to complain
and did not work, so at least with the version of gcc12 used over
there, it apparently is sufficient to clear only when we are
responsible for placing an address that is about to become invalid,
while leaving the pointer we didn't stuff in unmodified.

As far as I understand, with the most recent analysis by Dscho on
the http-push codepath, we can return to the loop while the slot is
holding a different request that is unrelated to ours that has
already finished without recursively calling run_active_slot(), and
with the current *(slot->finished)=1 trick, it will successfully
notify our loop that our request is done, even though slot->in_use
is set to true back again when it happens.  But by definition, at
that point, slot->finished is not used by anybody (obviously not by
us, but also not by the request that is currently using the slot,
because it hasn't used run_active_slot() and slot->finished is not
touched by it), so it is safe to unconditionally clear the member.

----- >8 --------- >8 --------- >8 --------- >8 --------- >8 -----
Subject: [PATCH v3] http.c: clear the 'finished' member once we are done with it

In http.c, the run_active_slot() function allows the given "slot" to
make progress by calling step_active_slots() in a loop repeatedly,
and the loop is not left until the request held in the slot
completes.

Ages ago, we used to use the slot->in_use member to get out of the
loop, which misbehaved when the request in "slot" completes (at
which time, the result of the request is copied away from the slot,
and the in_use member is cleared, making the slot ready to be
reused), and the "slot" gets reused to service a different request
(at which time, the "slot" becomes in_use again, even though it is
for a different request).  The loop terminating condition mistakenly
thought that the original request has yet to be completed.

Today's code, after baa7b67d (HTTP slot reuse fixes, 2006-03-10)
fixed this issue, uses a separate "slot->finished" member that is
set in run_active_slot() to point to an on-stack variable, and the
code that completes the request in finish_active_slot() clears the
on-stack variable via the pointer to signal that the particular
request held by the slot has completed.  It also clears the in_use
member (as before that fix), so that the slot itself can safely be
reused for an unrelated request.

One thing that is not quite clean in this arrangement is that,
unless the slot gets reused, at which point the finished member is
reset to NULL, the member keeps the value of &finished, which
becomes a dangling pointer into the stack when run_active_slot()
returns.  Clear the finished member before the control leaves the
function, which has a side effect of unconfusing compilers like
recent GCC 12 that is over-eager to warn against such an assignment.

Signed-off-by: Junio C Hamano <gitster@pobox.com>
---
 http.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/http.c b/http.c
index 229da4d148..9a98372f74 100644
--- a/http.c
+++ b/http.c
@@ -1367,6 +1367,32 @@ void run_active_slot(struct active_request_slot *slot)
 			select(max_fd+1, &readfds, &writefds, &excfds, &select_timeout);
 		}
 	}
+
+	/*
+	 * The value of slot->finished we set before the loop was used
+	 * to set our "finished" variable when our request completed.
+	 *
+	 * 1. The slot may not have been reused for another requst
+	 *    yet, in which case it still has &finished.
+	 *
+	 * 2. The slot may already be in-use to serve another request,
+	 *    which can further be divided into two cases:
+	 *
+	 * (a) If call run_active_slot() hasn't been called for that
+	 *     other request, slot->finished may still have the
+	 *     address of our &finished.
+	 *
+	 * (b) If the request did call run_active_slot(), then the
+	 *     call would have updated slot->finished at the beginning
+	 *     of this function, and with the clearing of the member
+	 *     below, we would find that slot->finished is now NULL.
+	 *
+	 * In all cases, slot->finished has no useful information to
+	 * anybody at this point.  Some compilers warn us for
+	 * attempting to smuggle a pointer that is about to become
+	 * invalid, i.e. &finished.  We clear it here to assure them.
+	 */
+	slot->finished = NULL;
 }
 
 static void release_active_slot(struct active_request_slot *slot)
-- 
2.36.1-306-g0dbcc0e187

  reply	other threads:[~2022-05-26 19:37 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-26  8:41 What's cooking in git.git (May 2022, #07; Wed, 25) Junio C Hamano
2022-05-26 16:02 ` Victoria Dye
2022-05-26 17:23   ` Junio C Hamano
2022-05-26 18:02     ` Ævar Arnfjörð Bjarmason
2022-05-26 18:30     ` Victoria Dye
2022-05-26 18:40       ` Ævar Arnfjörð Bjarmason
2022-05-26 23:10       ` Junio C Hamano
2022-05-26 18:08 ` tb/cruft-packs (was Re: What's cooking in git.git (May 2022, #07; Wed, 25)) Taylor Blau
2022-05-26 18:10 ` What's cooking in git.git (May 2022, #07; Wed, 25) Taylor Blau
2022-05-26 20:17   ` Junio C Hamano
2022-05-26 18:11 ` tb/midx-race-in-pack-objects (was Re: What's cooking in git.git (May 2022, #07; Wed, 25)) Taylor Blau
2022-05-26 18:49 ` sg/build-gitweb (was: " Ævar Arnfjörð Bjarmason
2022-05-26 19:06   ` sg/build-gitweb Junio C Hamano
2022-05-26 18:51 ` jc/http-clear-finished-pointer (was: What's cooking in git.git (May 2022, #07; Wed, 25)) Ævar Arnfjörð Bjarmason
2022-05-26 19:37   ` Junio C Hamano [this message]
2022-05-27 20:41     ` Re* jc/http-clear-finished-pointer Johannes Schindelin
2022-05-27 21:35       ` Junio C Hamano
2022-06-01  7:26     ` Ævar Arnfjörð Bjarmason
2022-06-01 15:48       ` Junio C Hamano
2022-05-26 18:54 ` js/bisect-in-c (was: What's cooking in git.git (May 2022, #07; Wed, 25)) Ævar Arnfjörð Bjarmason
2022-05-26 19:38   ` js/bisect-in-c Junio C Hamano
2022-05-26 20:00     ` js/bisect-in-c Junio C Hamano
2022-05-27 12:52       ` js/bisect-in-c Ævar Arnfjörð Bjarmason
2022-05-26 18:57 ` jx/l10n-workflow-change (was: What's cooking in git.git (May 2022, #07; Wed, 25)) Ævar Arnfjörð Bjarmason

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:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

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

  git send-email \
    --in-reply-to=xmqq7d68ytj8.fsf_-_@gitster.g \
    --to=gitster@pobox.com \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=johannes.schindelin@gmx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* 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.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.