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

Guarantee that WidgetAdded is the first thing a widget sees. #1259

Merged
merged 3 commits into from
Nov 9, 2020

Conversation

luleyleo
Copy link
Collaborator

Closes #1127 and hopefully closes #1258 as well.

With this, WidgetPod will panic whenever a widget receives anything but LifeCycle::WidgetAdded as its first event.

The advantage of a panic is that the backtrace makes it in my experience very easy to track down where things went wrong.

@luleyleo luleyleo added the S-needs-review waits for review label Sep 27, 2020
@luleyleo luleyleo force-pushed the debug-panic branch 2 times, most recently from 71a92a7 to 55bb1f0 Compare September 27, 2020 12:38
Comment on lines 553 to 532
// Should commands require layout?
// If yes, it will cause problems with our test suite.
Event::Command(_) => (),
_ => {
log::warn!(
"Widget '{}' received an event ({:?}) without having been laid out. \
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I had to exclude Event::Command here, because otherwise our tests would break.
There we often send test commands without doing any layout.

This can of course be changed back, but I wanted to push this now without meddling too much with our test stuff.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This came up in #1160 recently, because unviewed tabs don't get laid out but they might still receive commands. I think there's a bigger decision that needs to be made (and documented) about which events and lifecycle events hidden widgets should expect to receive, but for now I think it's completely reasonable for them to get commands.

Copy link
Member

Choose a reason for hiding this comment

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

I agree, it's even totally reasonable for a Command to be sent while handling Event::WindowConnected, and in that case layout should not have been called.

Copy link
Collaborator

Choose a reason for hiding this comment

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

There are methods in the tabs code that try to say whether an event or lifecycle should be delivered to hidden widgets. Possibly should be moved to the enums themselves.

@luleyleo
Copy link
Collaborator Author

@raphlinus does this help you with #1258?

Copy link
Member

@cmyr cmyr left a comment

Choose a reason for hiding this comment

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

A few notes but this looks like it should be quite helpful with debugging some of this subtler lifecycle stuff.

druid/src/core.rs Outdated Show resolved Hide resolved
@@ -411,6 +418,14 @@ impl<T: Data, W: Widget<T>> WidgetPod<T, W> {
return;
}

if !self.is_initialized() {
debug_panic!(
Copy link
Member

Choose a reason for hiding this comment

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

Although I think debug_panic! is an excellent addition, I wonder if this shouldn't actually be a debug_assert!?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

debug_assert! does nothing in release, while debug_panic! at least logs an error. Thus, I would prefer debug_panic.

Copy link
Member

Choose a reason for hiding this comment

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

makes sense!

Comment on lines 553 to 532
// Should commands require layout?
// If yes, it will cause problems with our test suite.
Event::Command(_) => (),
_ => {
log::warn!(
"Widget '{}' received an event ({:?}) without having been laid out. \
Copy link
Member

Choose a reason for hiding this comment

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

I agree, it's even totally reasonable for a Command to be sent while handling Event::WindowConnected, and in that case layout should not have been called.

/// but it will log the provided message instead of ignoring it in release builds.
///
/// It's useful when a backtrace would aid debugging but a crash can be avoided in release.
macro_rules! debug_panic {
Copy link
Member

Choose a reason for hiding this comment

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

this is an excellent addition.

@cmyr
Copy link
Member

cmyr commented Sep 28, 2020

Just checking in, is this ready to go? If not let us know what you need.

@luleyleo
Copy link
Collaborator Author

I'm waiting for @raphlinus to see if this helps with #1258

@cmyr
Copy link
Member

cmyr commented Nov 8, 2020

What's the status here? does it still hopelessly break crochet?

This will panic in debug mode and log in release mode.
It's useful when a backtrace would be valuable but a crash can be avoided.
@luleyleo
Copy link
Collaborator Author

luleyleo commented Nov 8, 2020

No, Crochet would work just fine with it, since I fixed the context handling. This is just waiting for someone to say yay or nay.

Copy link
Member

@cmyr cmyr left a comment

Choose a reason for hiding this comment

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

Okay, my inclination is to merge this, I think it may uncover some problems but ultimately that will be good for us? Worst case scenario we can always revert.

CHANGELOG.md Outdated Show resolved Hide resolved
@luleyleo
Copy link
Collaborator Author

luleyleo commented Nov 9, 2020

Ok. Once the checks are done, I'll merge it.

@luleyleo luleyleo merged commit 3ea404f into linebender:master Nov 9, 2020
@luleyleo luleyleo removed the S-needs-review waits for review label Nov 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants