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

ZxingScannerComponen.init method called twice when enable flag is set to true (leak once again) #559

Open
RafalOddslife opened this issue Apr 25, 2024 · 8 comments
Labels

Comments

@RafalOddslife
Copy link

Hi, it's me again. I didn't notice this earlier. Issue which gives similar effect as #557 but now it's caused by [enable]="true" flag.

Describe the bug

ZxingScannerComponen.init method is called twice when enable flag is set to true which causes to run scanning twice (leak). onDestroy stops only one of scanning processes.
Affected version: "@zxing/ngx-scanner": "^17.0.2"

Code

<button (click)="scannerVisible = !scannerVisible">Toggle scanner</button>
<zxing-scanner [enable]="true" *ngIf="scannerVisible"></zxing-scanner>

To Reproduce

Steps to reproduce the behavior:

  1. Go to simple repro
  2. Use toggle button to init and destroy zxing-scanner component (the more you do this, the bigger the leakage)
  3. Each time scanner component is initialized it's calling 'init' method twice
  4. Open chrome devtools, open Performance tab and capture new recording
  5. BrowserCodeReader is still trying to decode even after destroying component

Expected behavior
Component should start only one scanning process.

Screenshots
image

@werthdavid
Copy link
Member

Is this still the case with the version from today? From looking at the code this should be resolved via #557

@RafalOddslife
Copy link
Author

Is this still the case with the version from today?

Yes, your PR fixed problem with disposing scanning process on component destroy.

This issue is caused by initializing scanning process twice when enable input is set to true. Stackblitz repro is based on the newest version 17.0.2

@werthdavid
Copy link
Member

Damnit, as setting enable to false calls the reset method I was hoping this would be resolved. I'll take a look then. Thanks

@RafalOddslife
Copy link
Author

If you setup breakpoint in ZXingScannerComponent.scanFromDevice you'll see it's called twice on component creation and this._scanSubscription is overwritten.

@werthdavid
Copy link
Member

Good point! Thank you

werthdavid added a commit that referenced this issue Apr 25, 2024
werthdavid added a commit that referenced this issue Apr 25, 2024
#559 ZxingScannerComponen.init method called twice when enable flag i…
@werthdavid
Copy link
Member

plz test 17.0.3

@RafalOddslife
Copy link
Author

I'm afraid it solves my repro but the root of the problem is more complicated

IMO these leaks are caused by:

  • calling async methods from sync context (ngOnInit and enable setter)
  • parent component/template can toggle enable flag many times which leads to multiple ZxingScannerComponent.init calls without clearing previously created scanning processes

Simple example:

<button (click)="scannerEnable = !scannerEnable">Toggle scanner enable</button>
<zxing-scanner [enable]="scannerEnable"></zxing-scanner>

it causes leak when you toggle enable flag few times quickly

@werthdavid
Copy link
Member

the fix is indeed not addressing the root of the problem. with my upcoming vacation I'm not sure when I will find time to look deeper into it that's why I keep the issue open.

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

No branches or pull requests

2 participants