Title: FakeAsyncZone and RootZone used in conjunction can clobber interleaved timers of each other · Issue #54323 · angular/angular · GitHub
Open Graph Title: FakeAsyncZone and RootZone used in conjunction can clobber interleaved timers of each other · Issue #54323 · angular/angular
X Title: FakeAsyncZone and RootZone used in conjunction can clobber interleaved timers of each other · Issue #54323 · angular/angular
Description: Which @angular/* package(s) are the source of the bug? zone.js Is this a regression? Yes Description Currently, Zone.js patches setTimeout and keeps a tasksByHandleId map to keep a timerId <-> ZoneTask relationship. This map appears to b...
Open Graph Description: Which @angular/* package(s) are the source of the bug? zone.js Is this a regression? Yes Description Currently, Zone.js patches setTimeout and keeps a tasksByHandleId map to keep a timerId <-> Zone...
X Description: Which @angular/* package(s) are the source of the bug? zone.js Is this a regression? Yes Description Currently, Zone.js patches setTimeout and keeps a tasksByHandleId map to keep a timerId <->...
Opengraph URL: https://github.com/angular/angular/issues/54323
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"FakeAsyncZone and RootZone used in conjunction can clobber interleaved timers of each other","articleBody":"### Which @angular/* package(s) are the source of the bug?\n\nzone.js\n\n### Is this a regression?\n\nYes\n\n### Description\n\nCurrently, Zone.js patches `setTimeout` and keeps a `tasksByHandleId` map to keep a `timerId` \u003c-> `ZoneTask`\r\nrelationship. This map appears to be _global_ to each method per `window` (browser use case) / `global` instance and not per `Zone` as it's defined on `patchTimer`.\r\n\r\nTasks then use the return value from their schedule methods (such as `setTimeout`) to store this ID in `tasksByHandleId`. For example, in the case of a root zone with no wrapping it normally just calls\r\n`setNative` which may invoke `setTimeout` and we store the timer ID in the map. These IDs are monotonically increasing and therefore are never reused. However, in the case of [`fake-async-test`](https://github.com/angular/angular/blob/main/packages/zone.js/lib/zone-spec/fake-async-test.ts#L69) it starts from `1` and increments it's own counters per task. \r\n\r\nIf you have a `Zone` that is using `setNative` and scheduling using the real browser scheduler and using `fake-async-zone` it is possible to generate two tasks that are both pending with the same ID because they don't know of each other. For example:\r\n\r\n```\r\nlet oldTimerId = \r\nrunInFakeAsyncZone(() => {\r\n // Run a timer in such a way that it also returns the same timer ID\r\n // but does not resolve, thus overwriting the taskMap\r\n // Maybe it wasn't fired from an even and has to be flushed or something\r\n oldTimerId = ...\r\n});\r\n\r\n// Run in \u003croot>, running later\r\n// timerId == oldTimerId now\r\n// Therefore, `tasksByHandleId` is overwritten to use this ID\r\nlet timerId = setTimeout(() => console.log('I should run but never do'), 30000);\r\n\r\n\r\nrunInFakeAsyncZone(() => {\r\n // We changed our mind, cancel the timer \r\n clearInterval(oldTimerId)\r\n\r\n // Actually, they shared an ID and `tasksByHandleId` was updated\r\n // to point to the \u003croot> task so `task.zone.cancelTask(task)` is invoked\r\n // but the implementation uses `clearNative` and clears the \u003croot> task\r\n // meaning that the setTimeout never runs\r\n});\r\n```\r\n\r\nIn the above psuedo-code, the task that runs in some other zone is overwritten with one that runs the `root` later because of the shared map. In contexts outside of `fake-async-test`, it's likely never going to happen\r\nbecause the IDs are strictly monotonically increasing. But in `fake-async-test`, this is troublesome because they can overlap which is bad news when using a single global `tasksByHandleId`\r\n\r\nSome thoughts:\r\n\r\n1. I don't know if this use case is that common but it's at least common in a couple of Google tests that trigger this behaviour\r\n2. If overlap is not expected, the task map should probably assert and break early because this was a _huge pain_ to track down as not only is timing sensitive but it's also a read / write race\r\n3. If overlap is expected in some cases, we need to somehow handle this\r\n\r\nI've not included a full reproduction yet because the exact specifics are kind of hard to encode but I will try and get something up.\n\n### Please provide a link to a minimal reproduction of the bug\n\nN/A; coming soon maybe\n\n### Please provide the exception or error you saw\n\n_No response_\n\n### Please provide the environment you discovered this bug in (run `ng version`)\n\n```true\nAngular: HEAD\n```\n\n\n### Anything else?\n\n_No response_","author":{"url":"https://github.com/hilts-vaughan","@type":"Person","name":"hilts-vaughan"},"datePublished":"2024-02-07T20:42:06Z","url":"https://github.com/angular/angular/issues/54323"}
| 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:0f183a21-b3d8-9104-2a76-26ba97e5f089 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | EB7E:62F48:A33258:D7F219:6B01EBF0 |
| html-safe-nonce | 5b6012c36ff08c15710ac21b68d2d743f58e7276bc54ba90a4603913921a8679 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFQjdFOjYyRjQ4OkEzMzI1ODpEN0YyMTk6NkIwMUVCRjAiLCJ2aXNpdG9yX2lkIjoiNjY3Njg4NDI0NTIyNDQ4MzgyNSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | abcbc9b4c23346928b4ddb9a355e8a02b1a0a12ca4f33fd1da57a9c17e866431 |
| hovercard-subject-tag | issue:2123848890 |
| 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/54323/issue_layout |
| twitter:image | https://opengraph.githubassets.com/8398ed085b7640ff0fb0c835d79693793925a95071b2587defbe1a1452768578/angular/angular/issues/54323 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/8398ed085b7640ff0fb0c835d79693793925a95071b2587defbe1a1452768578/angular/angular/issues/54323 |
| og:image:alt | Which @angular/* package(s) are the source of the bug? zone.js Is this a regression? Yes Description Currently, Zone.js patches setTimeout and keeps a tasksByHandleId map to keep a timerId <-> Zone... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | hilts-vaughan |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| 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 | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width