Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

fix: stop flattenError at a cyclic error cause - #393

Open
ofri-peretz wants to merge 2 commits into
EventSource:mainfrom
ofri-peretz:fix/cyclic-error-cause
Open

ofri-peretz wants to merge 2 commits into
EventSource:mainfrom
ofri-peretz:fix/cyclic-error-cause

Conversation

@ofri-peretz

Copy link
Copy Markdown

flattenError follows err.cause (and err.errors) with no stop. If fetch() rejects with an error whose cause chain loops back on itself, it throws RangeError: Maximum call stack size exceeded inside the failure handler. The rejection goes unhandled, no error event fires, and the reconnect is never scheduled.

The fix passes a set of errors already flattened through the recursion and stops at one it has seen. It also stops passing map's index as a second argument. There's a new test with a fetch that rejects with a cyclic cause. It fails with the RangeError before the change, and the whole suite passes after.

I found this with no-unbounded-cause-recursion from eslint-plugin-reliability, a plugin I maintain. The patch was prepared with an AI assistant (Claude Code). I reviewed it and ran the tests.

@rexxars rexxars left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could we keep seen local to each branch? With the shared set, two errors that share a cause get treated as a cycle, even when there isn't one:

const cause = new Error('ECONNREFUSED')
const error = new AggregateError([
  new Error('IPv6', {cause}),
  new Error('IPv4', {cause}),
])

This previously produced Error: IPv6: ECONNREFUSED, Error: IPv4: ECONNREFUSED, but now produces Error: IPv6: ECONNREFUSED, IPv4. The cause is still in the overall message, but we lose its connection to the second error.

Replacing seen.add(err) with this would preserve that information while still stopping cycles:

seen = new Set(seen).add(err)

We can also drop the 'cause' in err check. Reading a missing cause gives undefined, which the instanceof check already handles:

if (err.cause instanceof Error && !seen.has(err.cause)) {
  return `${err}: ${flattenError(err.cause, seen)}`
}

Those two changes together came out slightly smaller than the current patch when minifying and compressing the full bundle with Terser and gzip/Brotli, though we're talking about a few bytes.

Would be good to cover the shared-cause case, a self-cause, and a cycle through AggregateError.errors too. The current test closes after the first error event, so it doesn't verify the reconnect mentioned in the changeset. Could we have it wait for another fetch attempt as well?

Siblings in an AggregateError now get their own copy of the set, so two
errors with a common cause both print it. Drops the redundant 'cause' in err
check, and tests a self-cause, an AggregateError cycle, a shared cause, and
the reconnect after a cyclic-cause error.
@ofri-peretz

ofri-peretz commented Nov 30, 2026 •

Copy link
Copy Markdown
Author

Thanks @rexxars, all four done in a76fe1a:

  • seen is now copied per branch (seen = new Set(seen).add(err)), so siblings no longer share it. Your example now gives Error: IPv6: ECONNREFUSED, Error: IPv4: ECONNREFUSED again. Against the previous commit the new test fails with exactly the Error: IPv6: ECONNREFUSED, IPv4 you saw.
  • Dropped the 'cause' in err check.
  • New tests: an error that is its own cause (boom), an AggregateError whose errors contains itself (IPv4, connect failed), and the shared-cause case.
  • The cyclic-cause test now waits for a second error event and checks that fetch was called twice, so it covers the reconnect (about 3 s, the default interval).

npm run lint passes, and vitest run test/client.test.ts gives 79 passed, 1 skipped.

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

Successfully merging this pull request may close these issues.

2 participants