diff options
author | Jeff King <peff@peff.net> | 2017-09-05 08:15:04 -0400 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2017-09-06 17:19:54 +0900 |
commit | 422a21c6a086ec8c05c96b04bdccd960da141c04 (patch) | |
tree | fb616abdbb0e5af7b34ee5ac7b080a8a03257d87 /tempfile.c | |
parent | tempfile: use list.h for linked list (diff) | |
download | tgif-422a21c6a086ec8c05c96b04bdccd960da141c04.tar.xz |
tempfile: remove deactivated list entries
Once a "struct tempfile" is added to the global cleanup
list, it is never removed. This means that its storage must
remain valid for the lifetime of the program. For single-use
tempfiles and locks, this isn't a big deal: we just declare
the struct static. But for library code which may take
multiple simultaneous locks (like the ref code), they're
forced to allocate a struct on the heap and leak it.
This is mostly OK in practice. The size of the leak is
bounded by the number of refs, and most programs exit after
operating on a fixed number of refs (and allocate
simultaneous memory proportional to the number of ref
updates in the first place). But:
1. It isn't hard to imagine a real leak: a program which
runs for a long time taking a series of ref update
instructions and fulfilling them one by one. I don't
think we have such a program now, but it's certainly
plausible.
2. The leaked entries appear as false positives to
tools like valgrind.
Let's relax this rule by keeping only "active" tempfiles on
the list. We can do this easily by moving the list-add
operation from prepare_tempfile_object to activate_tempfile,
and adding a deletion in deactivate_tempfile.
Existing callers do not need to be updated immediately.
They'll continue to leak any tempfile objects they may have
allocated, but that's no different than the status quo. We
can clean them up individually.
Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'tempfile.c')
-rw-r--r-- | tempfile.c | 46 |
1 files changed, 20 insertions, 26 deletions
diff --git a/tempfile.c b/tempfile.c index 11bda824cf..f82a5f3676 100644 --- a/tempfile.c +++ b/tempfile.c @@ -42,8 +42,7 @@ * states in which this condition doesn't hold). Client code should * *not* rely on the filename being empty in this state. * - `fd` is -1 and `fp` is `NULL` - * - the object is left registered in the `tempfile_list`, and - * `on_list` is set. + * - the object is removed from `tempfile_list` (but could be used again) * * A temporary file is owned by the process that created it. The * `tempfile` has an `owner` field that records the owner's PID. This @@ -92,36 +91,30 @@ static void remove_tempfiles_on_signal(int signo) raise(signo); } -/* - * Initialize *tempfile if necessary and add it to tempfile_list. - */ static void prepare_tempfile_object(struct tempfile *tempfile) { - if (volatile_list_empty(&tempfile_list)) { - /* One-time initialization */ - sigchain_push_common(remove_tempfiles_on_signal); - atexit(remove_tempfiles_on_exit); - } - - if (is_tempfile_active(tempfile)) - BUG("prepare_tempfile_object called for active object"); - if (!tempfile->on_list) { - /* Initialize *tempfile and add it to tempfile_list: */ - tempfile->fd = -1; - tempfile->fp = NULL; - tempfile->active = 0; - tempfile->owner = 0; - strbuf_init(&tempfile->filename, 0); - volatile_list_add(&tempfile->list, &tempfile_list); - tempfile->on_list = 1; - } else if (tempfile->filename.len) { - /* This shouldn't happen, but better safe than sorry. */ - BUG("prepare_tempfile_object called for improperly-reset object"); - } + tempfile->fd = -1; + tempfile->fp = NULL; + tempfile->active = 0; + tempfile->owner = 0; + INIT_LIST_HEAD(&tempfile->list); + strbuf_init(&tempfile->filename, 0); } static void activate_tempfile(struct tempfile *tempfile) { + static int initialized; + + if (is_tempfile_active(tempfile)) + BUG("activate_tempfile called for active object"); + + if (!initialized) { + sigchain_push_common(remove_tempfiles_on_signal); + atexit(remove_tempfiles_on_exit); + initialized = 1; + } + + volatile_list_add(&tempfile->list, &tempfile_list); tempfile->owner = getpid(); tempfile->active = 1; } @@ -130,6 +123,7 @@ static void deactivate_tempfile(struct tempfile *tempfile) { tempfile->active = 0; strbuf_release(&tempfile->filename); + volatile_list_del(&tempfile->list); } /* Make sure errno contains a meaningful value on error */ |