fix: rethrow error of failed iterable after calling `return()`

The fix in #17966 wasn't quite right, because we gotta rethrow in case the iterator stopped because of an error.
Fixes part of the SvelteKit `query.live` test failure.
pull/18169/head
Simon Holthausen 2 months ago
parent dc5bd887b5
commit 9ada3dc178

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: rethrow error of failed iterable after calling `return()`

@ -215,6 +215,9 @@ export async function* for_await_track_reactivity_loss(iterable) {
/** Whether the completion of the iterator was "normal", meaning it wasn't ended via `break` or a similar method */
let normal_completion = false;
const no_error = {}; // because a madman might throw `null` or `undefined`
/** @type {any} */
let thrown_error = no_error;
try {
while (true) {
const { done, value } = (await track_reactivity_loss(iterator.next()))();
@ -226,11 +229,29 @@ export async function* for_await_track_reactivity_loss(iterable) {
yield value;
set_reactivity_loss_tracker(prev);
}
} catch (error) {
thrown_error = error;
} finally {
// If the iterator had an abrupt completion and `return` is defined on the iterator, call it and return the value
if (!normal_completion && iterator.return !== undefined) {
// eslint-disable-next-line no-unsafe-finally
return /** @type {TReturn} */ ((await track_reactivity_loss(iterator.return()))().value);
try {
// Spec says a non-normal completion is break, return, or throw, in which case iterator.return() should be called...
const result = (await track_reactivity_loss(iterator.return()))();
// ... but only in case of break or return the result is returned, otherwise error takes precedence and is (re)thrown
if (thrown_error === no_error) {
// eslint-disable-next-line no-unsafe-finally
return /** @type {TReturn} */ (result.value);
}
} catch (error) {
if (thrown_error === no_error) {
throw error;
}
}
}
if (thrown_error !== no_error) {
throw thrown_error;
}
}
}

@ -0,0 +1,24 @@
import { tick } from 'svelte';
import { test } from '../../test';
import { normalise_trace_logs } from '../../../helpers.js';
export default test({
compileOptions: {
dev: true
},
html: '<p>pending</p>',
async test({ assert, target, warnings }) {
await tick();
assert.htmlEqual(
target.innerHTML,
'<h1>number -> number -> number -> return -> body failed -> ended</h1>'
);
assert.deepEqual(normalise_trace_logs(warnings), [
{
log: 'Detected reactivity loss when reading `values[1]`. This happens when state is read in an async function after an earlier `await`'
}
]);
}
});

@ -0,0 +1,45 @@
<script>
let values = $state([0, 1, 2]);
async function get_result() {
const logs = [];
const iterator = {
index: 0,
async next() {
if (this.index >= values.length) return { done: true };
return { done: false, value: values[this.index++] };
},
async return() {
logs.push('return');
throw new Error('return failed');
},
[Symbol.asyncIterator]() {
return this;
}
};
try {
for await (const value of iterator) {
logs.push('number');
// Read reactive state after async iterator await.
if (values.length === 3 && value === 2) {
throw new Error('body failed');
}
}
} catch (error) {
logs.push(error.message);
}
logs.push('ended');
return logs.join(' -> ');
}
</script>
<svelte:boundary>
<h1>{await get_result()}</h1>
{#snippet pending()}
<p>pending</p>
{/snippet}
</svelte:boundary>
Loading…
Cancel
Save