From: Jeff Hostetler <git@jeffhostetler.com>
To: Jeff King <peff@peff.net>,
Johannes Schindelin <johannes.schindelin@gmx.de>
Cc: git@vger.kernel.org, Jeff Hostetler <jeffhost@microsoft.com>,
Junio C Hamano <gitster@pobox.com>
Subject: Re: [PATCH] read-cache: close index.lock in do_write_index
Date: Thu, 27 Apr 2017 12:51:56 -0400 [thread overview]
Message-ID: <c7c63b48-cb11-35f4-f41f-52286d66bef2@jeffhostetler.com> (raw)
In-Reply-To: <20170427031311.hfbit6glkx2tw3ru@sigill.intra.peff.net>
On 4/26/2017 11:13 PM, Jeff King wrote:
> On Wed, Apr 26, 2017 at 10:05:23PM +0200, Johannes Schindelin wrote:
>
>> From: Jeff Hostetler <jeffhost@microsoft.com>
>>
>> Teach do_write_index() to close the index.lock file
>> before getting the mtime and updating the istate.timestamp
>> fields.
>>
>> On Windows, a file's mtime is not updated until the file is
>> closed. On Linux, the mtime is set after the last flush.
>
> I wondered at first what this would mean for atomicity. The original
> code does an fstat, so we're sure to get the timestamp of what we just
> wrote.
>
> I think we should be OK after your change, though. We're stat()ing the
> lockfile itself, so nobody else should be touching it (because they'd be
> violating the lock to do so).
>
>> -static int do_write_index(struct index_state *istate, int newfd,
>> +static int do_write_index(struct index_state *istate, struct tempfile *tempfile,
>> int strip_extensions)
>> [...]
>> - if (ce_flush(&c, newfd, istate->sha1) || fstat(newfd, &st))
>> + if (ce_flush(&c, newfd, istate->sha1))
>> + return -1;
>> + if (close_tempfile(tempfile))
>> + return error(_("could not close '%s'"), tempfile->filename.buf);
>> + if (lstat(tempfile->filename.buf, &st))
>> return -1;
>
> So now we unconditionally close in do_write_index(), but I don't see any
> close_tempfile() calls going away. For the call in write_shared_index(),
> that's because we either call delete_tempfile() or rename_tempfile(),
> either of which would close as needed, but can handle an already-closed
> file.
>
> The other caller is do_write_locked_index(), which accepts either a
> flag: either COMMIT_LOCK, CLOSE_LOCK, or neither. COMMIT_LOCK is OK; it
> can handle the already-closed file. CLOSE_LOCK is obviously fine. It
> just becomes a noop. But when neither flag is set, now we close the
> lock. Are there any callers that will be affected?
>
> There are three callers, but I think they all eventually trace up to
> write_locked_index(). And grepping for callers of that function, it
> looks like each one uses either COMMIT_LOCK or CLOSE_LOCK.
>
Yes, we only took a casual look at the calling environment(s)
and didn't try to do a full reduction/refactoring. In the
absence of any other red-flags, I'll look at doing this.
Thanks!
> So perhaps we'd want to squash in (or perhaps do as a preparatory
> patch) something like:
>
> diff --git a/read-cache.c b/read-cache.c
> index b0276fd55..db7a812af 100644
> --- a/read-cache.c
> +++ b/read-cache.c
> @@ -2193,14 +2193,16 @@ static int do_write_locked_index(struct index_state *istate, struct lock_file *l
> int ret = do_write_index(istate, &lock->tempfile, 0);
> if (ret)
> return ret;
> +
> + /* Callers must specify exactly one of COMMIT/CLOSE */
> assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) !=
> (COMMIT_LOCK | CLOSE_LOCK));
> + assert((flags & (COMMIT_LOCK | CLOSE_LOCK)) != 0);
> +
> if (flags & COMMIT_LOCK)
> return commit_locked_index(lock);
> - else if (flags & CLOSE_LOCK)
> - return close_lock_file(lock);
> else
> - return ret;
> + return close_lock_file(lock);
> }
>
> static int write_split_index(struct index_state *istate,
>
> We could also get rid of CLOSE_LOCK entirely at this point. Or since
> these are the only two flags, just turn the flags field into a boolean
> "int commit_lock". But doing it as above is perhaps more readable
> (callers say CLOSE_LOCK instead of an unannotated "0"), and the extra
> assert will catch any topics in flight that add calls using "0" for
> flags.
>
> -Peff
>
next prev parent reply other threads:[~2017-04-27 16:52 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-04-26 20:05 [PATCH] read-cache: close index.lock in do_write_index Johannes Schindelin
2017-04-27 3:13 ` Jeff King
2017-04-27 16:51 ` Jeff Hostetler [this message]
2017-04-27 3:21 ` Junio C Hamano
2017-04-27 16:45 ` Jeff Hostetler
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=c7c63b48-cb11-35f4-f41f-52286d66bef2@jeffhostetler.com \
--to=git@jeffhostetler.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=jeffhost@microsoft.com \
--cc=johannes.schindelin@gmx.de \
--cc=peff@peff.net \
/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;
as well as URLs for NNTP newsgroup(s).