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

#6624: Exclude discarded tabs in broadcasts and other "all tabs" actions #6626

Merged
merged 6 commits into from
Oct 13, 2023

Conversation

fregante
Copy link
Contributor

@fregante fregante commented Oct 10, 2023

What does this PR do?

It's ready for review. I'll want to test this before merging though Done

Checklist

  • Add tests
  • New files added to src/tsconfig.strictNullChecks.json (if possible)
  • Designate a primary reviewer: @grahamlangford

@fregante fregante changed the title WIP: Limit forEachTab WIP: Exclude discarded tabs Oct 10, 2023
@codecov
Copy link

codecov bot commented Oct 10, 2023

Codecov Report

Attention: 19 lines in your changes are missing coverage. Please review.

Comparison is base (d6128c7) 70.15% compared to head (8d8498a) 70.19%.
Report is 25 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6626      +/-   ##
==========================================
+ Coverage   70.15%   70.19%   +0.03%     
==========================================
  Files        1179     1178       -1     
  Lines       36654    36652       -2     
  Branches     6899     6899              
==========================================
+ Hits        25715    25727      +12     
+ Misses      10939    10925      -14     
Files Coverage Δ
src/background/deploymentUpdater.ts 88.40% <100.00%> (-0.06%) ⬇️
src/background/modUpdater.ts 92.07% <100.00%> (ø)
src/background/navigation.ts 0.00% <ø> (ø)
src/background/removeExtensionForEveryTab.ts 80.00% <100.00%> (+20.00%) ⬆️
src/background/starterMods.ts 86.48% <100.00%> (ø)
src/bricks/renderers/propertyTable.tsx 26.47% <ø> (ø)
src/contrib/google/sheets/core/useCurrentOrigin.ts 100.00% <100.00%> (ø)
src/utils/promiseUtils.ts 69.86% <100.00%> (-0.45%) ⬇️
src/background/telemetry.ts 65.64% <50.00%> (+0.26%) ⬆️
src/background/executor.ts 34.78% <57.14%> (+0.80%) ⬆️
... and 1 more

... and 1 file with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -436,7 +437,7 @@ export async function pong(): Promise<{ timestamp: number }> {
*/
export async function collectPerformanceDiagnostics(): Promise<Diagnostics> {
const timestamp = Date.now();
const allTabs = await browser.tabs.query({});
const allTabs = await getTabsWithAccess();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This now excludes discarded tabs and other tabs where PB does not run. I think it wouldn't make sense to track those. Can revert if needed

export async function getTabsWithAccess(): Promise<TabId[]> {
const tabs = await browser.tabs.query({
url: ["https://*/*", "http://*/*"],
discarded: false,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the core of the change:

  • only query the specific protocols we're looking for
  • exclude discarded tabs

const tabs = await getTabsWithAccess();

if (tabs.length > 20) {
console.warn("forEachTab called on more than 20");
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Second core part:

  • Log to console if there are a lot of tabs.

Alternatively:

  • replace this with reportEvent?

TCallback extends (target: { tabId: number }) => Promisable<unknown>
>(
callback: TCallback,
options?: { exclude: number }
Copy link
Contributor Author

@fregante fregante Oct 10, 2023

Choose a reason for hiding this comment

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

forEachTab now lets you optionally exclude a specific tab. I think this is useful in any instance where the action originates in a tab.

@fregante fregante changed the title WIP: Exclude discarded tabs Exclude discarded tabs Oct 10, 2023
@fregante fregante changed the title Exclude discarded tabs Exclude discarded tabs in broadcasts and other "all tabs" actions Oct 10, 2023
@fregante fregante changed the title Exclude discarded tabs in broadcasts and other "all tabs" actions #6624: Exclude discarded tabs in broadcasts and other "all tabs" actions Oct 10, 2023
@fregante fregante marked this pull request as ready for review October 10, 2023 18:35
@fregante fregante mentioned this pull request Oct 11, 2023
2 tasks
@fregante fregante force-pushed the F/perf/exclude-discarded-tabs branch from 59d1db9 to 4a103ed Compare October 11, 2023 06:53
@@ -1,39 +0,0 @@
/** @file It's possible that some of these tabs might lose the permission in the meantime, we can't track that exactly */
Copy link
Contributor Author

Choose a reason for hiding this comment

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

"activeTab" referred to some logic that was dropped a few months ago. I moved these functions to extensionUtils

// The origin should be set to the window.location.protocol + '//' +
// window.location.host of the top-most page, if your application is
// running in an iframe.
return url.protocol + "//" + url.host;
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 ended up here looking for tabs.query() usage

The comments in this file seem copy-pasted and out of place (it mentions "in this PR", what PR?). I dropped some.

async ({ tabId }) =>
safelyRunBrick({ tabId, frameId: TOP_LEVEL_FRAME_ID }, subRequest),
{
exclude: sourceTabId,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Replacing asyncForEach with the more specific forEachTab here allowed for a slight refactoring, thanks to the included Promise.allSettled

Comment on lines +175 to +178
if (rejected.length > 0) {
console.warn(`Broadcast rejected for ${rejected.length} tabs`, {
rejected,
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

What are the possible reasons for rejected tabs? Stale or closed tabs? Should we log this to Rollbar as well?

Copy link
Contributor Author

@fregante fregante Oct 11, 2023

Choose a reason for hiding this comment

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

What are the possible reasons for rejected tabs?

Stale or closed tabs?

Discarded tabs are already excluded.

There's a possible race condition if the user closes a tab between line 165 and line 166 😅 but I reckon it's rare.

Should we log this to Rollbar as well?

I think this generally classifies as user/business error. We don't log errors that are due to unconnectable tabs.

https://github.com/pixiebrix/pixiebrix-extension/blob/3b9730a20839903c643928e9735a32fcc71da5ae/src/errors/errorHelpers.ts/#L69-L72

Comment on lines +203 to +206
if (rejected.length > 0) {
console.warn(`Broadcast rejected for ${rejected.length} frame`, {
rejected,
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as above, except that frames are more likely to come into existence and disappear. Still classifies as a non-reportable error.

@twschiller twschiller requested a review from BLoe October 11, 2023 13:12
@twschiller
Copy link
Contributor

twschiller commented Oct 11, 2023

Adding @BLoe as reviewer because he will be helping out with performance issues

@github-actions
Copy link

No loom links were found in the first post. Please add one there if you'd like to it to appear on Slack.

Do not edit this comment manually.

@fregante fregante force-pushed the F/perf/exclude-discarded-tabs branch from 5ad0e78 to 8d8498a Compare October 12, 2023 07:05
@fregante fregante merged commit 2ab6d92 into main Oct 13, 2023
10 checks passed
@fregante fregante deleted the F/perf/exclude-discarded-tabs branch October 13, 2023 07:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants