git/builtin
Patrick Steinhardt 58d4d7f1c5 fetch: fix deadlock when cleaning up lockfiles in async signals
When fetching packfiles, we write a bunch of lockfiles for the packfiles
we're writing into the repository. In order to not leave behind any
cruft in case we exit or receive a signal, we register both an exit
handler as well as signal handlers for common signals like SIGINT. These
handlers will then unlink the locks and free the data structure tracking
them. We have observed a deadlock in this logic though:

    (gdb) bt
    #0  __lll_lock_wait_private () at ../sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:95
    #1  0x00007f4932bea2cd in _int_free (av=0x7f4932f2eb20 <main_arena>, p=0x3e3e4200, have_lock=0) at malloc.c:3969
    #2  0x00007f4932bee58c in __GI___libc_free (mem=<optimized out>) at malloc.c:2975
    #3  0x0000000000662ab1 in string_list_clear ()
    #4  0x000000000044f5bc in unlock_pack_on_signal ()
    #5  <signal handler called>
    #6  _int_free (av=0x7f4932f2eb20 <main_arena>, p=<optimized out>, have_lock=0) at malloc.c:4024
    #7  0x00007f4932bee58c in __GI___libc_free (mem=<optimized out>) at malloc.c:2975
    #8  0x000000000065afd5 in strbuf_release ()
    #9  0x000000000066ddb9 in delete_tempfile ()
    #10 0x0000000000610d0b in files_transaction_cleanup.isra ()
    #11 0x0000000000611718 in files_transaction_abort ()
    #12 0x000000000060d2ef in ref_transaction_abort ()
    #13 0x000000000060d441 in ref_transaction_prepare ()
    #14 0x000000000060e0b5 in ref_transaction_commit ()
    #15 0x00000000004511c2 in fetch_and_consume_refs ()
    #16 0x000000000045279a in cmd_fetch ()
    #17 0x0000000000407c48 in handle_builtin ()
    #18 0x0000000000408df2 in cmd_main ()
    #19 0x00000000004078b5 in main ()

The process was killed with a signal, which caused the signal handler to
kick in and try free the data structures after we have unlinked the
locks. It then deadlocks while calling free(3P).

The root cause of this is that it is not allowed to call certain
functions in async-signal handlers, as specified by signal-safety(7).
Next to most I/O functions, this list of disallowed functions also
includes memory-handling functions like malloc(3P) and free(3P) because
they may not be reentrant. As a result, if we execute such functions in
the signal handler, then they may operate on inconistent state and fail
in unexpected ways.

Fix this bug by not calling non-async-signal-safe functions when running
in the signal handler. We're about to re-raise the signal anyway and
will thus exit, so it's not much of a problem to keep the string list of
lockfiles untouched. Note that it's fine though to call unlink(2), so
we'll still clean up the lockfiles correctly.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Reviewed-by: brian m. carlson <sandals@crustytoothpaste.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2022-01-07 13:49:19 -08:00
..
add.c Merge branch 'ds/add-rm-with-sparse-index' 2021-10-13 15:15:56 -07:00
am.c Merge branch 'ab/config-based-hooks-1' 2021-10-13 15:15:57 -07:00
annotate.c strvec: rename struct fields 2020-07-30 19:18:06 -07:00
apply.c
archive.c use xopen() to handle fatal open(2) failures 2021-08-25 14:39:08 -07:00
bisect--helper.c bisect--helper: add space between colon and following sentence 2021-10-01 15:47:53 -07:00
blame.c Merge branch 'ab/parse-options-cleanup' 2021-10-25 16:06:59 -07:00
branch.c branch: use ref_sorting_release() 2021-10-20 11:36:13 -07:00
bugreport.c hook-list.h: add a generated list of hooks, like config-list.h 2021-09-27 09:44:54 -07:00
bundle.c Merge branch 'ab/bundle-remove-verbose-option' 2021-10-03 21:49:20 -07:00
cat-file.c cat-file: use packed_object_info() for --batch-all-objects 2021-10-08 15:45:14 -07:00
check-attr.c cache.h: flip NO_THE_INDEX_COMPATIBILITY_MACROS switch 2019-01-24 11:55:06 -08:00
check-ignore.c dir.[ch]: replace dir_init() with DIR_INIT 2021-07-01 12:32:22 -07:00
check-mailmap.c shortlog: remove unused(?) "repo-abbrev" feature 2021-01-12 14:04:42 -08:00
check-ref-format.c
checkout--worker.c pkt-line.[ch]: remove unused packet_read_line_buf() 2021-10-15 13:09:40 -07:00
checkout-index.c Merge branch 'mt/parallel-checkout-part-3' 2021-05-16 21:05:23 +09:00
checkout.c Change unpack_trees' 'reset' flag into an enum 2021-09-27 13:38:37 -07:00
clean.c dir.[ch]: replace dir_init() with DIR_INIT 2021-07-01 12:32:22 -07:00
clone.c fetch: fix deadlock when cleaning up lockfiles in async signals 2022-01-07 13:49:19 -08:00
column.c column: fix parsing of the '--nl' option 2021-08-26 14:36:27 -07:00
commit-graph.c Merge branch 'ab/ignore-replace-while-working-on-commit-graph' 2021-11-01 13:48:08 -07:00
commit-tree.c use xopen() to handle fatal open(2) failures 2021-08-25 14:39:08 -07:00
commit.c hook.c users: use "hook_exists()" instead of "find_hook()" 2021-09-27 09:44:54 -07:00
config.c urlmatch.[ch]: add and use URLMATCH_CONFIG_INIT 2021-10-01 14:22:51 -07:00
count-objects.c rename "alternate_object_database" to "object_directory" 2018-11-13 14:22:02 +09:00
credential-cache--daemon.c unix-socket: add backlog size option to unix_stream_listen() 2021-03-15 14:32:51 -07:00
credential-cache.c credential-cache: check for windows specific errors 2021-09-14 09:30:54 -07:00
credential-store.c Use a better name for the function interpolating paths 2021-07-26 12:17:16 -07:00
credential.c credential: load default config 2020-10-16 12:30:45 -07:00
describe.c hash: provide per-algorithm null OIDs 2021-04-27 16:31:39 +09:00
diff-files.c Merge branch 'jc/diffcore-rotate' 2021-02-25 16:43:30 -08:00
diff-index.c diff-index: restore -c/--cc options handling 2021-09-07 11:11:35 -07:00
diff-tree.c Merge branch 'jc/diffcore-rotate' 2021-02-25 16:43:30 -08:00
diff.c Merge branch 'dl/diff-merge-base' 2021-07-28 13:17:59 -07:00
difftool.c Merge branch 'da/difftool-dir-diff-symlink-fix' into maint 2021-10-12 13:51:48 -07:00
env--helper.c assert PARSE_OPT_NONEG in parse-options callbacks 2020-09-30 12:53:47 -07:00
fast-export.c Merge branch 'ab/fsck-unexpected-type' 2021-10-25 16:06:56 -07:00
fast-import.c Use the final_oid_fn to finalize hashing of object IDs 2021-04-27 16:31:38 +09:00
fetch-pack.c connect, transport: encapsulate arg in struct 2021-02-05 13:49:54 -08:00
fetch.c fetch: fix deadlock when cleaning up lockfiles in async signals 2022-01-07 13:49:19 -08:00
fmt-merge-msg.c Lib-ify fmt-merge-msg 2020-03-24 15:04:43 -07:00
for-each-ref.c ref-filter API user: add and use a ref_sorting_release() 2021-10-20 11:36:13 -07:00
for-each-repo.c builtin/for-each-repo: remove unnecessary argv copy to plug leak 2021-07-26 12:19:20 -07:00
fsck.c Merge branch 'ab/fsck-unexpected-type' 2021-11-12 15:29:25 -08:00
gc.c Merge branch 'ds/no-usable-cron-on-macos' 2021-11-10 15:01:20 -08:00
get-tar-commit-id.c builtin/get-tar-commit-id: make hash size independent 2019-04-01 11:57:39 +09:00
grep.c grep: fix a "path_list" memory leak 2021-10-23 10:45:25 -07:00
hash-object.c Merge branch 'jc/prefix-filename-allocates' into maint 2021-10-12 13:51:32 -07:00
help.c Merge branch 'ab/help-config-vars' 2021-10-13 15:15:58 -07:00
index-pack.c i18n: fix typos found during l10n for git 2.34.0 2021-10-31 22:49:49 -07:00
init-db.c Merge branch 'mt/init-template-userpath-fix' 2021-05-25 16:21:20 +09:00
interpret-trailers.c Use OPT_CALLBACK and OPT_CALLBACK_F 2020-04-28 10:47:10 -07:00
log.c revision: separate walk and unsorted flags 2021-08-05 09:37:28 -07:00
ls-files.c ls-files: add missing string_list_clear() 2021-10-07 15:40:15 -07:00
ls-remote.c parse-options API users: align usage output in C-strings 2021-09-12 18:57:30 -07:00
ls-tree.c tree.h API: simplify read_tree_recursive() signature 2021-03-20 16:09:26 -07:00
mailinfo.c mailinfo: allow squelching quoted CRLF warning 2021-05-10 15:06:22 +09:00
mailsplit.c use xopen() to handle fatal open(2) failures 2021-08-25 14:39:08 -07:00
merge-base.c rebase: --fork-point regression fix 2020-02-11 09:59:39 -08:00
merge-file.c assert NOARG/NONEG behavior of parse-options callbacks 2018-11-06 12:56:29 +09:00
merge-index.c merge-index: ensure full index 2021-04-14 13:47:21 -07:00
merge-ours.c builtins + test helpers: use return instead of exit() in cmd_* 2021-06-09 09:15:58 +09:00
merge-recursive.c Ensure index matches head before invoking merge machinery, round N 2019-08-19 10:08:03 -07:00
merge-tree.c xdiff users: use designated initializers for out_line 2021-05-11 12:47:31 +09:00
merge.c Merge branch 'ab/mark-leak-free-tests-more' 2021-10-25 16:06:59 -07:00
mktag.c fsck: report invalid object type-path combinations 2021-10-01 15:06:01 -07:00
mktree.c builtins + test helpers: use return instead of exit() in cmd_* 2021-06-09 09:15:58 +09:00
multi-pack-index.c Merge branch 'tb/repack-write-midx' 2021-10-18 15:47:57 -07:00
mv.c mv: refuse to move sparse paths 2021-09-28 10:31:02 -07:00
name-rev.c oid_pos(): access table through const pointers 2021-01-28 12:03:26 -08:00
notes.c use xopen() to handle fatal open(2) failures 2021-08-25 14:39:08 -07:00
pack-objects.c Merge branch 'tb/pack-finalize-ordering' into maint 2021-10-12 13:51:46 -07:00
pack-redundant.c builtin/pack-redundant: avoid casting buffers to struct object_id 2021-04-27 16:31:38 +09:00
pack-refs.c Honor core.precomposeUnicode in more places 2019-04-26 10:54:03 +09:00
patch-id.c patch-id: use oid_to_hex() to print multiple object IDs 2019-12-09 12:26:40 -08:00
prune-packed.c Lib-ify prune-packed 2020-03-24 15:04:44 -07:00
prune.c repack, prune: drop GIT_REF_PARANOIA settings 2021-09-27 12:36:45 -07:00
pull.c Merge branch 'ev/pull-already-up-to-date-is-noop' into maint 2021-11-23 14:48:04 -08:00
push.c advice: remove read uses of most global advice_ variables 2021-08-25 12:07:52 -07:00
range-diff.c column, range-diff: downcase option description 2021-03-29 14:06:08 -07:00
read-tree.c Change unpack_trees' 'reset' flag into an enum 2021-09-27 13:38:37 -07:00
rebase.c Merge branch 'js/retire-preserve-merges' 2021-10-18 15:47:56 -07:00
receive-pack.c Merge branch 'fs/ssh-signing' 2021-10-25 16:06:58 -07:00
reflog.c reflog: free() ref given to us by dwim_log() 2021-10-23 10:45:25 -07:00
remote-ext.c strvec: convert builtin/ callers away from argv_array name 2020-07-28 15:02:18 -07:00
remote-fd.c
remote.c Merge branch 'ab/designated-initializers-more' 2021-10-18 15:47:57 -07:00
repack.c Merge branch 'tb/repack-write-midx' 2021-10-18 15:47:57 -07:00
replace.c advice: move advice.graftFileDeprecated squashing to commit.[ch] 2021-08-25 12:07:52 -07:00
rerere.c xdiff users: use designated initializers for out_line 2021-05-11 12:47:31 +09:00
reset.c Change unpack_trees' 'reset' flag into an enum 2021-09-27 13:38:37 -07:00
rev-list.c rev-list: add option for --pretty=format without header 2021-07-12 10:12:31 -07:00
rev-parse.c refs: drop "broken" flag from for_each_fullref_in() 2021-09-27 12:36:45 -07:00
revert.c Merge branch 'ds/mergies-with-sparse-index' 2021-09-20 15:20:45 -07:00
rm.c Merge branch 'ds/add-rm-with-sparse-index' 2021-10-13 15:15:56 -07:00
send-pack.c Merge branch 'jk/http-push-status-fix' 2021-10-29 15:43:12 -07:00
shortlog.c parse-options.[ch]: consistently use "enum parse_opt_result" 2021-10-08 14:13:11 -07:00
show-branch.c parse-options API users: align usage output in C-strings 2021-09-12 18:57:30 -07:00
show-index.c builtin/show-index: set the algorithm for object IDs 2021-04-27 16:31:39 +09:00
show-ref.c refs: switch peel_ref() to peel_iterated_oid() 2021-01-21 15:51:31 -08:00
sparse-checkout.c sparse-checkout: clear tracked sparse dirs 2021-09-07 22:41:10 -07:00
stash.c Merge branch 'ab/align-parse-options-help' 2021-10-13 15:15:58 -07:00
stripspace.c stripspace: allow -s/-c outside git repository 2018-12-26 15:41:47 -08:00
submodule--helper.c Merge branch 'ks/submodule-add-message-fix' 2021-10-29 15:43:14 -07:00
symbolic-ref.c symbolic-ref: don't leak shortened refname in check_symref() 2021-03-14 15:57:59 -07:00
tag.c ref-filter API user: add and use a ref_sorting_release() 2021-10-20 11:36:13 -07:00
unpack-file.c
unpack-objects.c hash: provide per-algorithm null OIDs 2021-04-27 16:31:39 +09:00
update-index.c use xopen() to handle fatal open(2) failures 2021-08-25 14:39:08 -07:00
update-ref.c update-ref: fix streaming of status updates 2021-09-03 11:35:15 -07:00
update-server-info.c
upload-archive.c strvec: rename struct fields 2020-07-30 19:18:06 -07:00
upload-pack.c upload-pack: document and rename --advertise-refs 2021-08-05 08:59:37 -07:00
var.c
verify-commit.c Merge branch 'jk/no-system-includes-in-dot-c' 2019-07-31 14:38:56 -07:00
verify-pack.c Merge branch 'bc/sha-256-part-3' 2020-08-11 18:04:11 -07:00
verify-tag.c verify-tag: drop signal.h include 2019-06-19 08:19:21 -07:00
worktree.c hook.[ch]: move find_hook() from run-command.c to hook.c 2021-09-27 09:44:54 -07:00
write-tree.c cmd_{read,write}_tree: rename "unused" variable that is used 2019-05-13 14:22:53 +09:00