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

Improve panic handling code and communication #569

Merged
merged 2 commits into from
Nov 5, 2024
Merged

Conversation

webmaster128
Copy link
Member

No description provided.

Copy link
Collaborator

@chipshort chipshort left a comment

Choose a reason for hiding this comment

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

Nice!

Comment on lines +27 to +33
fn handle_vm_panic_works() {
fn nice_try() {
panic!("oh no!");
}
let err = catch_unwind(nice_try).unwrap_err();
handle_vm_panic("nice_try", err);
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe a bit of a nitpick, but I'm not sure how I feel about this test. It's really testing catch_unwind rather than handle_vm_panic. Basically the only thing this ensures about our code is that handle_vm_panic doesn't panic itself.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree for the automated case and it might not be a text book style test. However, it is very handy for manual inspection of the console output (which revealed the Any debug problem) as well as ensuring the function can be used in isolation with the weird input type. Do you have an idea how to improve the test?

Copy link
Member

Choose a reason for hiding this comment

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

The guard pattern maybe? A bit verbose but no parameter needed?

static VM_WRITER: RefCell<Option<Box<dyn Writer>>> = RefCell::new(None);

fn with_writer<W: Write + 'static, F: Fn(&dyn Writer)>(w: W, func: F) {
    let prev = VM_WRITER.get();
    VM_WRITER.set(Some(Box::new(w)));
    access_writer(func);
    VM_WRITER.set(prev);
}

fn access_writer<F: Fn(&dyn Writer)>(func: F) {
    VM_WRITER.with_borrow(func);
}

[...]

access_writer(|w| writeln!(w, "hello world!"));

Copy link
Member

Choose a reason for hiding this comment

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

Well, that code itself won't work immediately because .with_borrow isn't enough and it needs a fallback case to io::stdout() but close enough to illustrate what I mean.

Copy link
Member

Choose a reason for hiding this comment

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

And the overhead is alright since it's a rare case to occur anyway (hopefully never)

Copy link
Member Author

Choose a reason for hiding this comment

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

No strong opinion on if/how to improve this. But if there are no easy wins, I'd merge this as is and leave it up to you to improve it later.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Alright, then let's merge as is for now.

@chipshort chipshort merged commit 0d7a848 into main Nov 5, 2024
14 checks passed
@chipshort chipshort deleted the add-handle_vm_panic branch November 5, 2024 05:19
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.

3 participants