-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
fix(query-core): ignore a retained thenable callback invoked after settling #11128
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
Open
sobol-sudo
wants to merge
3
commits into
TanStack:main
Choose a base branch
from
sobol-sudo:test/query-core-thenable
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+138
−0
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 @@ | ||
| --- | ||
| '@tanstack/query-core': patch | ||
| --- | ||
|
|
||
| Ignore a retained `pendingThenable` settlement callback invoked after the thenable has settled. Holding a reference to `resolve`/`reject` and calling it later used to overwrite `status` and `reason` even though the underlying promise had already settled, leaving the thenable advertising a state that disagreed with its value. |
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,126 @@ | ||
| import { describe, expect, it, vi } from 'vitest' | ||
| import { pendingThenable, tryResolveSync } from '../thenable' | ||
| import type { FulfilledThenable, RejectedThenable } from '../thenable' | ||
|
|
||
| describe('pendingThenable', () => { | ||
| it('should start out pending with resolve and reject attached', () => { | ||
| const thenable = pendingThenable<string>() | ||
|
|
||
| expect(thenable.status).toBe('pending') | ||
| expect(typeof thenable.resolve).toBe('function') | ||
| expect(typeof thenable.reject).toBe('function') | ||
| }) | ||
|
|
||
| it('should expose the value and drop the pending props once resolved', async () => { | ||
| const thenable = pendingThenable<string>() | ||
|
|
||
| thenable.resolve('data') | ||
|
|
||
| expect(thenable.status).toBe('fulfilled') | ||
| expect((thenable as unknown as FulfilledThenable<string>).value).toBe('data') | ||
| expect(thenable.resolve).toBeUndefined() | ||
| expect(thenable.reject).toBeUndefined() | ||
| await expect(thenable).resolves.toBe('data') | ||
| }) | ||
|
|
||
| it('should expose the reason and drop the pending props once rejected', async () => { | ||
| const thenable = pendingThenable<string>() | ||
| const reason = new Error('error') | ||
|
|
||
| thenable.reject(reason) | ||
|
|
||
| expect(thenable.status).toBe('rejected') | ||
| expect((thenable as unknown as RejectedThenable<string>).reason).toBe(reason) | ||
| expect(thenable.resolve).toBeUndefined() | ||
| expect(thenable.reject).toBeUndefined() | ||
| await expect(thenable).rejects.toBe(reason) | ||
| }) | ||
|
|
||
| it('should not reject a second time after being resolved', async () => { | ||
| const thenable = pendingThenable<string>() | ||
|
|
||
| thenable.resolve('data') | ||
| // `reject` is deleted on finalize, so a late caller cannot settle it twice | ||
| expect(thenable.reject).toBeUndefined() | ||
|
|
||
| await expect(thenable).resolves.toBe('data') | ||
| }) | ||
|
|
||
| it('should ignore a retained settlement callback invoked after it settled', async () => { | ||
| const thenable = pendingThenable<string>() | ||
| // a caller can hold on to `reject` before the thenable settles | ||
| const retainedReject = thenable.reject | ||
|
|
||
| thenable.resolve('data') | ||
| retainedReject(new Error('error')) | ||
|
|
||
| expect(thenable.status).toBe('fulfilled') | ||
| expect((thenable as unknown as FulfilledThenable<string>).value).toBe('data') | ||
| expect((thenable as unknown as RejectedThenable<string>).reason).toBeUndefined() | ||
| await expect(thenable).resolves.toBe('data') | ||
| }) | ||
|
|
||
| it('should not report an unhandled rejection when nobody awaits it', async () => { | ||
| const onUnhandledRejection = vi.fn() | ||
| process.on('unhandledRejection', onUnhandledRejection) | ||
|
|
||
| pendingThenable<string>().reject(new Error('error')) | ||
| // unhandled rejections are reported after the microtask queue drains | ||
| await new Promise((resolve) => setTimeout(resolve, 0)) | ||
|
|
||
| process.off('unhandledRejection', onUnhandledRejection) | ||
| expect(onUnhandledRejection).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
|
|
||
| describe('tryResolveSync', () => { | ||
| it('should return undefined for an already resolved native promise', () => { | ||
| // `then` on a native promise always defers to a microtask, so the data is | ||
| // never synchronously available | ||
| expect(tryResolveSync(Promise.resolve('data'))).toBeUndefined() | ||
| }) | ||
|
|
||
| it('should return the data of a thenable that resolves synchronously', () => { | ||
| const thenable = { | ||
| then: (onFulfilled: (value: string) => unknown) => { | ||
| onFulfilled('data') | ||
| return Promise.resolve('data') | ||
| }, | ||
| } as unknown as Promise<string> | ||
|
|
||
| expect(tryResolveSync(thenable)).toEqual({ data: 'data' }) | ||
| }) | ||
|
|
||
| it('should support a synchronous thenable whose then() has no catch', () => { | ||
| // a React thenable is not always a full promise, so `then` may return | ||
| // something without a `catch` method | ||
| const thenable = { | ||
| then: (onFulfilled: (value: string) => unknown) => { | ||
| onFulfilled('data') | ||
| }, | ||
| } as unknown as Promise<string> | ||
|
|
||
| expect(tryResolveSync(thenable)).toEqual({ data: 'data' }) | ||
| }) | ||
|
|
||
| it('should return undefined when a synchronous thenable resolves with undefined', () => { | ||
| const thenable = { | ||
| then: (onFulfilled: (value: undefined) => unknown) => { | ||
| onFulfilled(undefined) | ||
| }, | ||
| } as unknown as Promise<undefined> | ||
|
|
||
| expect(tryResolveSync(thenable)).toBeUndefined() | ||
| }) | ||
|
|
||
| it('should return undefined for a rejected promise without leaving it unhandled', async () => { | ||
| const onUnhandledRejection = vi.fn() | ||
| process.on('unhandledRejection', onUnhandledRejection) | ||
|
|
||
| expect(tryResolveSync(Promise.reject(new Error('error')))).toBeUndefined() | ||
| await new Promise((resolve) => setTimeout(resolve, 0)) | ||
|
|
||
| process.off('unhandledRejection', onUnhandledRejection) | ||
| expect(onUnhandledRejection).not.toHaveBeenCalled() | ||
| }) | ||
| }) | ||
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
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.