-
-
Notifications
You must be signed in to change notification settings - Fork 618
feat(sse): invoke finalize on response stream end
#2741
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
kettanaito
wants to merge
4
commits into
main
Choose a base branch
from
fix/sse-finalize
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+223
−50
Draft
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
04eee2e
fix(sse): invoke `finalize` on response stream end
kettanaito f5bbf27
Merge branch 'main' into fix/sse-finalize
kettanaito 5404c93
Merge branch 'main' into fix/sse-finalize
kettanaito 82e4879
fix: permissive `text/event-stream` check
kettanaito 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
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,127 @@ | ||
| import type { sse } from 'msw' | ||
| import type { setupWorker } from 'msw/browser' | ||
| import { DeferredPromise } from '@open-draft/deferred-promise' | ||
| import { test, expect } from '../playwright.extend' | ||
|
|
||
| declare namespace window { | ||
| export const msw: { | ||
| setupWorker: typeof setupWorker | ||
| sse: typeof sse | ||
| } | ||
| } | ||
|
|
||
| const EXAMPLE_URL = new URL('./sse.mocks.ts', import.meta.url) | ||
|
|
||
| test('runs cleanup after the event source is closed by the client', async ({ | ||
| loadExample, | ||
| page, | ||
| }) => { | ||
| await loadExample(EXAMPLE_URL, { | ||
| skipActivation: true, | ||
| }) | ||
|
|
||
| const finalizedAt = new DeferredPromise<number>() | ||
| await page.exposeFunction('notifyFinalized', () => { | ||
| finalizedAt.resolve(Date.now()) | ||
| }) | ||
|
|
||
| await page.evaluate(async () => { | ||
| const { setupWorker, sse } = window.msw | ||
|
|
||
| const worker = setupWorker( | ||
| sse('http://localhost/stream', ({ finalize }) => { | ||
| finalize(() => window.notifyFinalized()) | ||
| }), | ||
| ) | ||
| await worker.start() | ||
| }) | ||
|
|
||
| const closedAt = await page.evaluate(() => { | ||
| const source = new EventSource('http://localhost/stream') | ||
|
|
||
| return new Promise<number>((resolve) => { | ||
| source.addEventListener('open', () => { | ||
| source.close() | ||
| resolve(Date.now()) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| await expect(finalizedAt).resolves.toBeGreaterThanOrEqual(closedAt) | ||
| }) | ||
|
|
||
| test('runs cleanup after the event source is closed by the handler', async ({ | ||
| loadExample, | ||
| page, | ||
| }) => { | ||
| await loadExample(EXAMPLE_URL, { | ||
| skipActivation: true, | ||
| }) | ||
|
|
||
| const finalizedAt = new DeferredPromise<number>() | ||
| await page.exposeFunction('notifyFinalized', () => { | ||
| finalizedAt.resolve(Date.now()) | ||
| }) | ||
|
|
||
| await page.evaluate(async () => { | ||
| const { setupWorker, sse } = window.msw | ||
|
|
||
| const worker = setupWorker( | ||
| sse('http://localhost/stream', ({ client, finalize }) => { | ||
| setTimeout(() => client.close(), 250) | ||
| finalize(() => window.notifyFinalized()) | ||
| }), | ||
| ) | ||
| await worker.start() | ||
| }) | ||
|
|
||
| const closedAt = await page.evaluate(() => { | ||
| const source = new EventSource('http://localhost/stream') | ||
|
|
||
| return new Promise<number>((resolve) => { | ||
| source.addEventListener('open', () => { | ||
| resolve(Date.now()) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| await expect(finalizedAt).resolves.toBeGreaterThanOrEqual(closedAt) | ||
| }) | ||
|
|
||
| test('runs cleanup after the event source is errored by the handler', async ({ | ||
| loadExample, | ||
| page, | ||
| }) => { | ||
| await loadExample(EXAMPLE_URL, { | ||
| skipActivation: true, | ||
| }) | ||
|
|
||
| const finalizedAt = new DeferredPromise<number>() | ||
| await page.exposeFunction('notifyFinalized', () => { | ||
| finalizedAt.resolve(Date.now()) | ||
| }) | ||
|
|
||
| await page.evaluate(async () => { | ||
| const { setupWorker, sse } = window.msw | ||
|
|
||
| const worker = setupWorker( | ||
| sse('http://localhost/stream', ({ client, finalize }) => { | ||
| setTimeout(() => client.error(), 250) | ||
| finalize(() => window.notifyFinalized()) | ||
| }), | ||
| ) | ||
| await worker.start() | ||
| }) | ||
|
|
||
| const closedAt = await page.evaluate(() => { | ||
| const source = new EventSource('http://localhost/stream') | ||
|
|
||
| return new Promise<number>((resolve) => { | ||
| source.addEventListener('open', () => { | ||
| resolve(Date.now()) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| await expect(finalizedAt).resolves.toBeGreaterThanOrEqual(closedAt) | ||
| }) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: mswjs/msw
Length of output: 3767
🏁 Script executed:
Repository: mswjs/msw
Length of output: 895
🏁 Script executed:
Repository: mswjs/msw
Length of output: 1799
🏁 Script executed:
Repository: mswjs/msw
Length of output: 971
🏁 Script executed:
Repository: mswjs/msw
Length of output: 1183
🏁 Script executed:
Repository: mswjs/msw
Length of output: 174
🏁 Script executed:
Repository: mswjs/msw
Length of output: 35
Cleanup wiring on the shared emitter cross-fires across connections and has a startup race.
Two correctness issues stem from registering cleanups on the per-handler
#emitter:Multi-connection cross-fire.
#emitteris shared by every connection that thissse(...)handler accepts. When connection A closes, the emitter emitscloseonce and fires everyonce('close', onClose)listener registered so far — including theonClosecaptured for connection B's cleanups. B'sclearInterval(or other per-connection cleanup from the issue#2630example) will run while B is still streaming, dropping its background work.Resolver-synchronous close race. If a resolver synchronously calls
client.close()after registeringfinalize(callback), the event fires beforeexhaustCleanupsattaches listeners. The cleanup will never run because the'close'event is emitted synchronously (line 336) while the resolver is still executing, beforerunScheduledCleanups→exhaustCleanupscan attach the listener (line 210).The cleanup binding must be per-connection. One option is to track close state on the client (the
#closeddeferred already exists) and pass a per-connection emitter or close-promise into the resolver context, runningsuper.exhaustCleanups(cleanups)when that promise resolves — handling both "already closed" (run immediately) and "closes later" (await) cases.🤖 Prompt for AI Agents