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

Bug 1446236 - Add & use simpler method to check if an extension is present #35

Merged
merged 6 commits into from
Apr 8, 2018

Conversation

CyberShadow
Copy link
Member

Ideally these should probably all be refactored into extension-agnostic hooks, but for the time being simplify the code a little.

This adds extensions_hash as a separate variable to request_cache. An alternative would be to change $cache->{extensions} to a hash, however as hashes are unsorted, I was worried that it could introduce unwanted effects due to the iteration order being changed.

Add a new method to check if an extension is present and loaded.
Transition from previous explicit searches in the extension list to
invoking the method added in the previous commit.
}
return exists $cache->{extensions_hash}{$name};
}

Copy link
Member Author

Choose a reason for hiding this comment

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

There is something wrong with this implementation, but I'm not sure yet what. After the hash is built, it contains only two elements on successive requests.

Copy link

Choose a reason for hiding this comment

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

That suggests to me there should be a test to capture correctness in extensions, which I gather this change would fail (and prompt an investigation of why).

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 figured out the problem, it was caused by a change in another pull request of mine.

Bugzilla.pm Outdated
@@ -248,6 +248,19 @@ sub extensions {
return $cache->{extensions};
}

sub have_extension {
Copy link
Member

Choose a reason for hiding this comment

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

I think has_extension() is a better name, as it maps to has_feature(). Not sure if it's grammatically correct or not though.

Copy link
Member Author

Choose a reason for hiding this comment

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

Renamed.

Copy link
Member

@dylanwh dylanwh left a comment

Choose a reason for hiding this comment

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

Suggested changes to make it work.

Bugzilla.pm Outdated
my ($class, $name) = @_;
my $cache = $class->request_cache;
if (!$cache->{extensions_hash}) {
my %extensions;
Copy link
Member

Choose a reason for hiding this comment

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

my %extensions = map { $_->NAME => 1 } @{ Bugzilla->extensions };

Copy link
Member Author

Choose a reason for hiding this comment

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

Same problem with that code... but it is shorter.

Also using () does something really weird here. I thought it evaluates to undef, which should be slightly more efficient than using 1 or any other value.

Copy link
Member Author

Choose a reason for hiding this comment

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

Found the problem: Bugzilla->extensions was being invoked recursively (as it initializes extensions as it populates the list), so the truncated list I was seeing was from the incomplete invocation higher in the stack trace.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed

There is no simple and obvious way to allow extensions to query the
extension list as it is being constructed, so explicitly forbid this
behavior.

The trap will help avoid weird bugs caused by halfway-populated and
cached extension lists.
Suggested-by: Dylan William Hardison <[email protected]>
@CyberShadow
Copy link
Member Author

Will update the other PRs to rename have_extension when this is merged.

@@ -245,9 +250,20 @@ sub extensions {
}
$cache->{extensions} = \@extensions;
}
$recursive = 0;
Copy link

Choose a reason for hiding this comment

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

I don't understand why you are guarding against a recursive call. That at least needs a comment.

Copy link
Member Author

Choose a reason for hiding this comment

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

There's some explanation in the commit message. Essentially it's an extension checking Bugzilla->extensions during its initialization, which is invalid as the extension list is still being constructed.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added a comment.

Copy link

Choose a reason for hiding this comment

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

Great! Looking at it now, my gut says it might not work completely right. I'd have thought it was more idiomatic to use not a state var, but a my var outside the sub, and use local to set the guard variable straight after the check, in order to use dynamic scoping rather than lexical.

}
return exists $cache->{extensions_hash}{$name};
}

Copy link

Choose a reason for hiding this comment

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

That suggests to me there should be a test to capture correctness in extensions, which I gather this change would fail (and prompt an investigation of why).

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

Successfully merging this pull request may close these issues.

3 participants