-
Notifications
You must be signed in to change notification settings - Fork 1
fix(wa-sqlite): recover failed OPFS initialization #230
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
6fe0e66
fix(wa-sqlite): recover failed OPFS initialization
marcus-pousette 64ba28f
fix(wa-sqlite): honor OPFS fallback in workers
marcus-pousette 2e31d27
test(wa-sqlite): use browser client entry
e20cd85
fix(wa-sqlite): release shared worker port on drop
99dae78
fix(wa-sqlite): limit OPFS fallback to open failures
8614232
test(wa-sqlite): reuse lifecycle fallback harness
ea3cfec
refactor(wa-sqlite): simplify fallback cleanup
6f93252
refactor(wa-sqlite): simplify fallback error
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@treecrdt/wa-sqlite': patch | ||
| --- | ||
|
|
||
| Honor the requested OPFS fallback policy in worker runtimes, close failed SQLite and OPFS resources, retry allowed memory fallback with a fresh module, and release SharedWorker ports when initialization or teardown fails. |
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 |
|---|---|---|
|
|
@@ -2,6 +2,13 @@ import { test, expect, type Page } from '@playwright/test'; | |
|
|
||
| type LifecycleHarness = NonNullable<Window['__treecrdtLifecycle']>; | ||
| type LifecycleRuntime = 'direct' | 'dedicated-worker' | 'shared-worker'; | ||
| type LifecycleOptions = { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it a duplicate of the same type from
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Goood catch fixed! |
||
| docId: string; | ||
| fallback?: 'memory' | 'throw'; | ||
| filename: string; | ||
| runtime: LifecycleRuntime; | ||
| sharedWorkerName?: string; | ||
| }; | ||
|
|
||
| const scenarios: Array<{ | ||
| runtime: LifecycleRuntime; | ||
|
|
@@ -39,52 +46,42 @@ async function support(page: Page): Promise<ReturnType<LifecycleHarness['support | |
| }); | ||
| } | ||
|
|
||
| async function drop( | ||
| page: Page, | ||
| opts: { docId: string; filename: string; runtime: LifecycleRuntime }, | ||
| ) { | ||
| async function drop(page: Page, opts: LifecycleOptions) { | ||
| await page.evaluate(async (dropOpts) => { | ||
| const harness = window.__treecrdtLifecycle; | ||
| if (!harness) throw new Error('__treecrdtLifecycle not available'); | ||
| await harness.drop(dropOpts); | ||
| }, opts); | ||
| } | ||
|
|
||
| async function write( | ||
| page: Page, | ||
| opts: { | ||
| docId: string; | ||
| filename: string; | ||
| runtime: LifecycleRuntime; | ||
| closeBeforeReload?: boolean; | ||
| }, | ||
| ) { | ||
| async function write(page: Page, opts: LifecycleOptions & { closeBeforeReload?: boolean }) { | ||
| return page.evaluate(async (writeOpts) => { | ||
| const harness = window.__treecrdtLifecycle; | ||
| if (!harness) throw new Error('__treecrdtLifecycle not available'); | ||
| return await harness.write(writeOpts); | ||
| }, opts); | ||
| } | ||
|
|
||
| async function read( | ||
| page: Page, | ||
| opts: { docId: string; filename: string; runtime: LifecycleRuntime }, | ||
| ) { | ||
| async function read(page: Page, opts: LifecycleOptions) { | ||
| return page.evaluate(async (readOpts) => { | ||
| const harness = window.__treecrdtLifecycle; | ||
| if (!harness) throw new Error('__treecrdtLifecycle not available'); | ||
| return await harness.read(readOpts); | ||
| }, opts); | ||
| } | ||
|
|
||
| function expectReloadedTree( | ||
| function expectLifecycleTree( | ||
| state: Awaited<ReturnType<typeof read>>, | ||
| expected: { mode: 'direct' | 'worker'; runtime: LifecycleRuntime }, | ||
| expected: { | ||
| mode: 'direct' | 'worker'; | ||
| runtime: LifecycleRuntime; | ||
| storage?: 'memory' | 'opfs'; | ||
| }, | ||
| ) { | ||
| expect(state).toMatchObject({ | ||
| mode: expected.mode, | ||
| runtime: expected.runtime, | ||
| storage: 'opfs', | ||
| storage: expected.storage ?? 'opfs', | ||
| headLamport: 2, | ||
| parentExists: true, | ||
| childExists: true, | ||
|
|
@@ -125,15 +122,15 @@ test.describe('browser OPFS lifecycle', () => { | |
| ...opts, | ||
| closeBeforeReload: reloadCase.closeBeforeReload, | ||
| }); | ||
| expectReloadedTree(initialState, { | ||
| expectLifecycleTree(initialState, { | ||
| mode: scenario.expectedMode, | ||
| runtime: scenario.runtime, | ||
| }); | ||
|
|
||
| await page.reload({ waitUntil: 'load' }); | ||
| await waitForHarness(page); | ||
|
|
||
| expectReloadedTree(await read(page, opts), { | ||
| expectLifecycleTree(await read(page, opts), { | ||
| mode: scenario.expectedMode, | ||
| runtime: scenario.runtime, | ||
| }); | ||
|
|
@@ -143,4 +140,122 @@ test.describe('browser OPFS lifecycle', () => { | |
| }); | ||
| } | ||
| } | ||
|
|
||
| test('opens a usable dedicated-worker memory fallback after OPFS open fails', async ({ | ||
| page, | ||
| }, testInfo) => { | ||
| if (testInfo.project.name !== 'chromium-dev') test.skip(); | ||
| test.setTimeout(120_000); | ||
| page.on('console', (msg) => console.log(`[page][${msg.type()}] ${msg.text()}`)); | ||
|
|
||
| const suffix = `${Date.now().toString(36)}-${Math.random().toString(16).slice(2, 8)}`; | ||
| const opts: LifecycleOptions = { | ||
| docId: `lifecycle-fallback-${suffix}`, | ||
| fallback: 'memory', | ||
| filename: `/${'x'.repeat(512)}.db`, | ||
| runtime: 'dedicated-worker', | ||
| }; | ||
|
|
||
| await waitForHarness(page); | ||
| const opfsSupport = await support(page); | ||
| if (!opfsSupport.available) test.skip(true, `OPFS unavailable: ${opfsSupport.reason}`); | ||
| expect(new TextEncoder().encode(opts.filename).byteLength).toBeGreaterThan(512); | ||
|
|
||
| expectLifecycleTree(await write(page, { ...opts, closeBeforeReload: true }), { | ||
| mode: 'worker', | ||
| runtime: 'dedicated-worker', | ||
| storage: 'memory', | ||
| }); | ||
| }); | ||
|
|
||
| test('releases a SharedWorker port after failed OPFS initialization', async ({ | ||
| page, | ||
| }, testInfo) => { | ||
| if (testInfo.project.name !== 'chromium-dev') test.skip(); | ||
| test.setTimeout(120_000); | ||
| page.on('console', (msg) => console.log(`[page][${msg.type()}] ${msg.text()}`)); | ||
|
|
||
| const suffix = `${Date.now().toString(36)}-${Math.random().toString(16).slice(2, 8)}`; | ||
| const sharedWorkerName = `lifecycle-recovery-${suffix}`; | ||
| const failedOpts: LifecycleOptions = { | ||
| docId: `lifecycle-recovery-failed-${suffix}`, | ||
| filename: `/${'x'.repeat(512)}.db`, | ||
| runtime: 'shared-worker', | ||
| sharedWorkerName, | ||
| }; | ||
| const firstOpts: LifecycleOptions = { | ||
| docId: `lifecycle-recovery-first-${suffix}`, | ||
| filename: `/lifecycle-recovery-first-${suffix}.db`, | ||
| runtime: 'shared-worker', | ||
| sharedWorkerName, | ||
| }; | ||
| const secondOpts: LifecycleOptions = { | ||
| docId: `lifecycle-recovery-second-${suffix}`, | ||
| filename: `/lifecycle-recovery-second-${suffix}.db`, | ||
| runtime: 'shared-worker', | ||
| sharedWorkerName, | ||
| }; | ||
|
|
||
| await waitForHarness(page); | ||
| const opfsSupport = await support(page); | ||
| if (!opfsSupport.available) test.skip(true, `OPFS unavailable: ${opfsSupport.reason}`); | ||
| expect(new TextEncoder().encode(failedOpts.filename).byteLength).toBeGreaterThan(512); | ||
|
|
||
| try { | ||
| await expect(write(page, failedOpts)).rejects.toThrow(/sqlite3_open_v2|OPFS requested/); | ||
|
|
||
| expectLifecycleTree(await write(page, { ...firstOpts, closeBeforeReload: true }), { | ||
| mode: 'worker', | ||
| runtime: 'shared-worker', | ||
| }); | ||
|
|
||
| expectLifecycleTree(await write(page, { ...secondOpts, closeBeforeReload: true }), { | ||
| mode: 'worker', | ||
| runtime: 'shared-worker', | ||
| }); | ||
| } finally { | ||
| await drop(page, { ...firstOpts, runtime: 'direct' }).catch(() => {}); | ||
| await drop(page, { ...secondOpts, runtime: 'direct' }).catch(() => {}); | ||
| } | ||
| }); | ||
|
|
||
| test('releases a SharedWorker port after drop', async ({ page }, testInfo) => { | ||
| if (testInfo.project.name !== 'chromium-dev') test.skip(); | ||
| test.setTimeout(120_000); | ||
| page.on('console', (msg) => console.log(`[page][${msg.type()}] ${msg.text()}`)); | ||
|
|
||
| const suffix = `${Date.now().toString(36)}-${Math.random().toString(16).slice(2, 8)}`; | ||
| const sharedWorkerName = `lifecycle-drop-${suffix}`; | ||
| const optsFor = (label: string): LifecycleOptions => ({ | ||
| docId: `lifecycle-drop-${label}-${suffix}`, | ||
| filename: `/lifecycle-drop-${label}-${suffix}.db`, | ||
| runtime: 'shared-worker', | ||
| sharedWorkerName, | ||
| }); | ||
| const droppedOpts = optsFor('first'); | ||
| const firstReuseOpts = optsFor('second'); | ||
| const secondReuseOpts = optsFor('third'); | ||
|
|
||
| await waitForHarness(page); | ||
| const opfsSupport = await support(page); | ||
| if (!opfsSupport.available) test.skip(true, `OPFS unavailable: ${opfsSupport.reason}`); | ||
|
|
||
| try { | ||
| await drop(page, droppedOpts); | ||
|
|
||
| expectLifecycleTree(await write(page, { ...firstReuseOpts, closeBeforeReload: true }), { | ||
| mode: 'worker', | ||
| runtime: 'shared-worker', | ||
| }); | ||
|
|
||
| // Closing the only live port must reset the worker so the same name can serve another store. | ||
| expectLifecycleTree(await write(page, { ...secondReuseOpts, closeBeforeReload: true }), { | ||
| mode: 'worker', | ||
| runtime: 'shared-worker', | ||
| }); | ||
| } finally { | ||
| await drop(page, { ...firstReuseOpts, runtime: 'direct' }).catch(() => {}); | ||
| await drop(page, { ...secondReuseOpts, runtime: 'direct' }).catch(() => {}); | ||
| } | ||
| }); | ||
| }); | ||
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
Oops, something went wrong.
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.
Why is it necessary to provide the name of the shared worker?
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.
The explicit name is needed for these regression tests, where want it to be the same name to make sure stale port/session cleanup works. I added a comment