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 leaking windows message event listener #422

Merged

Conversation

yinzara
Copy link
Contributor

@yinzara yinzara commented Apr 15, 2020

In both "loginWithPopup" and "loginWithRedirect", window.addEventListener('message',... is called.

There are issues with the cleanup of this to properly remove it from the window. In the case of timeouts, it is not being removed properly. For loginWithPopup, it wasn't being removed at all causing leaking resources the more times its called.

@yinzara yinzara requested a review from a team April 15, 2020 23:30
@stevehobbsdev
Copy link
Contributor

Thanks for this @yinzara. The test is failing thanks to some whitespace removal but is being address in #420. Once that is merged we can bring it in here too.

Copy link
Contributor

@paulfalgout paulfalgout left a comment

Choose a reason for hiding this comment

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

just some thoughts. take-em-or-leave-em

src/utils.ts Outdated
@@ -42,7 +42,7 @@ export const runIframe = (
timeoutInSeconds: number = DEFAULT_AUTHORIZE_TIMEOUT_IN_SECONDS
) => {
return new Promise<AuthenticationResult>((res, rej) => {
var iframe = window.document.createElement('iframe');
let iframe = window.document.createElement('iframe');
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like this lib's style would prefer const here

src/utils.ts Outdated
const timeoutSetTimeoutId = setTimeout(() => {
rej(TIMEOUT_ERROR);
removeIframe();
window.removeEventListener('message', iframeEventHandler, false);
Copy link
Contributor

Choose a reason for hiding this comment

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

seems like this could just be in removeIframe both here and in the handler and it'd be slightly simpler.

@stevehobbsdev
Copy link
Contributor

@yinzara Please let me know if you'd like to continue with this PR. There are a few unit tests that look to be broken - could you evaluate and fix? Thanks.

@stevehobbsdev stevehobbsdev force-pushed the fix/popup-message-listener-cleanup branch from 200023a to d630dde Compare October 28, 2020 10:54
@stevehobbsdev
Copy link
Contributor

@adamjmcgrath I have fixed up this PR, could you please take a pass over?

clearTimeout(timeoutSetTimeoutId);
window.removeEventListener('message', iframeEventHandler, false);
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the danger of removing this one would be that, if you did 2 successful getTokenSilently({ ignoreCache: true }) within CLEANUP_IFRAME_TIMEOUT_IN_SECONDS seconds of each other, the first iframeEventHandler would still be around and handle the second getTokenSilently's postMessage

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, good catch - I'll reinstate it.

clearTimeout(timeoutId);
window.removeEventListener('message', popupEventListener, false);
Copy link
Contributor

Choose a reason for hiding this comment

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

This one is fine because you're removing the event listener in the successful and unsuccessful (timed out) cases

@stevehobbsdev stevehobbsdev added CH: Fixed PR is fixing a bug review:small Small review labels Oct 28, 2020
@stevehobbsdev stevehobbsdev added this to the vNext milestone Oct 28, 2020
@stevehobbsdev stevehobbsdev merged commit ed9b388 into auth0:master Oct 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CH: Fixed PR is fixing a bug review:small Small review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants