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

fix: stop polling on environment close #29707

Merged
merged 6 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions app/scripts/controllers/account-tracker-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,23 @@ describe('AccountTrackerController', () => {
});
});

it('should gracefully handle unknown polling tokens', async () => {
await withController(({ controller, blockTrackerFromHookStub }) => {
jest.spyOn(controller, 'updateAccounts').mockResolvedValue();

const pollingToken =
controller.startPollingByNetworkClientId('mainnet');

controller.stopPollingByPollingToken('unknown-token');
controller.stopPollingByPollingToken(pollingToken);

expect(blockTrackerFromHookStub.removeListener).toHaveBeenCalledWith(
'latest',
expect.any(Function),
);
});
});

it('should not unsubscribe from the block tracker if called with one of multiple active polling tokens for a given networkClient', async () => {
await withController(({ controller, blockTrackerFromHookStub }) => {
jest.spyOn(controller, 'updateAccounts').mockResolvedValue();
Expand All @@ -391,14 +408,6 @@ describe('AccountTrackerController', () => {
}).toThrow('pollingToken required');
});
});

it('should error if no matching pollingToken is found', async () => {
await withController(({ controller }) => {
expect(() => {
controller.stopPollingByPollingToken('potato');
}).toThrow('pollingToken not found');
});
});
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add a unit test to verify that if it is called with a token that matches nothing, and then called with a token that does match, that unsubscribe is still successful? (Basically the 'should unsubscribe from the block tracker when called with a valid polling that was the only active pollingToken for a given networkClient' case, but with a call to the token that doesn't match

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added in 4f1f73b


describe('stopAll', () => {
Expand Down
5 changes: 0 additions & 5 deletions app/scripts/controllers/account-tracker-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,20 +403,15 @@ export default class AccountTrackerController extends BaseController<
if (!pollingToken) {
throw new Error('pollingToken required');
}
let found = false;
this.#pollingTokenSets.forEach((tokenSet, key) => {
if (tokenSet.has(pollingToken)) {
found = true;
tokenSet.delete(pollingToken);
if (tokenSet.size === 0) {
this.#pollingTokenSets.delete(key);
this.#unsubscribeWithNetworkClientId(key);
}
}
});
if (!found) {
throw new Error('pollingToken not found');
}
}

/**
Expand Down
9 changes: 8 additions & 1 deletion app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6987,7 +6987,7 @@ export default class MetamaskController extends EventEmitter {

/**
* A method that is called by the background when a particular environment type is closed (fullscreen, popup, notification).
* Currently used to stop polling in the gasFeeController for only that environement type
* Currently used to stop polling controllers for only that environement type
*
* @param environmentType
*/
Expand All @@ -6997,8 +6997,15 @@ export default class MetamaskController extends EventEmitter {
const pollingTokensToDisconnect =
this.appStateController.state[appStatePollingTokenType];
pollingTokensToDisconnect.forEach((pollingToken) => {
// We don't know which controller the token is associated with, so try them all.
// Consider storing the tokens per controller in state instead.
this.gasFeeController.stopPollingByPollingToken(pollingToken);
this.currencyRateController.stopPollingByPollingToken(pollingToken);
this.tokenRatesController.stopPollingByPollingToken(pollingToken);
this.tokenDetectionController.stopPollingByPollingToken(pollingToken);
this.tokenListController.stopPollingByPollingToken(pollingToken);
this.tokenBalancesController.stopPollingByPollingToken(pollingToken);
this.accountTrackerController.stopPollingByPollingToken(pollingToken);
this.appStateController.removePollingToken(
pollingToken,
appStatePollingTokenType,
Expand Down
Loading