Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 36.7k
timers: do not retain a reference to the async store after firing#53443
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -87,6 +87,9 @@ const { | ||
| immediateInfo, | ||
| timeoutInfo, | ||
| } = binding; | ||
| const { | ||
| enqueueMicrotask, | ||
| } = internalBinding('task_queue'); | ||
| const { | ||
| getDefaultTriggerAsyncId, | ||
| @@ -97,6 +100,9 @@ const { | ||
| emitBefore, | ||
| emitAfter, | ||
| emitDestroy, | ||
| symbols: { | ||
| async_local_storage_context_symbol, | ||
| }, | ||
| } = require('internal/async_hooks'); | ||
| // Symbols for storing async id state. | ||
| @@ -125,6 +131,39 @@ const AsyncContextFrame = require('internal/async_context_frame'); | ||
| const async_context_frame = Symbol('kAsyncContextFrame'); | ||
| function removeStoresFromResource(resource) { | ||
| if (AsyncContextFrame.enabled) { | ||
| if (resource[async_context_frame] !== undefined) { | ||
| resource[async_context_frame] = undefined; | ||
| } | ||
| } else if (resource[async_local_storage_context_symbol] !== undefined) { | ||
| resource[async_local_storage_context_symbol] = undefined; | ||
| } | ||
| } | ||
| function cleanTimer(timer) { | ||
| removeStoresFromResource(timer); | ||
| timer._onTimeout = undefined; | ||
| timer._timerArgs = undefined; | ||
| } | ||
| function cleanImmediate(immediate) { | ||
| removeStoresFromResource(immediate); | ||
| immediate._onImmediate = undefined; | ||
| immediate._argv = undefined; | ||
| } | ||
| function enqueueRemoveStoresFromResource(resource) { | ||
| enqueueMicrotask(() => removeStoresFromResource(resource)); | ||
| } | ||
| function enqueueRemoveStoresIfNotReinserted(resource) { | ||
| enqueueMicrotask(() => { | ||
| if (!resource._idleNext && !resource._idlePrev) | ||
| removeStoresFromResource(resource); | ||
| }); | ||
| } | ||
| // *Must* match Environment::ImmediateInfo::Fields in src/env.h. | ||
| const kCount = 0; | ||
| const kRefCount = 1; | ||
| @@ -528,14 +567,22 @@ function getTimerCallbacks(runNextTicks) { | ||
| const asyncId = immediate[async_id_symbol]; | ||
| emitBefore(asyncId, immediate[trigger_async_id_symbol], immediate); | ||
| let threw = true; | ||
| try { | ||
| const argv = immediate._argv; | ||
| if (!argv) | ||
| immediate._onImmediate(); | ||
| else | ||
| immediate._onImmediate(...argv); | ||
| threw = false; | ||
| } finally { | ||
| immediate._onImmediate = null; | ||
| if (threw) { | ||
| immediate._onImmediate = undefined; | ||
| immediate._argv = undefined; | ||
| enqueueRemoveStoresFromResource(immediate); | ||
| } else { | ||
| cleanImmediate(immediate); | ||
| } | ||
| emitDestroy(asyncId); | ||
| @@ -607,6 +654,8 @@ function getTimerCallbacks(runNextTicks) { | ||
| if (!timer._destroyed) { | ||
| timer._destroyed = true; | ||
| cleanTimer(timer); | ||
| if (timer[kHasPrimitive]) | ||
| delete knownTimersById[asyncId]; | ||
| @@ -629,26 +678,42 @@ function getTimerCallbacks(runNextTicks) { | ||
| start = binding.getLibuvNow(); | ||
| } | ||
| let threw = true; | ||
| try { | ||
| const args = timer._timerArgs; | ||
| if (args === undefined) | ||
| timer._onTimeout(); | ||
| else | ||
| ReflectApply(timer._onTimeout, timer, args); | ||
| threw = false; | ||
| } finally { | ||
| if (timer._repeat && timer._idleTimeout !== -1) { | ||
| timer._idleTimeout = timer._repeat; | ||
| insert(timer, timer._idleTimeout, start); | ||
| } else if (!timer._idleNext && !timer._idlePrev && !timer._destroyed) { | ||
| timer._destroyed = true; | ||
| if (timer[kHasPrimitive]) | ||
| delete knownTimersById[asyncId]; | ||
| if (timer[kRefed]) | ||
| timeoutInfo[0]--; | ||
| emitDestroy(asyncId); | ||
| } else if (!timer._idleNext && !timer._idlePrev) { | ||
| if (timer._destroyed) { | ||
| timer._onTimeout = undefined; | ||
| timer._timerArgs = undefined; | ||
| if (threw) | ||
| enqueueRemoveStoresIfNotReinserted(timer); | ||
| else | ||
| removeStoresFromResource(timer); | ||
| } else { | ||
| if (threw) | ||
| enqueueRemoveStoresIfNotReinserted(timer); | ||
| else | ||
| removeStoresFromResource(timer); | ||
| timer._destroyed = true; | ||
| if (timer[kHasPrimitive]) | ||
| delete knownTimersById[asyncId]; | ||
| if (timer[kRefed]) | ||
| timeoutInfo[0]--; | ||
| emitDestroy(asyncId); | ||
| } | ||
| } | ||
| } | ||
| @@ -728,8 +793,11 @@ module.exports = { | ||
| kTimeout: Symbol('timeout'), // For hiding Timeouts on other internals. | ||
| async_id_symbol, | ||
| trigger_async_id_symbol, | ||
| async_context_frame, | ||
Flarna marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| Timeout, | ||
| Immediate, | ||
| cleanImmediate, | ||
| cleanTimer, | ||
| kRefed, | ||
| kHasPrimitive, | ||
| initAsyncResource, | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be extended to somthing like this to cover also the async context frame variant of ALS: