From: Toon Claes <toon@iotcl.com>
To: Junio C Hamano <gitster@pobox.com>, Patrick Steinhardt <ps@pks.im>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 3/6] packfile: inline `open_packed_git_1()`
Date: Thu, 26 Nov 2026 15:16:33 +0100 [thread overview]
Message-ID: <87a4mvsmny.fsf@dev.null.iotcl.com.invalid> (raw)
In-Reply-To: <xmqqwlq1lwdp.fsf@gitster.g>
Junio C Hamano <gitster@pobox.com> writes:
> Patrick Steinhardt <ps@pks.im> writes:
>
>> The function `open_packed_git()` is responsible for opening a packfile.
>> The majority of work is not performed by it though, but instead by its
>> `_1()` sibling. But this sibling really only exists because we thought
>> it too hard to handle errors properly and close the packfile's file
>> descriptor on an error path. So `_1()` doesn't handle those, and instead
>> defers to its only caller to close the file descriptor in the error path
>> again.
>>
>> Fix this design smell by introducing a common exit path that knows to
>> close the file descriptor on error. This allows us to inline the call to
>> `open_packed_git_1()`.
>
> I agree that this makes the descriptor ownership much easier to follow.
> It is not just the call but the entire function got inlined, but I
> shouldn't split hairs ;-)
>
>> @@ -510,55 +509,81 @@ static int open_packed_git_1(struct packed_git *p)
>> ; /* nothing */
>>
>> p->pack_fd = git_open(p->pack_name);
>> - if (p->pack_fd < 0 || fstat(p->pack_fd, &st))
>> - return -1;
>> + if (p->pack_fd < 0 || fstat(p->pack_fd, &st)) {
>> + ret = -1;
>> + goto out;
>> + }
>> pack_open_fds++;
>
> There may be a mismatch in pack_open_fds accounting. If git_open()
> succeeds (so p->pack_fd >= 0) and fstat() fails, we jump to 'out:'
> before incrementing pack_open_fds.
Good find.
>> +out:
>> + if (ret < 0)
>> + packfile_close_fd(p);
>> + return ret;
>> }
>
> close_pack_fd() (now called packfile_close_fd()) closes p->pack_fd
> whenever p->pack_fd >= 0 and decrements pack_open_fds, which may
> underflow to UINT_MAX if the ftat() failed because of this?
>
> May not be a new bug (we used to leave the p->pack_fd open without
> nobody closing instead, so we may not have underflown but leaked file
> descriptor instead),
Yes, as far as I can tell this is not a new bug.
> but since you are touching around the area, we may
> want to fix it as well.
Makes sense to me to split out fstat() into a separate if{}.
--
Laters,
Toon
next prev parent reply other threads:[~2026-11-26 14:16 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 12:02 [PATCH 0/6] packfile: some lifecycle improvements Patrick Steinhardt
2026-11-20 12:02 ` [PATCH 1/6] builtin/fast-import: stop reporting packfile statistics Patrick Steinhardt
2026-11-24 20:43 ` Junio C Hamano
2026-11-24 22:19 ` brian m. carlson
2026-11-26 14:15 ` Toon Claes
2026-11-20 12:02 ` [PATCH 2/6] packfile: rename lifecycle functions Patrick Steinhardt
2026-11-24 23:47 ` Junio C Hamano
2026-11-26 14:16 ` Toon Claes
2026-11-20 12:02 ` [PATCH 3/6] packfile: inline `open_packed_git_1()` Patrick Steinhardt
2026-11-24 22:04 ` Junio C Hamano
2026-11-26 14:16 ` Toon Claes [this message]
2026-11-26 14:16 ` Toon Claes
2026-11-26 20:39 ` Junio C Hamano
2026-11-20 12:02 ` [PATCH 4/6] packfile: introduce function to free a packfile Patrick Steinhardt
2026-11-24 21:34 ` Junio C Hamano
2026-11-26 14:13 ` Toon Claes
2026-11-26 20:41 ` Junio C Hamano
2026-11-20 12:02 ` [PATCH 5/6] packfile: maintain process-wide limits globally Patrick Steinhardt
2026-11-24 23:24 ` Junio C Hamano
2026-11-26 15:11 ` Toon Claes
2026-11-20 12:02 ` [PATCH 6/6] packfile: skip closing packs marked as do-not-close Patrick Steinhardt
2026-11-24 21:14 ` Junio C Hamano
2026-11-26 15:26 ` Toon Claes
2026-11-26 19:56 ` Junio C Hamano
2026-11-26 15:29 ` [PATCH 0/6] packfile: some lifecycle improvements Toon Claes
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=87a4mvsmny.fsf@dev.null.iotcl.com.invalid \
--to=toon@iotcl.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=ps@pks.im \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox