Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made signal handling more uniform during crashes. #82163

Merged
merged 1 commit into from
Oct 5, 2023

Conversation

naelstrof
Copy link
Contributor

Fixes #82102

I've learned that signal is heavily implementation specific. According to the cppreference docs:

When signal handler is set to a function and a signal occurs, it is implementation defined whether signal(sig, SIG_DFL) will be executed immediately before the start of signal handler. Also, the implementation can prevent some implementation-defined set of signals from occurring while the signal handler runs.

This means that we won't know if further exceptions during crash handling will be ignored, cause loops, lockups, or crashes since it's completely implementation specific.

It also states

POSIX recommends sigaction instead of signal, due to its underspecified behavior and significant implementation variations, regarding signal delivery while a signal handler is executed.

I'm not sure if sigaction is a good fit for godot, since godot is multi-platform and sigaction is not. And it's nice keeping the crash handlers somewhat similar in design when possible.

So now for my proposed solution: a very simple fix is to enforce the behavior of signal by starting the crash handler with

static void handle_crash(int sig) {
    signal(SIGSEGV, SIG_DFL);
    signal(SIGFPE,  SIG_DFL);
    signal(SIGILL,  SIG_DFL);
...

This prevents further fatal errors from initiating patty-cake with the kernel for most implementations. The trade-off being that we still won't know if the implementation is currently ignoring any of the signals.

I think making it work for all implementations would require a reassessment for using sigaction.

Regardless, this change should make the crash handlers more sane without being complicated. Tested it and it works okay:
image

@naelstrof naelstrof requested review from a team as code owners September 23, 2023 01:33
@jams3223
Copy link

jams3223 commented Sep 23, 2023

You forgot to put a blank line between line 77 and line 78, small stuff but just to keep the code clean.

@naelstrof
Copy link
Contributor Author

Added the space, how's that?

@jams3223
Copy link

Added the space, how's that?

Sorry i meant a blank line.

@naelstrof
Copy link
Contributor Author

This pull request should also fix the freeze (but not the true source of the problem) for the following issues:
#54426
#77109

@AThousandShips AThousandShips changed the title Made signal handling more uniform during crashes. Fixes #82102 Made signal handling more uniform during crashes. Sep 23, 2023
@AThousandShips AThousandShips added this to the 4.x milestone Sep 23, 2023
@Calinou
Copy link
Member

Calinou commented Sep 26, 2023

Does SIGABRT also need to be handled? See #77286.

@naelstrof
Copy link
Contributor Author

naelstrof commented Sep 27, 2023

Does SIGABRT also need to be handled? See #77286.

In general, Godot's crash handler is several steps removed from being signal-safe. Handling more signals right now means more opportunities to mishandle them and cause lockups.

The default behavior of fatal signals is to simply crash, which is very safe. However it comes with the tradeoff of not providing any stack trace information for the user.

Ideally, Godot's crash handler would first be made signal-safe, then all signals that could be reasonably caused by a programmer's code should print a stack-trace to help identify what's wrong.

tldr Not right now, but yes after improvements have been made to the crash handler.

@naelstrof
Copy link
Contributor Author

I've created a new issue to address the lack of signal safety here for further discussion: #82418

Copy link
Member

@bruvzg bruvzg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me.

@akien-mga akien-mga modified the milestones: 4.x, 4.2 Oct 5, 2023
@akien-mga akien-mga merged commit d8ab953 into godotengine:master Oct 5, 2023
15 checks passed
@akien-mga
Copy link
Member

Thanks! And congrats for your first merged Godot contribution 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exception during close causes hang at "Dumping the backtrace."
7 participants