-
-
Notifications
You must be signed in to change notification settings - Fork 474
fix: share in-flight payload envelope import promise to prevent sync spin loop #9501
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
nflaig
merged 2 commits into
ChainSafe:unstable
from
barnabasbusa:fix/payload-envelope-import-dedupe-spin
Jul 4, 2026
+108
−17
Merged
Changes from all commits
Commits
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
91 changes: 91 additions & 0 deletions
91
packages/beacon-node/test/unit/chain/blocks/payloadEnvelopeProcessor.test.ts
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,91 @@ | ||
| import {afterEach, beforeEach, describe, expect, it, vi} from "vitest"; | ||
| import {processExecutionPayload} from "../../../../src/chain/blocks/importExecutionPayload.js"; | ||
| import {PayloadEnvelopeProcessor} from "../../../../src/chain/blocks/payloadEnvelopeProcessor.js"; | ||
| import type {BeaconChain} from "../../../../src/chain/chain.js"; | ||
| import {PayloadEnvelopeInput} from "../../../../src/chain/seenCache/seenPayloadEnvelopeInput.js"; | ||
|
|
||
| vi.mock("../../../../src/chain/blocks/importExecutionPayload.js"); | ||
|
|
||
| describe("chain / blocks / PayloadEnvelopeProcessor", () => { | ||
| let processor: PayloadEnvelopeProcessor; | ||
| let abortController: AbortController; | ||
| let payloadInput: PayloadEnvelopeInput; | ||
|
|
||
| beforeEach(() => { | ||
| abortController = new AbortController(); | ||
| processor = new PayloadEnvelopeProcessor({} as BeaconChain, null, abortController.signal); | ||
| // processExecutionPayload is mocked, the input is only used as an identity key | ||
| payloadInput = {} as PayloadEnvelopeInput; | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| abortController.abort(); | ||
| vi.resetAllMocks(); | ||
| }); | ||
|
|
||
| it("should run the import once for duplicate calls and resolve all callers on completion", async () => { | ||
| let resolveImport: () => void = () => {}; | ||
| vi.mocked(processExecutionPayload).mockReturnValue( | ||
| new Promise((resolve) => { | ||
| resolveImport = resolve; | ||
| }) | ||
| ); | ||
|
|
||
| let firstResolved = false; | ||
| let secondResolved = false; | ||
| const first = processor.processPayloadEnvelopeJob(payloadInput).then(() => { | ||
| firstResolved = true; | ||
| }); | ||
| const second = processor.processPayloadEnvelopeJob(payloadInput).then(() => { | ||
| secondResolved = true; | ||
| }); | ||
|
|
||
| // Duplicate calls must not resolve before the import completes, else callers like | ||
| // BlockInputSync treat a still-pending payload as processed and re-queue it in a tight loop | ||
| await new Promise((resolve) => setTimeout(resolve, 0)); | ||
| expect(firstResolved).toBe(false); | ||
| expect(secondResolved).toBe(false); | ||
| expect(processExecutionPayload).toHaveBeenCalledTimes(1); | ||
|
|
||
| resolveImport(); | ||
| await Promise.all([first, second]); | ||
| expect(firstResolved).toBe(true); | ||
| expect(secondResolved).toBe(true); | ||
| expect(processExecutionPayload).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("should propagate import failure to deduped callers", async () => { | ||
| let rejectImport: (e: Error) => void = () => {}; | ||
| vi.mocked(processExecutionPayload).mockReturnValue( | ||
| new Promise((_resolve, reject) => { | ||
| rejectImport = reject; | ||
| }) | ||
| ); | ||
|
|
||
| const first = processor.processPayloadEnvelopeJob(payloadInput); | ||
| const second = processor.processPayloadEnvelopeJob(payloadInput); | ||
|
|
||
| rejectImport(new Error("IMPORT_FAILED")); | ||
| await expect(first).rejects.toThrow("IMPORT_FAILED"); | ||
| await expect(second).rejects.toThrow("IMPORT_FAILED"); | ||
| expect(processExecutionPayload).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it("should retry the import after a failed attempt", async () => { | ||
| vi.mocked(processExecutionPayload).mockRejectedValueOnce(new Error("IMPORT_FAILED")); | ||
| vi.mocked(processExecutionPayload).mockResolvedValueOnce(undefined); | ||
|
|
||
| await expect(processor.processPayloadEnvelopeJob(payloadInput)).rejects.toThrow("IMPORT_FAILED"); | ||
|
|
||
| await processor.processPayloadEnvelopeJob(payloadInput); | ||
| expect(processExecutionPayload).toHaveBeenCalledTimes(2); | ||
| }); | ||
|
|
||
| it("should not re-run the import after success", async () => { | ||
| vi.mocked(processExecutionPayload).mockResolvedValue(undefined); | ||
|
|
||
| await processor.processPayloadEnvelopeJob(payloadInput); | ||
| await processor.processPayloadEnvelopeJob(payloadInput); | ||
| expect(processExecutionPayload).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
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.
To prevent potential race conditions in future refactorings (for example, if a timeout or manual cancellation mechanism is introduced that deletes or overwrites the promise in
this.imports), it is safer to defensively check that the promise being deleted is indeed the one that was created by this invocation.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.
Sensible defensive pattern but not a current correctness issue. Walked the call sites: the only
imports.set(payloadInput, importPromise)is at line 51 right afterjobQueue.push, and the onlyimports.delete(payloadInput)is the catch at line 58 of the same call that wrote it — so any deletion necessarily targets the same promise this call created. There's no path that overwrites the entry mid-flight (no timeout, no manual cancellation, no concurrentset).The identity check would only matter if a future refactor introduced one of those overwrite paths, which is exactly the future-proofing case you flagged. Happy to take it as a one-line follow-up or fold it into this PR — your call. Either way, the dedupe semantics are correct as-is. Approved in
pullrequestreview-4468399794.