muntrace: reset file and hooks before finalizing the stream fclose will call free, invoking its hook, then fprintf which would indirectly try to allocate a buffer, and this can cause malloc to be used (thus its hook to be invoked) if libio uses malloc instead of mmap; given any malloc/free hook locks the internal lock, this leads to a deadlock. To prevent this hook roundtrip at muntrace, first unset MALLSTREAM and the hooks, and only after that close the trace file. 2012-11-17 Pino Toscano * malloc/mtrace.c (muntrace): Reset MALLSTREAM and the hooks before finalizing MALLSTREAM. --- a/malloc/mtrace.c +++ b/malloc/mtrace.c @@ -361,14 +361,18 @@ mtrace () void muntrace () { + FILE *f; + if (mallstream == NULL) return; - fprintf (mallstream, "= End\n"); - fclose (mallstream); + f = mallstream; mallstream = NULL; __free_hook = tr_old_free_hook; __malloc_hook = tr_old_malloc_hook; __realloc_hook = tr_old_realloc_hook; __memalign_hook = tr_old_memalign_hook; + + fprintf (f, "= End\n"); + fclose (f); }