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

Better model panics #1716

Merged
merged 3 commits into from
Mar 23, 2023
Merged

Conversation

IamTheCarl
Copy link
Contributor

@IamTheCarl IamTheCarl commented Mar 23, 2023

Mostly resolves addresses #1569 . (edited by @hannobraun: "resolves" is a magic word that closes the issue; GitHub will readily take instructions from random words within the pull request description, but doesn't know what "mostly" means)

It currently does not print the stack trace, but it does print the fact that it's a panic and a line number where the panic occurred. Given a little more time I might be able to get the stack trace.

image

The key is to override the panic hook within the model. This cannot be done externally, so the initialization code is injected into the model function by the #[fj::model] macro. That new panic hook then places all panics into a global variable for the on_panic function to grab. I suspect this could get a bit funky when multiple threads are involved, but the worst I would expect is it giving a "no details provided" message or the wrong error message on a panic.

@IamTheCarl IamTheCarl requested a review from hannobraun as a code owner March 23, 2023 02:19
Copy link
Owner

@hannobraun hannobraun left a comment

Choose a reason for hiding this comment

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

Thank you, @IamTheCarl! Looks good and works great.

I've left two comments with suggestions for potential improvements. None of those are critical, so I'm going to merge this now. But feel free to send a follow-up PR, if you agree with them.

I also made a small edit to your pull request description, to prevent #1569 from being closed when I merge this.

Comment on lines +100 to +149
// Yes, all of this is to add `fj::abi::initialize_panic_handling();` to the top of the function.
item.block.stmts.insert(
0,
Stmt::Semi(
Expr::Call(ExprCall {
attrs: vec![],
func: Box::new(Expr::Path(ExprPath {
attrs: vec![],
qself: None,
path: syn::Path {
leading_colon: None,
segments: {
let mut segments = Punctuated::new();

segments.push(PathSegment {
ident: Ident::new(
"fj",
Span::call_site(),
),
arguments: PathArguments::None,
});

segments.push(PathSegment {
ident: Ident::new(
"abi",
Span::call_site(),
),
arguments: PathArguments::None,
});

segments.push(PathSegment {
ident: Ident::new(
"initialize_panic_handling",
Span::call_site(),
),
arguments: PathArguments::None,
});

segments
},
},
})),
paren_token: Paren {
span: Span::call_site(),
},
args: Punctuated::new(),
}),
syn::token::Semi::default(),
),
);
Copy link
Owner

Choose a reason for hiding this comment

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

I'm far from an expert in procedural macros and might be wrong about what's happening here, but couldn't this be done much easier using the quote! macro? See expand.rs for examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll give it a try tonight. I've still got my experiments to do with collecting a backtrace after all.

Comment on lines +157 to +158
eprintln!("TOKENS: {}", tokens);

Copy link
Owner

Choose a reason for hiding this comment

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

It it intentional that this was left in here? Or just leftover debug output?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Leftover debug output... I thought I got them all...

@hannobraun hannobraun merged commit 55c83b8 into hannobraun:main Mar 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants