git/list[1] front-page[2] threads[3] people[4] search[5] about
 

RFC - concurrency causes segfault in git grep since 2.26.0

From
Phil Hord <phil.hord@gmail.com>
Date
Sep 25, 2020, 02:36 UTC
Message-ID
<CABURp0r_VPNeU5ugbJDQo=hV_aOit7W-pf9nhDF9Og=vVJG8tw@mail.gmail.com>
Hi list.  It's been a while.

I can reliably reproduce a segfault in git grep when searching the object DB. It is caused by a race when threads > 2.

I have a patch that fixes the problem, but I don't know exactly why. Can someone else explain it and/or offer a better solution?

====
diff --git packfile.c packfile.c
index e69012e7f2..52b7b54aeb 100644
--- packfile.c
+++ packfile.c
@@ -1812,7 +1812,9 @@ void *unpack_entry(struct repository *r, struct
packed_git *p, off_t obj_offset,
                if (!base)
                        continue;

+               obj_read_lock();
                delta_data = unpack_compressed_entry(p, &w_curs,
curpos, delta_size);
+               obj_read_unlock();

                if (!delta_data) {
                        error("failed to unpack compressed delta "

====

My notes thus far:

I have seen it fail with `--threads 3`, but it fails very reliably for
me with `--threads 20`.

My repo is large (7GB) which gives grep enough to do. I'm not able to
make it fail on git.git; it seems like it's too small to run into this
problem.

The problem bisects to 1d1729caeb.  It was somewhat hard to find this,
though, because git grep disables --threads when reading from the
object store until another patch later in the same series.

====

$ git bisect run sh -c \
    "sed -i -e 's/list.nr || cached || show_in_pager/list.nr ||
show_in_pager/' builtin/grep.c || exit 129;
    make -j 30 || exit 125;
    git checkout HEAD -- builtin/grep.c ;
    ./bin-wrappers/git -C ~/git/purity grep --threads 20 --cached
'target-regex' || exit 1"

1d1729caebd41b340dd8dd61057f613da4df526c is the first bad commit
commit 1d1729caebd41b340dd8dd61057f613da4df526c
Author: Matheus Tavares <matheus.bernardino@usp.br>
Date:   Wed Jan 15 23:39:54 2020 -0300

    grep: replace grep_read_mutex by internal obj read lock

Here's what I'm testing with:
$ ./bin-wrappers/git --version
git version 2.28.0.585.ge1cfff6765

I have found that the crash always happens in cache_or_unpack_entry.
The thread that segfaults is always in patch_delta, and there are
always at least two threads inside unpack_compressed_entry at the time
of the failure.

I was able to get it to crash with just three threads one time.  I
copied a log with the stack trace of all (4) threads during that run
to a gist [1].

Another log showing four different segfaults when run with 40 threads
is also in that gist [1].

[1] https://gist.github.com/phord/02e84d003688baa493b978110225d443
Next: Matheus Tavares
Message 1 of 12 in “RFC - concurrency causes segfault in git grep since 2.26.0”
  1. Phil HordSep 25, 2020
  2. Matheus TavaresSep 25, 2020
  3. Phil HordSep 25, 2020
  4. 0/2 Fix race condition and memory leak in delta base cacheMatheus Tavares, Sep 28, 2020
  5. 1/2 packfile: fix race condition on unpack_entry()Matheus Tavares, Sep 28, 2020
  6. Junio C HamanoSep 28, 2020
  7. 2/2 packfile: fix memory leak in add_delta_base_cache()Matheus Tavares, Sep 28, 2020
  8. Junio C HamanoSep 28, 2020
  9. 0/2 Fix race condition and memory leak in delta base cacheMatheus Tavares, Sep 29, 2020
  10. 1/2 packfile: fix race condition on unpack_entry()Matheus Tavares, Sep 29, 2020
  11. Phil HordOct 2, 2020
  12. 2/2 packfile: fix memory leak in add_delta_base_cache()Matheus Tavares, Sep 29, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.