Repository navigation
refactor(website): adopt the bounded-operation primitive in workshop session state #17645
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
Changes from all commits
8b2a2fe
29edccc
1637def
f2760ad
3c248a6
fbb8684
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,11 +15,13 @@ | |
| import { z } from 'zod' | ||
|
|
||
| import type { User } from 'firebase/auth' | ||
| import { computed, effectScope, shallowRef, watch } from 'vue' | ||
| import type { EffectScope } from 'vue' | ||
| import { computed, shallowRef, watch } from 'vue' | ||
|
|
||
| import type { OperationHandle } from '@comfyorg/account/boundedOperation' | ||
| import { createBoundedOperation } from '@comfyorg/account/boundedOperation' | ||
| import type { SessionSnapshot } from '@comfyorg/account/session' | ||
| import { isPermanentSessionError } from '@comfyorg/account/session' | ||
| import { createLifecycleScope } from '@comfyorg/account/vue' | ||
|
|
||
| import { identifyWorkshopUser, useWorkshopAuthFlag } from '../scripts/posthog' | ||
| import { | ||
|
|
@@ -39,10 +41,9 @@ const PENDING: SessionSnapshot<User> = { | |
| } | ||
|
|
||
| const snapshot = shallowRef<SessionSnapshot<User>>(PENDING) | ||
| let started = false | ||
| let running = false | ||
| let lifecycle: EffectScope | undefined | ||
| let generation = 0 | ||
| const lifecycle = createLifecycleScope() | ||
| const operation = createBoundedOperation() | ||
| let detachIdentity: (() => void) | undefined | ||
| let stopSnapshot: (() => void) | undefined | ||
| let stopTelemetry: (() => void) | undefined | ||
|
|
@@ -168,7 +169,7 @@ async function settleFailedWorkspaceRestore( | |
| async function restoreRememberedWorkspace( | ||
| next: AuthenticatedSnapshot, | ||
| remembered: string, | ||
| restoreGeneration: number | ||
| attempt: OperationHandle | ||
| ): Promise<void> { | ||
| let result: Awaited<ReturnType<typeof workshopSessionClient.remint>> | ||
| try { | ||
|
|
@@ -179,7 +180,7 @@ async function restoreRememberedWorkspace( | |
| } catch { | ||
| return | ||
| } | ||
| if (result?.status !== 'error' || generation !== restoreGeneration) return | ||
| if (result?.status !== 'error' || !attempt.live()) return | ||
| await settleFailedWorkspaceRestore(next, isPermanentSessionError(result.code)) | ||
| } | ||
|
|
||
|
|
@@ -206,14 +207,13 @@ function holdsForRestore(next: SessionSnapshot<User>): boolean { | |
| restoredForUid = uid | ||
| const remembered = rememberedWorkspace(uid) | ||
| if (!remembered || remembered === workspace.id) return false | ||
| const restoreGeneration = generation | ||
| void restoreRememberedWorkspace(next, remembered, restoreGeneration) | ||
| void restoreRememberedWorkspace(next, remembered, operation.capture()) | ||
| return true | ||
| } | ||
|
|
||
| async function begin(expectedGeneration: number): Promise<void> { | ||
| async function begin(attempt: OperationHandle): Promise<void> { | ||
| const firebase = await import('./workshop-firebase') | ||
| if (generation !== expectedGeneration) return | ||
| if (!attempt.live()) return | ||
|
|
||
| running = true | ||
| stopSnapshot = workshopSessionClient.subscribe((next) => { | ||
|
|
@@ -237,34 +237,30 @@ async function begin(expectedGeneration: number): Promise<void> { | |
| } | ||
|
|
||
| function start(): void { | ||
| if (started || typeof window === 'undefined') return | ||
| started = true | ||
| // This detached scope gives the module singleton its own lifetime instead | ||
| // of binding its watcher to whichever component calls this first. | ||
| lifecycle = effectScope(true) | ||
| const enabled = useWorkshopAuthFlag() | ||
| lifecycle.run(() => { | ||
| lifecycle.start(() => { | ||
| const enabled = useWorkshopAuthFlag() | ||
| watch( | ||
| enabled, | ||
| (on) => { | ||
| if (on && running) return | ||
| const expectedGeneration = ++generation | ||
| operation.abandon() | ||
| const attempt = operation.capture() | ||
| stopListeners() | ||
| snapshot.value = PENDING | ||
| if (!on) { | ||
| restoredForUid = undefined | ||
| workshopSessionClient.clearStoredCredential() | ||
| return | ||
| } | ||
| void begin(expectedGeneration).catch((error: unknown) => { | ||
| if (generation !== expectedGeneration) return | ||
| void begin(attempt).catch((error: unknown) => { | ||
| if (!attempt.live()) return | ||
| console.error('Workshop auth initialization failed', error) | ||
| // Nothing half-installed survives, and the latch opens again so | ||
| // the next caller retries instead of waiting out the timeout. | ||
| // Nothing half-installed survives: abandon invalidates a restore | ||
| // captured before begin threw, and the latch reopens so the next | ||
| // caller retries instead of waiting out the timeout. | ||
| operation.abandon() | ||
| stopListeners() | ||
| lifecycle?.stop() | ||
| lifecycle = undefined | ||
| started = false | ||
| lifecycle.stop() | ||
|
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. suggestion (non-blocking): this is the one teardown path that does not abandon the operation, so it cannot enforce the invariant the new test pins.
To be clear, this is pre-existing and not a regression from this PR: the old catch did not bump stopListeners()
operation.abandon()
lifecycle.stop()Entirely reasonable to leave for a follow-up if you would rather keep this PR strictly behavior-preserving — that is a fair call for a refactor stack. Would you prefer that?
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. Fixed: the error-catch teardown now calls operation.abandon() before stopListeners/lifecycle.stop, so a restore captured synchronously inside begin is invalidated too, matching the flag-off path. |
||
| }) | ||
| }, | ||
| { immediate: true } | ||
|
|
||
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.
suggestion (non-blocking): this
operation.abandon()is the one line in the PR that is new behavior rather than a rename, and nothing asserts it.Everything else here maps one-for-one onto the old generation counter, but the old catch did not bump the generation — it only did
stopListeners()/lifecycle.stop()/started = false. So the case this now covers is real and previously leaked:begininstallssubscribe(...), the client publishes an authenticated snapshot synchronously,holdsForRestorecaptures a handle, and then a later step inbegin(attachIdentity,subscribeAuthRefreshTelemetry) throws — leaving that restore live against a lifecycle that is being torn down.workshop-session-state.failure.test.tscovers the throw path for the latch-and-cleanup half, but it does not have a remembered workspace in play, so it never captures a restore and would pass with this line deleted.Given the parsimonious-testing convention I would not ask for a whole new file — but if the existing failure test can be extended to seed
workshop:workspaceand assert the in-flight restore stays unpublished, that would make this line mutation-killing instead of trusting the comment above it.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.
Extended the existing failure test in 0f55f1c rather than adding a file. The new case seeds
workshop:workspacewith a differing id, makessubscribeemit an authenticated snapshot synchronously soholdsForRestorecaptures a restore handle mid-begin, then throws fromattachIdentity. A gated re-mint settles only after the catch runs, and the test asserts the held boot session never publishes (sessionstays undefined). Verified it is mutation-killing: deleting the catch'soperation.abandon()turns it red (the settled re-mint republishes the held snapshot), restoring it turns it green.