Title: Erratic behavior with DestroyRef, not all callbacks are always being called upon component destruction. · Issue #50221 · angular/angular · GitHub
Open Graph Title: Erratic behavior with DestroyRef, not all callbacks are always being called upon component destruction. · Issue #50221 · angular/angular
X Title: Erratic behavior with DestroyRef, not all callbacks are always being called upon component destruction. · Issue #50221 · angular/angular
Description: Which @angular/* package(s) are the source of the bug? core Is this a regression? Yes Description We have migrated our code base to use the new takeUntilDestroyed pipe along with DestroyRef. Unfortunately, we have noticed very erratic be...
Open Graph Description: Which @angular/* package(s) are the source of the bug? core Is this a regression? Yes Description We have migrated our code base to use the new takeUntilDestroyed pipe along with DestroyRef. Unfort...
X Description: Which @angular/* package(s) are the source of the bug? core Is this a regression? Yes Description We have migrated our code base to use the new takeUntilDestroyed pipe along with DestroyRef. Unfort...
Opengraph URL: https://github.com/angular/angular/issues/50221
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Erratic behavior with DestroyRef, not all callbacks are always being called upon component destruction.","articleBody":"### Which @angular/* package(s) are the source of the bug?\r\n\r\ncore\r\n\r\n### Is this a regression?\r\n\r\nYes\r\n\r\n### Description\r\n\r\nWe have migrated our code base to use the new takeUntilDestroyed pipe along with DestroyRef.\r\n\r\nUnfortunately, we have noticed very erratic behavior that with these, going so far as to not actually unsubscribe from some Observables at all after components are destroyed (I am still working on trying to provide a minimal reproduction for some cases).\r\n\r\nI have been able to replicate some bugs consistently with the reproduction below.\r\nhttps://stackblitz.com/edit/at-ng-14-router-aoffwi?file=src%2Fapp%2Fhello.component.ts\r\n\r\n# Reproduction\r\nThis small App contains\r\nA Service with a BehaviorSubject (bhSubject) that increments an integer and a Subject (subject) that \"increments\" strings.\r\nThose two actions are performed from AppComponent, which is always visible with the buttons.\r\nA third button \"Toggle Child\" will show and hide the HelloComponent, which contains the actual bug.\r\n\r\nHelloComponent registers a onDestroy callback in each of the following cases:\r\n1) In the callback given to subscribe() on bhSubject.\r\n2) In a method called from the same callback given to subscribe on bhSubject.\r\n3) In the callback given to subscribe() on subject.\r\n4) In a setTimeout of 0.\r\n5) In ngZone.runOutsideAngular()\r\n6) Directly in ngOnit()\r\n\r\nNote that a lot of the stuff is going on in the console.\r\n\r\n## Case 1:\r\n\r\n- Toggle Child\r\n- Toggle Child again\r\n\r\n### Expected onDestroy callbacks to have been called:\r\n- 1, 2, 4, 5?, 6\r\n\r\n### Actual callbacks called\r\n- 2, 4, 6\r\n\r\n## Case 2:\r\n\r\n- Toggle Child\r\n- Int++\r\n- Toggle Child\r\n\r\n### Expected onDestroy callbacks to have been called:\r\n- 1, 1, 2, 2, 4, 5?, 6\r\n\r\n### Actual callbacks called\r\n- 1, 2, 2, 4, 6\r\n\r\n## Case 3:\r\n\r\n- Toggle Child\r\n- Letter++\r\n- Toggle Child\r\n\r\n### Expected onDestroy callbacks to have been called:\r\n- 1, 2, 3, 4, 5?, 6\r\n\r\n### Actual callbacks called\r\n- 2, 3, 4, 6\r\n\r\n\r\nFollowing this, it seems like the first callback from a BehaviorSubject doesn't allow to use DestroyRef.\r\n\r\nNow you might be wondering why I added runOutsideAngular() in this, it's normal it's not working there. Well, if we comment the subscription to the normal subject (line 40 to 50 in hello.component.ts) and do case 1 again, it somehow does work with runOutsideAngular(). https://stackblitz.com/edit/at-ng-14-router-d9iur8?file=src%2Fapp%2Fhello.component.ts\r\n\r\n```ts\r\n /*this.someService.subject\r\n .pipe(takeUntilDestroyed(this.destroyRef))\r\n .subscribe((value) => {\r\n console.log(value);\r\n\r\n this.destroyRef.onDestroy(() => {\r\n console.log('Subject ' + value + ' destroyRef');\r\n });\r\n\r\n this.letterValue = value;\r\n });*/\r\n```\r\n\r\n## Case 4 (Same as Case 1, but with a subscription that is commented out):\r\n\r\n- Toggle Child\r\n- Toggle Child again\r\n\r\n### Expected onDestroy callbacks to have been called:\r\n- 1, 2, 4, 5?, 6\r\n\r\n### Actual callbacks called\r\n- 2, 4, 5, 6\r\n\r\n\r\n\r\n\r\nThis all seems very inconsistent. As stated before, we ran into bugs where takeUntilDestroyed() didn't work at all when used on observables within ngOnit directly (with destroyRef supplied, of course) and it wouldn't unsubscribe from all observables. Sadly I've not been able to reproduce this in a simple example yet, but this makes this new feature quite unusable. I'll keep you updated if I manage to craft a minimal reproduction for that bug as well.\r\n\r\n### Please provide a link to a minimal reproduction of the bug\r\n\r\nhttps://stackblitz.com/edit/at-ng-14-router-aoffwi?file=src%2Fapp%2Fhello.component.ts\r\n\r\n### Please provide the exception or error you saw\r\n\r\n_No response_\r\n\r\n### Please provide the environment you discovered this bug in (run `ng version`)\r\n\r\n```true\r\nAngular CLI: 16.0.0\r\nNode: 16.18.0\r\nPackage Manager: npm 9.6.4\r\nOS: win32 x64\r\n\r\nAngular: 16.0.0\r\n... animations, cdk, cli, common, compiler, compiler-cli, core\r\n... forms, localize, material, platform-browser\r\n... platform-browser-dynamic, router\r\n\r\nPackage Version\r\n---------------------------------------------------------\r\n@angular-devkit/architect 0.1600.0\r\n@angular-devkit/build-angular 16.0.0\r\n@angular-devkit/core 16.0.0\r\n@angular-devkit/schematics 16.0.0\r\n@schematics/angular 16.0.0\r\nrxjs 7.8.1\r\ntypescript 5.0.4\r\n```\r\n\r\n\r\n### Anything else?\r\n\r\n_No response_","author":{"url":"https://github.com/wartab","@type":"Person","name":"wartab"},"datePublished":"2023-05-09T16:38:21Z","url":"https://github.com/angular/angular/issues/50221"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:8db6bf86-c846-dc55-317d-255df79f9ffa |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D35E:1C94F:CE7C6C:1076DBB:6B013FCE |
| html-safe-nonce | d0d25691b198ba46a6600b35376f7de18c57f54907671e7a12b168460013fadd |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJEMzVFOjFDOTRGOkNFN0M2QzoxMDc2REJCOjZCMDEzRkNFIiwidmlzaXRvcl9pZCI6IjgzMzUzMzQ1NzYwNzUzOTA5MjYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | ee97a6e2322304d13d0b5b6c097dc08bd88111c6d8cc66bc3de659f4b7f34b13 |
| hovercard-subject-tag | issue:1702376856 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/angular/angular/50221/issue_layout |
| twitter:image | https://opengraph.githubassets.com/c7f1229bbda94fa745b1e32f1151c9e1ca39b1640e4deaa70bc86e1260a16904/angular/angular/issues/50221 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/c7f1229bbda94fa745b1e32f1151c9e1ca39b1640e4deaa70bc86e1260a16904/angular/angular/issues/50221 |
| og:image:alt | Which @angular/* package(s) are the source of the bug? core Is this a regression? Yes Description We have migrated our code base to use the new takeUntilDestroyed pipe along with DestroyRef. Unfort... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | wartab |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| go-import | github.com/angular/angular git https://github.com/angular/angular.git |
| octolytics-dimension-user_id | 139426 |
| octolytics-dimension-user_login | angular |
| octolytics-dimension-repository_id | 24195339 |
| octolytics-dimension-repository_nwo | angular/angular |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 24195339 |
| octolytics-dimension-repository_network_root_nwo | angular/angular |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width