-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
fix: unset context on stale promises #16935
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
Merged
+68
−3
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'svelte': patch | ||
--- | ||
|
||
fix: unset context on stale promises |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/svelte/tests/runtime-runes/samples/async-resolve-stale/_config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { test } from '../../test'; | ||
|
||
export default test({ | ||
async test({ assert, target }) { | ||
// We gotta wait a bit more in this test because of the macrotasks in App.svelte | ||
function macrotask(t = 3) { | ||
return new Promise((r) => setTimeout(r, t)); | ||
} | ||
|
||
await macrotask(); | ||
assert.htmlEqual(target.innerHTML, '<input> 1 | '); | ||
|
||
const [input] = target.querySelectorAll('input'); | ||
|
||
input.value = '1'; | ||
input.dispatchEvent(new Event('input', { bubbles: true })); | ||
await macrotask(); | ||
assert.htmlEqual(target.innerHTML, '<input> 1 | '); | ||
|
||
input.value = '12'; | ||
input.dispatchEvent(new Event('input', { bubbles: true })); | ||
await macrotask(6); | ||
// TODO this is wrong (separate bug), this should be 3 | 12 | ||
assert.htmlEqual(target.innerHTML, '<input> 5 | 12'); | ||
} | ||
}); |
38 changes: 38 additions & 0 deletions
38
packages/svelte/tests/runtime-runes/samples/async-resolve-stale/main.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<script> | ||
let count = $state(0); | ||
let value = $state(''); | ||
let prev; | ||
|
||
function asd(v) { | ||
const r = Promise.withResolvers(); | ||
|
||
if (prev || v === '') { | ||
console.log('hello', !!prev) | ||
Promise.resolve().then(async () => { | ||
console.log('count++') | ||
count++; | ||
r.resolve(v); | ||
await new Promise(r => setTimeout(r, 0)); | ||
// TODO with a microtask like below it still throws a mutation error | ||
// await Promise.resolve(); | ||
prev?.resolve(); | ||
}) | ||
} else { | ||
console.log('other') | ||
prev = Promise.withResolvers(); | ||
prev.promise.then(() => { | ||
console.log('other coun++') | ||
count++; | ||
r.resolve(v) | ||
}) | ||
} | ||
|
||
return r.promise; | ||
} | ||
|
||
const x = $derived(await asd(value)) | ||
</script> | ||
|
||
<input bind:value /> | ||
|
||
{count} | {x} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
we're not doing
d.promise.then(handler)
, we're doingd.promise.then(onresolved, onrejected)
— I'd expectonrejected
(which does callunset_context
) to run ifd.promise
rejects. what am I missing?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 had the same question, glad it didn't trick only me. The order of events is
async_effect
creates the promise, and runfn
which saves the contextasync_effect
creates a new promise and reject the old one, handler is executed with null and the context is clearedd.resolve
is invoked and that should clear the context but since d was already rejected it does nothingThe context is never cleared and there's a hanging
active_reaction
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.
The order of events is this:
d.promise.then
below runs, also unsets context but that doesn't matter, because ...$.async_derived((await save(x())()))
, which means therestore
method ofsave
runs. That means the context is now set again. Butd.promise.then(handle)
will NOT run, because that promise is already done (we called its reject), so the context is never unset (unless with my fix)Here's a playground that shows you the error without the fix (type in 1234 fast enough): https://svelte.dev/playground/hello-world?version=5.39.11#H4sIAAAAAAAACm1RwWrjMBD9lWEoRGq9bnroxU0Kve1xWXpb7UG1J4lAGRlpbKcY__uiaNO00ItA781783gzI9sjYYM_yfsAU4i-A0WdE-o0VrhznhI2f2aU9z7PZQCri-ql7-s0kpeMvdlE3-FtYCGWhA1uUhtdL8-GjXgSaMPAAlu4SWKF1Fo_XZjR-oGuzGqlDWduN3ArLjDY1KlRw5zBIomwhV8xHF2ienJy-E0p-JFiUsXWSBs4BU-1D3u1SmKjON6vKhh14cvLNF18VITt82WHkUTy6o4UBlGxAvX4Y6w98V4OGm7hYb1e__dZdC0HYqX0F_nn9QbjOZ7jvcGc4Ok6NbDc3X38Y10mSY0f9p_jRpIhMsS6L5nPwqW0lTcKnHKPHUU3UqfsZJ2U9nLFWhve3F_PwhvH_SDw5rhryhHuz_h8zrUYnk8LVih0EmwkDrT8rVCs85PjDpud9YmWf-ytupNXAgAA
I don't know why my test does not show the error in the playground, I thought it did.
I didn't get this to show up with button clicks yet, maybe because our event handlers unset the context before flushing, masking the error.
As to monkey-patching: We can just do
let d = { ...Promise.withResolvers(), rejected: false }
laterThere 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 see. Couldn't we just do it like this though? #16936