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]: Multiple Event Listeners Accumulation in CheckSessionService #2015

Open
jagipas opened this issue Sep 28, 2024 · 0 comments
Open

[Bug]: Multiple Event Listeners Accumulation in CheckSessionService #2015

jagipas opened this issue Sep 28, 2024 · 0 comments

Comments

@jagipas
Copy link

jagipas commented Sep 28, 2024

Version

18.0.1

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

## Description
The `CheckSessionService` is accumulating multiple event listeners for the 'message' event, causing the `messageHandler` to be executed multiple times for each event. This issue appears to have been introduced in PR #1735.

- Each time `bindMessageEventToIframe()` is called, a new event listener is added to `defaultView` without removing the previous one.
- This results in multiple executions of the `messageHandler` for a single 'message' event.
- The problem worsens over time as more listeners accumulate.

Steps to reproduce the behavior

1. Initialize the `CheckSessionService`.
2. Call `bindMessageEventToIframe()` multiple times (this may happen implicitly through other service methods).
3. Observe that 'message' events trigger multiple executions of the `messageHandler`.

A clear and concise description of what you expected to happen.

- Only one event listener should be active at any given time.
- When `bindMessageEventToIframe()` is called, it should remove any existing listener before adding a new one.

Additional context

Possible Solution

Modify the bindMessageEventToIframe() method to remove the existing listener before adding a new one:

private bindMessageEventToIframe(configuration: OpenIdConfiguration): void {
  const defaultView = this.document.defaultView;

  // Remove existing listener if there is one
  if (this.iframeMessageEventListener && defaultView) {
    defaultView.removeEventListener('message', this.iframeMessageEventListener, false);
  }

  // Create and assign the new listener
  this.iframeMessageEventListener = this.messageHandler.bind(
    this,
    configuration
  );

  if (defaultView) {
    defaultView.addEventListener(
      'message',
      this.iframeMessageEventListener,
      false
    );
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant