fix(editor): honest stopped state when the draft is unsaved; no dead retry
0279953 chose the stopped message from CoreRuntime.stopped alone: it
promised the report was kept even when the tab's storage refused the
draft (export, the banner's advice, needs the core), four surfaces still
offered a retry that could not work, and a failed recovery below the cap
was a dead end since nothing called retry().
The runtime restarts a failed recovery on the next call (compiled module
reused, cap still counts traps) and notifies status listeners. One
CoreStoppedNotice on the editor, Home, New report and the full-page
notice: reload when saved, else core.stopped_unsaved with a core-free
copy download (also on the storage-full banner) and a beforeunload
guard. Export, import and new report say the stopped message; a core
failure reading an unread draft is FAILED, not held. ESLint census of
the failure keys; core.stopped says "plusieurs erreurs".
This commit is contained in:
1 parent
0279953e9e
commit
6371523f8c
24 files changed
+839
-83
No files matched your search
@@ -13,6 +13,8 @@ import {
|
||||
* a fresh one is created from the compiled module kept in memory and a glue module record
|
||||
* imported in advance (a trap needs no network). Calls made while it is re-created answer
|
||||
* `core_unavailable`; after `maxRecoveries` traps the page stays unavailable until reload.
|
||||
* A recovery or restart that fails below the cap is not final: the next call or `ready()`
|
||||
* restarts the runtime (a restart is not a recovery and does not touch the counter).
|
||||
*/
|
||||
|
||||
/** What the wasm-bindgen glue (`--target web`) exports. */
|
||||
@@ -60,6 +62,7 @@ export class CoreRuntime {
|
||||
private imports = 0
|
||||
private recoveries = 0
|
||||
private readonly maxRecoveries: number
|
||||
private readonly listeners = new Set<() => void>()
|
||||
/** Increments with every instance; sheet handles of an older generation are dropped. */
|
||||
generation = 0
|
||||
|
||||
@@ -82,9 +85,30 @@ export class CoreRuntime {
|
||||
return this.state === 'unavailable' && this.recoveries >= this.maxRecoveries
|
||||
}
|
||||
|
||||
/**
|
||||
* Unavailable after an instance already ran (a failed recovery or restart) with
|
||||
* recoveries left: the next call may bring it back. A failed FIRST load is not restarted
|
||||
* implicitly, only by `retry()`.
|
||||
*/
|
||||
private get restartable(): boolean {
|
||||
return this.state === 'unavailable' && !this.stopped && this.generation > 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls `listener` (after a microtask, never inside a load or recovery) each time the
|
||||
* status changes; returns the unsubscribe function.
|
||||
*/
|
||||
subscribe(listener: () => void): () => void {
|
||||
this.listeners.add(listener)
|
||||
return () => {
|
||||
this.listeners.delete(listener)
|
||||
}
|
||||
}
|
||||
|
||||
/** Loads the module on first use; true when calls can be made. */
|
||||
ready(): Promise<boolean> {
|
||||
if (this.state === 'ready') return Promise.resolve(true)
|
||||
if (this.restartable) return this.retry()
|
||||
if (this.state === 'unavailable') return Promise.resolve(false)
|
||||
this.pending ??= this.load()
|
||||
return this.pending
|
||||
@@ -94,12 +118,18 @@ export class CoreRuntime {
|
||||
retry(): Promise<boolean> {
|
||||
if (this.state !== 'unavailable') return this.ready()
|
||||
if (this.recoveries >= this.maxRecoveries) return Promise.resolve(false)
|
||||
this.state = 'idle'
|
||||
this.compiled = null
|
||||
this.setState('idle')
|
||||
// Only a failed first load clears the compiled module: once an instance ran, it is sound.
|
||||
if (this.generation === 0) this.compiled = null
|
||||
this.pending = null
|
||||
return this.ready()
|
||||
}
|
||||
|
||||
/** Starts a restart when the next call may succeed (the current call still answers). */
|
||||
kickRestart(): void {
|
||||
if (this.restartable) void this.retry()
|
||||
}
|
||||
|
||||
/** Runs a free operation. */
|
||||
call<T>(op: string, input: string): CoreOutcome<T> {
|
||||
return this.invoke((glue) => parseOutcome<T>(glue.call(op, input)))
|
||||
@@ -116,7 +146,10 @@ export class CoreRuntime {
|
||||
*/
|
||||
invoke<T>(fn: (glue: GlueModule) => CoreOutcome<T>): CoreOutcome<T> {
|
||||
const glue = this.glue
|
||||
if (this.state !== 'ready' || !glue) return CORE_UNAVAILABLE
|
||||
if (this.state !== 'ready' || !glue) {
|
||||
this.kickRestart()
|
||||
return CORE_UNAVAILABLE
|
||||
}
|
||||
try {
|
||||
return fn(glue)
|
||||
} catch (error) {
|
||||
@@ -126,12 +159,12 @@ export class CoreRuntime {
|
||||
}
|
||||
|
||||
private async load(): Promise<boolean> {
|
||||
this.state = 'loading'
|
||||
this.setState('loading')
|
||||
try {
|
||||
await this.instantiate(await this.source.importGlue(this.nextImport()))
|
||||
return true
|
||||
} catch {
|
||||
this.state = 'unavailable'
|
||||
this.setState('unavailable')
|
||||
this.pending = null
|
||||
return false
|
||||
}
|
||||
@@ -142,12 +175,28 @@ export class CoreRuntime {
|
||||
await glue.default({ module_or_path: await this.compiled })
|
||||
this.glue = glue
|
||||
this.generation += 1
|
||||
this.state = 'ready'
|
||||
this.setState('ready')
|
||||
this.spare = this.source
|
||||
.importGlue(this.nextImport())
|
||||
.catch(() => null)
|
||||
}
|
||||
|
||||
private setState(next: CoreStatus): void {
|
||||
if (next === this.state) return
|
||||
this.state = next
|
||||
queueMicrotask(() => this.notify())
|
||||
}
|
||||
|
||||
private notify(): void {
|
||||
for (const listener of [...this.listeners]) {
|
||||
try {
|
||||
listener()
|
||||
} catch {
|
||||
// A listener never changes the state machine.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private nextImport(): number {
|
||||
this.imports += 1
|
||||
return this.imports
|
||||
@@ -156,11 +205,11 @@ export class CoreRuntime {
|
||||
private poison(): void {
|
||||
this.glue = null
|
||||
if (this.recoveries >= this.maxRecoveries) {
|
||||
this.state = 'unavailable'
|
||||
this.setState('unavailable')
|
||||
return
|
||||
}
|
||||
this.recoveries += 1
|
||||
this.state = 'recovering'
|
||||
this.setState('recovering')
|
||||
this.pending = this.recover()
|
||||
}
|
||||
|
||||
@@ -172,7 +221,7 @@ export class CoreRuntime {
|
||||
)
|
||||
return true
|
||||
} catch {
|
||||
this.state = 'unavailable'
|
||||
this.setState('unavailable')
|
||||
return false
|
||||
} finally {
|
||||
this.pending = null
|
||||
@@ -220,9 +269,9 @@ export class SheetSession {
|
||||
/** The handle of the live instance; a handle of a dropped instance is never called. */
|
||||
private current(): CoreOutcome<GlueSheetHandle> {
|
||||
if (this.handle && this.handleGeneration === this.runtime.generation) {
|
||||
return this.runtime.status === 'ready'
|
||||
? { ok: true, value: this.handle }
|
||||
: CORE_UNAVAILABLE
|
||||
if (this.runtime.status === 'ready') return { ok: true, value: this.handle }
|
||||
this.runtime.kickRestart()
|
||||
return CORE_UNAVAILABLE
|
||||
}
|
||||
this.handle = null
|
||||
return this.runtime.invoke((glue) => {
|
||||
|
||||
@@ -47,6 +47,38 @@ function countingSource(offlineAfter = Infinity) {
|
||||
return source
|
||||
}
|
||||
|
||||
/**
|
||||
* Counts compiles and imports; the numbered glue imports in `failing` are refused like a
|
||||
* lost network (import 1 is the first instance, 2 its spare, 3 the recovery's fallback).
|
||||
*/
|
||||
function flakySource(failing: number[]) {
|
||||
const inner = nodeCoreSource('pkg-proofs')
|
||||
const source = {
|
||||
compiles: 0,
|
||||
imports: 0,
|
||||
compile: () => {
|
||||
source.compiles += 1
|
||||
return inner.compile()
|
||||
},
|
||||
importGlue: async (n: number): Promise<GlueModule> => {
|
||||
source.imports += 1
|
||||
if (failing.includes(source.imports)) throw new TypeError('offline')
|
||||
return inner.importGlue(n)
|
||||
},
|
||||
}
|
||||
return source
|
||||
}
|
||||
|
||||
/**
|
||||
* A handle of a trapped instance is freed by the glue's finalizer inside the dead module,
|
||||
* which throws out of the garbage collector at a random later time. Tests that trap with a
|
||||
* live handle keep it reachable until the process ends.
|
||||
*/
|
||||
const retainedHandles: unknown[] = []
|
||||
function retainHandle(sheet: object): void {
|
||||
retainedHandles.push((sheet as { handle: unknown }).handle)
|
||||
}
|
||||
|
||||
/** Traps the live instance through the proofs build (stack exhaustion). */
|
||||
function trap(runtime: CoreRuntime) {
|
||||
return runtime.invoke((glue) => {
|
||||
@@ -91,6 +123,7 @@ describe('core runtime', () => {
|
||||
ok: true,
|
||||
value: { ref: { sheetId: 'sheet', versionId: null } },
|
||||
})
|
||||
retainHandle(sheet)
|
||||
trap(runtime)
|
||||
expect(sheet.call('index_data', '')).toMatchObject({
|
||||
kind: 'core_unavailable',
|
||||
@@ -180,4 +213,100 @@ describe('core runtime', () => {
|
||||
expect(runtime.sheet(BUNDLE).open()).toMatchObject({ ok: false, kind: 'internal' })
|
||||
expect(runtime.status).not.toBe('ready')
|
||||
})
|
||||
|
||||
it('allows three recoveries by default, then stops on the fourth trap', async () => {
|
||||
const runtime = new CoreRuntime(countingSource())
|
||||
await runtime.ready()
|
||||
for (let i = 0; i < 3; i += 1) {
|
||||
trap(runtime)
|
||||
expect(await runtime.ready()).toBe(true)
|
||||
expect(runtime.stopped).toBe(false)
|
||||
}
|
||||
trap(runtime)
|
||||
expect(runtime.stopped).toBe(true)
|
||||
expect(await runtime.retry()).toBe(false)
|
||||
})
|
||||
|
||||
it('restarts on the next call after a failed recovery below the cap', async () => {
|
||||
const source = flakySource([2, 3])
|
||||
const runtime = new CoreRuntime(source)
|
||||
await runtime.ready()
|
||||
trap(runtime)
|
||||
expect(await runtime.ready()).toBe(false)
|
||||
expect(runtime.stopped).toBe(false)
|
||||
expect(runtime.ready()).toBeInstanceOf(Promise)
|
||||
expect(await runtime.ready()).toBe(true)
|
||||
expect(runtime.call('core_version', '')).toMatchObject({ ok: true })
|
||||
// A restart is not a recovery, and it reuses the compiled module.
|
||||
expect(source.compiles).toBe(1)
|
||||
})
|
||||
|
||||
it('kicks the restart from a call and answers the next one', async () => {
|
||||
const runtime = new CoreRuntime(flakySource([2, 3]))
|
||||
await runtime.ready()
|
||||
const sheet = runtime.sheet(BUNDLE)
|
||||
trap(runtime)
|
||||
expect(await runtime.ready()).toBe(false)
|
||||
expect(runtime.call('core_version', '')).toMatchObject({ kind: 'core_unavailable' })
|
||||
expect(runtime.status).toBe('loading')
|
||||
expect(await runtime.ready()).toBe(true)
|
||||
expect(sheet.call('index_data', '')).toMatchObject({ ok: true })
|
||||
})
|
||||
|
||||
it('restarts from a sheet call as well', async () => {
|
||||
const runtime = new CoreRuntime(flakySource([2, 3]))
|
||||
await runtime.ready()
|
||||
const sheet = runtime.sheet(BUNDLE)
|
||||
expect(sheet.open()).toEqual({ ok: true, value: null })
|
||||
retainHandle(sheet)
|
||||
trap(runtime)
|
||||
await runtime.ready()
|
||||
expect(sheet.call('index_data', '')).toMatchObject({ kind: 'core_unavailable' })
|
||||
expect(runtime.status).toBe('loading')
|
||||
await runtime.ready()
|
||||
expect(sheet.call('index_data', '')).toMatchObject({ ok: true })
|
||||
})
|
||||
|
||||
it('does not restart once the cap is spent', async () => {
|
||||
const source = flakySource([2, 3])
|
||||
const runtime = new CoreRuntime(source, { maxRecoveries: 1 })
|
||||
await runtime.ready()
|
||||
trap(runtime)
|
||||
expect(await runtime.ready()).toBe(false)
|
||||
expect(runtime.stopped).toBe(true)
|
||||
const imports = source.imports
|
||||
expect(runtime.call('core_version', '')).toMatchObject({ kind: 'core_unavailable' })
|
||||
expect(await runtime.ready()).toBe(false)
|
||||
expect(runtime.status).toBe('unavailable')
|
||||
expect(source.imports).toBe(imports)
|
||||
})
|
||||
|
||||
it('does not restart a failed first load from a call', async () => {
|
||||
const source = flakySource([1])
|
||||
const runtime = new CoreRuntime(source)
|
||||
expect(await runtime.ready()).toBe(false)
|
||||
expect(runtime.call('core_version', '')).toMatchObject({ kind: 'core_unavailable' })
|
||||
expect(await runtime.ready()).toBe(false)
|
||||
expect(source.imports).toBe(1)
|
||||
expect(await runtime.retry()).toBe(true)
|
||||
})
|
||||
|
||||
it('notifies listeners after a microtask and survives a throwing one', async () => {
|
||||
const runtime = new CoreRuntime(countingSource(), { maxRecoveries: 0 })
|
||||
await runtime.ready()
|
||||
const calls: boolean[] = []
|
||||
runtime.subscribe(() => {
|
||||
throw new Error('listener bug')
|
||||
})
|
||||
const unsubscribe = runtime.subscribe(() => calls.push(runtime.stopped))
|
||||
trap(runtime)
|
||||
expect(calls).toEqual([])
|
||||
await Promise.resolve()
|
||||
expect(calls).toEqual([true])
|
||||
expect(runtime.stopped).toBe(true)
|
||||
unsubscribe()
|
||||
await runtime.retry()
|
||||
await Promise.resolve()
|
||||
expect(calls).toEqual([true])
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user