Skip to content

fix: avoid false-positive infinite loop error #16611

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shaggy-donuts-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: avoid false-positive infinite loop error
20 changes: 19 additions & 1 deletion packages/svelte/src/internal/client/reactivity/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ let last_scheduled_effect = null;
let is_flushing = false;

let is_flushing_sync = false;

/** @type {Map<Effect, number>} */
let effect_execution_count = new Map();

let flush_count = 0;

export class Batch {
/**
* The current values of any sources that are updated in this batch
Expand Down Expand Up @@ -526,7 +532,7 @@ function flush_effects() {
is_flushing = true;

try {
var flush_count = 0;
flush_count = 0;
set_is_updating_effect(true);

while (queued_root_effects.length > 0) {
Expand Down Expand Up @@ -564,6 +570,7 @@ function flush_effects() {
} finally {
is_flushing = false;
set_is_updating_effect(was_updating_effect);
effect_execution_count.clear();

last_scheduled_effect = null;
}
Expand Down Expand Up @@ -626,6 +633,17 @@ function flush_queued_effects(effects) {
current_batch.current.size > n &&
(effect.f & USER_EFFECT) !== 0
) {
var execution_count = (effect_execution_count.get(effect) ?? 0) + 1;
effect_execution_count.set(effect, execution_count);

// If many effects are executed, they cause another flush loop, which could
// lead to false-positives in the infinite loop detection. Therefore decrease
// the counter unless the individual effect has been executed many times, which
// indeed hints at an infinite loop.
if (execution_count < 1000) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this additional measure make sense to prevent a case where effects are constantly created/destroyed because you're playing ping-pong with an if block appearing/disappearing? Dunno if even possible, but maybe?

Suggested change
if (execution_count < 1000) {
if (execution_count < 1000 && effect_execution_count.size < 10_000_000) {

flush_count--;
}

break;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<script>
let a = $state(0);
$effect(() => {
a = 1;
});
</script>

{a}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { test } from '../../test';

export default test({
mode: ['client', 'hydrate'],

compileOptions: {
dev: true
},

html: `1`.repeat(2000)
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import Component from './Component.svelte';
</script>

{#each Array(2000) as _, i}
<Component />
{/each}
Loading