Skip to content
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

Svelte 5: Mutating property of passed in object locks up browser #11507

Open
CaptainCodeman opened this issue May 7, 2024 · 13 comments
Open
Assignees

Comments

@CaptainCodeman
Copy link

CaptainCodeman commented May 7, 2024

Describe the bug

Running in Svelte 4 / non-runes mode.

Sorting a child array on an object property passed in triggers reactivity as though the passed in property changed (it's not using bind) resulting in the browser locking up.

Seems to happen if data comes from a SK load fn, and is then passed from the page to a component that changes it (without using bind)

Pausing the code sometimes breaks at the error shown below.

Reproduction

+page.js

export async function load() {
	return {
		folder: {
			name: 'a',
			count: 1,
			children: [
				{ name: 'e', count: 3 },
				{ name: 'c', count: 4 },
				{ name: 'd', count: 2 },
				{ name: 'b', count: 1 },
			],
		},
	}
}

+page.svelte

<script>
	import Component from './Component.svelte'

	export let data

	$: folder = data.folder
</script>

<Component {folder} />

Component.svelte

<script>
	export let folder

	function sortedChildren(folder) {
		return folder.children.sort((a, b) => a.count - b.count)
	}
</script>

<h1>{folder.name}</h1>

<ul>
	{#each sortedChildren(folder) as child}
		<li>{child.name} {child.count}</li>
	{/each}
</ul>

Logs

client.js:299 Uncaught (in promise) Svelte error: effect_update_depth_exceeded
Maximum update depth exceeded. This can happen when a reactive block or effect repeatedly sets a new value. Svelte limits the number of nested updates to prevent infinite loops
    at effect_update_depth_exceeded (http://localhost:5173/node_modules/.vite/deps/chunk-WMXRUQR4.js?v=290ed5a1:137:19)
    at infinite_loop_guard (http://localhost:5173/node_modules/.vite/deps/chunk-WMXRUQR4.js?v=290ed5a1:1197:5)
    at flush_sync (http://localhost:5173/node_modules/.vite/deps/chunk-WMXRUQR4.js?v=290ed5a1:1342:5)
    at flush_sync (http://localhost:5173/node_modules/.vite/deps/chunk-WMXRUQR4.js?v=290ed5a1:1352:7)
    at flush_sync (http://localhost:5173/node_modules/.vite/deps/chunk-WMXRUQR4.js?v=290ed5a1:1352:7)
    at flush_sync (http://localhost:5173/node_modules/.vite/deps/chunk-WMXRUQR4.js?v=290ed5a1:1352:7)
    at flush_sync (http://localhost:5173/node_modules/.vite/deps/chunk-WMXRUQR4.js?v=290ed5a1:1352:7)
    at flush_sync (http://localhost:5173/node_modules/.vite/deps/chunk-WMXRUQR4.js?v=290ed5a1:1352:7)
    at flush_sync (http://localhost:5173/node_modules/.vite/deps/chunk-WMXRUQR4.js?v=290ed5a1:1352:7)
    at flush_sync (http://localhost:5173/node_modules/.vite/deps/chunk-WMXRUQR4.js?v=290ed5a1:1352:7)

System Info

System:
    OS: macOS 14.4.1
    CPU: (6) x64 Intel(R) Core(TM) i5-8500B CPU @ 3.00GHz
    Memory: 284.70 MB / 32.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 22.0.0 - ~/Library/pnpm/node
    npm: 10.5.1 - ~/Library/pnpm/npm
    pnpm: 9.1.0 - ~/Library/pnpm/pnpm
    bun: 1.0.0 - ~/.bun/bin/bun
  Browsers:
    Brave Browser: 123.1.64.113
    Chrome: 124.0.6367.119
    Chrome Canary: 126.0.6465.0
    Safari: 17.4.1
    Safari Technology Preview: 17.4
  npmPackages:
    svelte: ^5.0.0-next.123 => 5.0.0-next.123

Severity

annoyance

@trueadm trueadm self-assigned this May 9, 2024
@Alia5
Copy link

Alia5 commented May 10, 2024

I've ran into this as well

Array.sort sorts the array in place.
Use .toSorted or copy the array before like [...arr].sort(...)

@trueadm
Copy link
Contributor

trueadm commented May 11, 2024

It would be good to get a REPL of this if possible.

@CaptainCodeman
Copy link
Author

@trueadm
Copy link
Contributor

trueadm commented May 11, 2024

Can you make a REPL please?

@CaptainCodeman
Copy link
Author

Sorry, I don't know how to reproduce it in a REPL

@brunnerh
Copy link
Member

brunnerh commented May 11, 2024

The StackBlitz project also does not seem to reproduce the problem?

@gtm-nayan
Copy link
Contributor

You probably need a lot of items to make it obvious,
related: https://discord.com/channels/457912077277855764/1153350350158450758/1233989949498593330

@CaptainCodeman
Copy link
Author

CaptainCodeman commented May 11, 2024

The StackBlitz project also does not seem to reproduce the problem?

Does for me, running in Chrome. It's unrelated to the number of items. In an actual app it's completely unresponsive after this happens.

Open dev tools and refresh the preview, and you should see the error:

Screenshot 2024-05-11 at 9 46 00 AM

@brunnerh
Copy link
Member

You are right, since there are no interactive elements, it is just not directly apparent that anything broke. Wonder why that does not happen in the REPL, though...

@CaptainCodeman
Copy link
Author

Dunno, I tried lots of different combinations and was never able to get it to trigger the same behavior.

@singlyfy
Copy link

I am experiencing similar issue, however, in my case:

  • browser completely freezes and have to xkill it
  • I am using runes and no sorting, but I do have nested structure (parent/child folder relationship)
  • it only happens in DEV mode...running a build & preview doesn't have the issue

I'll try to isolate and extract a simple reproducer

@trueadm
Copy link
Contributor

trueadm commented May 13, 2024

@singlyfy Are you mixing Svelte 4 and Svelte 5 components? It could be that the deep_read logic in Svelte is getting stuck on very deep object structures.

@singlyfy
Copy link

@trueadm : project itself is a mix of v4 and v5, specific component is converted to v5. I'll try to run it in a clean v5 project to see if that makes any difference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants