-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(web): prevent browser crash from timer leaks, DOM growth, SSE buffer #2433
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
Closed
+167
−2
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| """DOM and timer resource limit tests for issue #2406. | ||
|
|
||
| Verifies that the web UI does not exhaust browser resources during extended | ||
| sessions: DOM node count stays bounded, timers are cleaned up on reconnect, | ||
| and streaming messages survive pruning. | ||
| """ | ||
|
|
||
| from helpers import SEL | ||
|
|
||
|
|
||
| async def _wait_for_connected(page, *, timeout: int = 10000) -> None: | ||
| await page.wait_for_function( | ||
| "() => typeof sseHasConnectedBefore !== 'undefined' && sseHasConnectedBefore === true", | ||
| timeout=timeout, | ||
| ) | ||
|
|
||
|
|
||
| async def test_dom_pruned_after_many_messages(page): | ||
| """DOM stays bounded at MAX_DOM_MESSAGES after many insertions (#2406).""" | ||
| # Inject 250 messages directly (faster than round-tripping through LLM) | ||
| await page.evaluate("""() => { | ||
| for (let i = 0; i < 250; i++) { | ||
| addMessage(i % 2 === 0 ? 'user' : 'assistant', 'Message ' + i); | ||
| } | ||
| pruneOldMessages(); | ||
| }""") | ||
|
|
||
| count = await page.locator(f"{SEL['chat_messages']} .message").count() | ||
| assert count <= 200, f"Expected <= 200 DOM messages after pruning, got {count}" | ||
| assert count >= 100, f"Expected at least 100 DOM messages (not over-pruned), got {count}" | ||
|
|
||
|
|
||
| async def test_no_timer_leak_across_reconnects(page): | ||
| """Reconnect cycles do not accumulate leaked setInterval timers (#2406).""" | ||
| await _wait_for_connected(page, timeout=10000) | ||
|
|
||
| # Instrument: track net interval count via monkeypatching | ||
| await page.evaluate("""() => { | ||
| window.__testIntervalCount = 0; | ||
| const origSet = window.setInterval; | ||
| const origClear = window.clearInterval; | ||
| window.setInterval = function(...args) { | ||
| const id = origSet.apply(this, args); | ||
| window.__testIntervalCount++; | ||
| return id; | ||
| }; | ||
| window.clearInterval = function(id) { | ||
| if (id != null) window.__testIntervalCount--; | ||
| return origClear.call(this, id); | ||
| }; | ||
| }""") | ||
|
|
||
| baseline = await page.evaluate("window.__testIntervalCount") | ||
|
|
||
| # Force 5 reconnect cycles, triggering _streamDebounceTimer each time | ||
| # by dispatching a synthetic stream_chunk event before disconnecting. | ||
| for _ in range(5): | ||
| await page.evaluate("""() => { | ||
| // Trigger the stream_chunk handler which creates _streamDebounceTimer | ||
| const evt = new MessageEvent('stream_chunk', { | ||
| data: JSON.stringify({ content: 'test', thread_id: currentThreadId }) | ||
| }); | ||
| if (eventSource) eventSource.dispatchEvent(evt); | ||
| }""") | ||
| await page.evaluate("if (eventSource) eventSource.close()") | ||
| await page.evaluate("sseHasConnectedBefore = false; connectSSE()") | ||
| await _wait_for_connected(page, timeout=10000) | ||
|
|
||
| after = await page.evaluate("window.__testIntervalCount") | ||
| # Allow at most 1 net new interval (the gateway status poller or similar) | ||
| assert after <= baseline + 1, ( | ||
| f"Interval leak detected: baseline={baseline}, after 5 reconnects={after}" | ||
| ) | ||
|
henrypark133 marked this conversation as resolved.
|
||
|
|
||
|
|
||
| async def test_prune_preserves_streaming_message(page): | ||
| """pruneOldMessages must not remove a message with data-streaming=true (#2406).""" | ||
| # Fill the DOM to just under the cap | ||
| await page.evaluate("""() => { | ||
| for (let i = 0; i < 199; i++) { | ||
| addMessage('assistant', 'msg ' + i); | ||
| } | ||
| }""") | ||
|
|
||
| # Mark the last assistant message as actively streaming | ||
| await page.evaluate("""() => { | ||
| const msgs = document.querySelectorAll('#chat-messages .message.assistant'); | ||
| msgs[msgs.length - 1].setAttribute('data-streaming', 'true'); | ||
| }""") | ||
|
|
||
| # Push over the cap and prune | ||
| await page.evaluate("""() => { | ||
| for (let i = 0; i < 10; i++) { | ||
| addMessage('user', 'overflow ' + i); | ||
| } | ||
| pruneOldMessages(); | ||
| }""") | ||
|
|
||
| streaming_count = await page.locator('[data-streaming="true"]').count() | ||
| assert streaming_count == 1, ( | ||
| f"Streaming message was pruned: expected 1 element with data-streaming, got {streaming_count}" | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.