ianlancetaylor / libbacktrace

A C library that may be linked into a C/C++ program to produce symbolic backtraces
Other
975 stars 228 forks source link

handling open file descriptor limits with early initialisation #77

Closed stefantalpalaru closed 3 months ago

stefantalpalaru commented 3 years ago

When you reach your process' open file descriptor limit, you can no longer produce a backtrace, because libbacktrace tries and fails to open the executable:

libbacktrace error: failed to read executable information (-1)
libbacktrace error: failed to read executable information (-1)
libbacktrace error: failed to read executable information (-1)
libbacktrace error: failed to read executable information (-1)
libbacktrace error: failed to read executable information (-1)
libbacktrace error: failed to read executable information (-1)
libbacktrace error: failed to read executable information (-1)
libbacktrace error: failed to read executable information (-1)
libbacktrace error: failed to read executable information (-1)
libbacktrace error: failed to read executable information (-1)
libbacktrace error: failed to read executable information (-1)

What we need is to replace that on-demand file opening with an early one - either by making fileline_initialize() public, so we can call it after backtrace_create_state(), or by including a call to it in the latter function.

ianlancetaylor commented 3 months ago

The fix is to call any backtrace function early in your program execution. Once you've done that, libbacktrace shouldn't need to open any more files. You can just discard the results.

stefantalpalaru commented 3 months ago

The fix is to call any backtrace function early in your program execution.

My use case was generating stack traces for exceptions in Nim programs: https://github.com/nim-lang/Nim/pull/12922

ianlancetaylor commented 3 months ago

OK, but I'm not sure what you are saying. I think that my comment still applies.

stefantalpalaru commented 3 months ago

I think that my comment still applies.

No, of course not. With exceptions, I collect program counters during stack traversal and use them to generate a stack trace (using libbacktrace) when printing an exception: https://github.com/status-im/nim-libbacktrace/blob/master/libbacktrace.nim

That usually happens at the end of a program, for fatal errors, or in the middle of it, for recoverable ones where we want to log exceptions (and associated stack traces).

ianlancetaylor commented 3 months ago

I think that we are somehow failing to communicate.

In the original issue report you said "What we need is to replace that on-demand file opening with an early one - either by making fileline_initialize() public, so we can call it after backtrace_create_state(), or by including a call to it in the latter function."

I am saying that such a function already exists: call backtrace_full and ignore the results.

stefantalpalaru commented 3 months ago

Got it. Thanks.