-
Notifications
You must be signed in to change notification settings - Fork 17
fix(ensindexer): hot-reload safe writer worker #1928
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
Closed
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
73689cc
fix(ensindexer): make EnsDbWriterWorker hot-reload safe
shrugs 31604d9
add changeset
shrugs d80e3d4
fix(ensindexer): address PR review feedback
shrugs 1617314
refactor(ensindexer): cleanups from simplify pass
shrugs 46b4f4d
fix(ensindexer): tighten singleton catch path and widen shutdown call…
shrugs 7bb950f
fix(ensindexer): cancel in-progress run on stop, fail-fast on real wo…
shrugs f57ae1d
fix(ensindexer): treat superseded worker as intentional stop in catch…
shrugs 23070cf
docs(ensindexer): explain DOMException AbortError choice in checkCanc…
shrugs cb59db0
refactor(ensindexer): replace localPonderContext Proxy with explicit …
shrugs 8702d0d
docs(ensindexer): document AbortError DOMException in run() JSDoc
shrugs 60b269e
fix(ensindexer): clear singleton before awaiting stop, add cancellati…
shrugs 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,6 @@ | ||
| --- | ||
| "ensindexer": patch | ||
| "@ensnode/ponder-sdk": patch | ||
| --- | ||
|
|
||
| ENSIndexer in dev mode no longer crashes during hot reloading due to EnsDbWriterWorker failure. | ||
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
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 |
|---|---|---|
| @@ -1,14 +1,85 @@ | ||
| import { deserializePonderAppContext, type PonderAppContext } from "@ensnode/ponder-sdk"; | ||
| import { | ||
| deserializePonderAppContext, | ||
| isPonderAppShutdownManager, | ||
| type PonderAppContext, | ||
| type PonderAppShutdownManager, | ||
| } from "@ensnode/ponder-sdk"; | ||
|
|
||
| /** | ||
| * Local Ponder Context — reactive wrapper over Ponder's runtime globals. | ||
| * | ||
| * Why this is a Proxy and not an eagerly-deserialized object: | ||
| * | ||
| * Ponder's dev mode hot-reloads the API entry file by re-executing it via | ||
| * vite-node. On every indexing-file change, Ponder ALSO kills and replaces | ||
| * `common.shutdown` and `common.apiShutdown` on `globalThis.PONDER_COMMON` | ||
| * (see `ponder/src/bin/commands/dev.ts:95-101`). Modules in our API-side | ||
| * dependency graph (this file included) are NOT re-evaluated when only an | ||
| * indexing file changes — vite-node only invalidates the changed file's | ||
| * dep tree. So any value cached in a module-level closure during the | ||
| * original boot becomes stale on the very next reload. | ||
| * | ||
| * Stable fields (`command`, `localPonderAppUrl`, `logger`) are validated | ||
| * once and memoized — Ponder does not mutate `options` or `logger` on | ||
| * reload. Reload-scoped fields (`apiShutdown`, `shutdown`) MUST be re-read | ||
| * from `globalThis.PONDER_COMMON` on every access. | ||
| * | ||
| * Contract for callers: NEVER cache reload-scoped fields in a module-level | ||
| * closure or capture them in a constructor argument. Always reach for them | ||
| * via `localPonderContext.<field>` from code that runs per-reload (e.g. the | ||
| * API entry file or per-request handlers). If you need an `AbortSignal` | ||
| * across calls, store a getter (`() => localPonderContext.apiShutdown | ||
| * .abortController.signal`), not the signal itself. | ||
| */ | ||
|
|
||
| if (!globalThis.PONDER_COMMON) { | ||
| throw new Error("PONDER_COMMON must be defined by Ponder at runtime as a global variable."); | ||
| } | ||
|
|
||
| function readShutdownManager(field: "apiShutdown" | "shutdown"): PonderAppShutdownManager { | ||
| const raw = (globalThis.PONDER_COMMON as Record<string, unknown> | undefined)?.[field]; | ||
| if (!isPonderAppShutdownManager(raw)) { | ||
| throw new Error(`globalThis.PONDER_COMMON.${field} is not a valid Ponder shutdown manager.`); | ||
| } | ||
| return raw; | ||
| } | ||
|
|
||
| let cachedStableContext: PonderAppContext | undefined; | ||
| function getStableContext(): PonderAppContext { | ||
| if (!cachedStableContext) { | ||
| if (!globalThis.PONDER_COMMON) { | ||
| throw new Error("PONDER_COMMON must be defined by Ponder at runtime as a global variable."); | ||
| } | ||
| cachedStableContext = deserializePonderAppContext(globalThis.PONDER_COMMON); | ||
| } | ||
| return cachedStableContext; | ||
| } | ||
|
|
||
| /** | ||
| * Local Ponder app context | ||
| * Local Ponder Context. | ||
| * | ||
| * Represents the {@link PonderAppContext} object provided by Ponder runtime to | ||
| * the local Ponder app. Useful for accessing internal Ponder app configuration | ||
| * and utilities such as the logger. | ||
| * Combines stable {@link PonderAppContext} fields with reload-scoped | ||
| * shutdown managers. See module-level comment for the staleness contract. | ||
| */ | ||
| export const localPonderContext = deserializePonderAppContext(globalThis.PONDER_COMMON); | ||
| export interface LocalPonderContext extends PonderAppContext { | ||
| /** | ||
| * The current `apiShutdown` manager. RELOAD-SCOPED — identity changes | ||
| * every API hot-reload. Always read fresh; never cache. | ||
| */ | ||
| readonly apiShutdown: PonderAppShutdownManager; | ||
|
|
||
| /** | ||
| * The current `shutdown` manager. RELOAD-SCOPED — identity changes | ||
| * every indexing hot-reload. Always read fresh; never cache. | ||
| */ | ||
| readonly shutdown: PonderAppShutdownManager; | ||
| } | ||
|
|
||
| export const localPonderContext: LocalPonderContext = new Proxy({} as LocalPonderContext, { | ||
| get(_target, prop) { | ||
| if (typeof prop === "symbol") return undefined; | ||
| if (prop === "apiShutdown") return readShutdownManager("apiShutdown"); | ||
| if (prop === "shutdown") return readShutdownManager("shutdown"); | ||
| return getStableContext()[prop as keyof PonderAppContext]; | ||
| }, | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.