Some users reported issues of repeated messages:
fatal: recursion detected in die handler
This wasn't happening every time, but we eventually captured a
GIT_TRACE2_PERF log file with this issue and revealed an interesting
internal detail, failing with this message:
unable to format message: %4d-%02d-%02dT%02d:%02d:%02d.%06ldZ
This specific format string tracks to tr2_tbuf_utc_datetime_extended()
in trace2/tr2_tbuf.c. This logic began as tr2_tbuf_utc_time() in
ee4512ed48 (trace2: create new combined trace facility, 2019-02-22) but
was later split in bad229aef2 (trace2: clarify UTC datetime formatting,
2019-04-15).
This use of xsnprintf() is writing a very specific datetime format into a
32-character buffer. The format requires that the input data will not
overflow the format digits or the buffer will not hold the result. Since
we are using xsnprintf() here, those failures turn into die() events.
This method and its siblings, tr2_tbuf_local_time() and
tr2_tbuf_utc_datetime(), are used in the tracing library. The extended
form is used only for the 'event' format, which these users were using
via a config setting for use in client-side telemetry. The non-extended
form is used to help generate the 'SID' that defines the process in the
traces.
Not only are these inappropriate times for a failure, but the extended
method is called specifially during the 'atexit' event, which was
triggering this problem in a loop as the 'atexit' event would be
retriggered by the die().
Based on other symptoms impacting users on the version reporting these
failures, it is most likely that this is actually a failure to allocate
memory, which is a specific symptom in Git for Windows. That fork uses a
different library for its implementation of vsprintf() which allocates
an array when seven or more positional arguments exist in the formatting
string, such as this one.
Ultimately, the trace2 machinery is so low-level that it should not rely on
any helper functions that perform error handling with die(), as that can
trigger issues that would then be traced, causing this kind of recursive
loop.
These changes help remove any use of die() within this file:
1. Both 'tv' and 'tm' structs are initialized with zero values, allowing
an erroring gettimeofday() or gmtime_r() method to leave them
zero-valued. A zero-valued date is better than a die() here.
2. Replace the use of xsnprintf() with snprintf() to avoid the
possibility of calling die() here. Instead, check the response to see
if there was a failure. On failure, put a blank value into the buffer
instead of possibly allowing a value that would not format correctly
for a trace2 consumer. This value should be seen as obviously wrong
and therefore signals a problem.
As the core issue in this code seems to require a system method
returning an error, no test accompanies this change.
This change removes all uses of xsnprintf() from the trace2/ directory.
There are two uses of xstrdup() that could be considered for removal,
but they only die() on out-of-memory errors instead of formatting
issues. I chose to leave those in place for now.
Helped-by: Taylor Blau <ttaylorr@openai.com>
Signed-off-by: Derrick Stolee <stolee@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>