Skip to content

Reproduce HTMLRewriter stack overflow using JS code + quick fix - #3758

Merged
npaun merged 2 commits into
mainfrom
npaun/htmlrewriter-alt-stack-overflow
Mar 20, 2025
Merged

npaun merged 2 commits into
mainfrom
npaun/htmlrewriter-alt-stack-overflow

Conversation

@npaun

@npaun npaun commented Mar 19, 2025

Copy link
Copy Markdown
Member

This PR adds two tests that cause a stack overflow in workerd:

  • stackOverflow1: A single replacement function generates a lot of output bytes
  • stackOverflow2: The replacement function runs many times

I've also implemented the idea we came up with to limit the depth of the promise chain: waiting on writePromise after every thunk call. As expected, this fixes stackOverflow2 but not stackOverflow1. Therefore I commented out stackOverflow1 for now.

I'd like to merge this quick fix to see if the sentry errors go away, but it's not a full fix. The next step would be to implement an internal buffer in Rewriter and flush that to the inner (output sink) after each thunk call.

There's also another potential situation where a ReadableStream is used to provide the replacement. In another PR, we could implement back pressure to avoid too much data from being buffered at once.

@npaun
npaun requested a review from harrishancock March 19, 2025 22:57
@npaun
npaun requested review from a team as code owners March 19, 2025 22:58
@npaun
npaun requested a review from tewaro March 19, 2025 22:58
@npaun npaun changed the title Reproduce HTMLRewriter stack overflow using JS code Reproduce HTMLRewriter stack overflow using JS code + quick fix Mar 19, 2025
@npaun
npaun merged commit 29e3762 into main Mar 20, 2025
@npaun
npaun deleted the npaun/htmlrewriter-alt-stack-overflow branch March 20, 2025 17:27
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