grep: take the read-lock when adding a submodule

With --recurse-submodules, we add each submodule that we encounter to the list of alternate object databases. With threading, our changes to the list are not protected against races. Indeed, ThreadSanitizer reports a race when we call `add_to_alternates_memory()` around the same time that another thread is reading in the list through `read_sha1_file()`. Take the grep read-lock while adding the submodule. The lock is used to serialize uses of non-thread-safe parts of Git's API, including `read_sha1_file()`. Helped-by: Brandon Williams <bmwill@google.com> Signed-off-by: Martin Ågren <martin.agren@gmail.com> Acked-by: Brandon Williams <bmwill@google.com> Reviewed-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Martin Ågren committed Nov 1, 2017 at 21:45 UTC 9560e6245a1a0b7483a57d7018e5b7a852fdba62
1 file changed +2
builtin/grep.c
+2
@@ -431,7 +431,9 @@ static int grep_submodule(struct grep_opt *opt, struct repository *superproject,
431 * store is no longer global and instead is a member of the repository
432 * object.
433 */
434 + grep_read_lock();
435 add_to_alternates_memory(submodule.objectdir);
436 + grep_read_unlock();
437
438 if (oid) {
439 struct object *object;