Skip to content

fix(query-core): use !== undefined for timer ID checks to handle falsy 0#10402

Closed
ljjunh wants to merge 2 commits intoTanStack:mainfrom
ljjunh:fix/falsy-timer-id-zero-not-cleared
Closed

fix(query-core): use !== undefined for timer ID checks to handle falsy 0#10402
ljjunh wants to merge 2 commits intoTanStack:mainfrom
ljjunh:fix/falsy-timer-id-zero-not-cleared

Conversation

@ljjunh
Copy link
Copy Markdown

@ljjunh ljjunh commented Apr 6, 2026

🎯 Changes

Fixes #10395

Timer IDs can legally be 0 when using a custom timeout provider. The current code uses truthy checks (if (this.#gcTimeout)) before calling clearTimeout/clearInterval, which silently skips cleanup when the ID is 0 since
0 is falsy in JavaScript.

Three locations were affected:

File Method Field
removable.ts clearGcTimeout() #gcTimeout
queryObserver.ts #clearStaleTimeout() #staleTimeoutId
queryObserver.ts #clearRefetchInterval() #refetchIntervalId

Replace truthy checks with explicit !== undefined comparisons:

// Before                                                                                                               
if (this.#gcTimeout) { ... }
                                                                                                                        
// After                                                  
if (this.#gcTimeout !== undefined) { ... }

##  Checklist

- [x] I have followed the steps in the [Contributing guide](https://github.com/TanStack/query/blob/main/CONTRIBUTING.md).
- [x] I have tested this code locally with `pnpm run test:pr`.

## 🚀 Release Impact

- [x] This change affects published code, and I have generated a [changeset](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md).
- [ ] This change is docs/CI/dev-only (no release).


<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
## Summary by CodeRabbit

* **Bug Fixes**
* Fixed timeout clearing mechanism to properly handle falsy timer identifiers (e.g., ID `0`) during cleanup operations.

* **Tests**
* Added test coverage for falsy timer ID handling in query observation and interval management.
* Added test suite for garbage collection timer cleanup with falsy timer IDs.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 6, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 925f7886-e80a-4faf-a11a-391a213c29d2

📥 Commits

Reviewing files that changed from the base of the PR and between ef887c9 and 3450e7d.

📒 Files selected for processing (1)
  • packages/query-core/src/__tests__/queryObserver.test.tsx

📝 Walkthrough

Walkthrough

Adds test coverage and a changeset entry for a bug fix addressing incorrect timer cleanup when custom timeout providers return falsy timer IDs (e.g., 0). The fix changes truthy checks to explicit undefined comparisons, ensuring falsy timer IDs are properly cleared during cleanup operations.

Changes

Cohort / File(s) Summary
Changeset Entry
.changeset/seven-facts-unite.md
Documents patch release with fix for clearGcTimeout using !== undefined check instead of truthy check to handle falsy timer IDs correctly.
QueryObserver Tests
packages/query-core/src/__tests__/queryObserver.test.tsx
Adds two tests verifying falsy timer ID (0) handling: one for setOptions clearing timeout with ID 0, another for unsubscribe clearing interval with ID 0.
Garbage Collection Timer Tests
packages/query-core/src/__tests__/removable.test.tsx
New test file verifying that garbage-collection timeout with falsy ID (0) is properly cleared when adding observers after subscription/unsubscription cycle.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🐰 A timer that's zero won't hide anymore,
We check what is undefined, not what's falsy before,
With tests that verify each cleanup flow,
Those missing clears will surely go! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: replacing truthy timer checks with explicit !== undefined comparisons to handle falsy timer ID 0.
Description check ✅ Passed The description follows the template with all required sections completed: changes clearly described with context and affected locations, checklist items checked, and release impact noted.
Linked Issues check ✅ Passed All code changes address the requirements in issue #10395: replacements made in removable.ts, queryObserver.ts (both clearStaleTimeout and clearRefetchInterval), comprehensive tests added, and changeset created.
Out of Scope Changes check ✅ Passed All changes are directly in scope: timer ID comparisons fixed in three locations, corresponding tests added to verify falsy ID 0 handling, and changeset documentation created.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
.changeset/seven-facts-unite.md (1)

5-6: Changeset description is incomplete.

The description only mentions clearGcTimeout, but the fix also applies to #clearStaleTimeout and #clearRefetchInterval in queryObserver.ts. Consider updating for accurate release notes.

📝 Suggested changeset description
-fix: use !== undefined instead of truthy check for timer IDs to correctly handle falsy value 0 in clearGcTimeout,  
+fix: use !== undefined instead of truthy check for timer IDs to correctly handle falsy value 0 in clearGcTimeout, clearStaleTimeout, and clearRefetchInterval
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.changeset/seven-facts-unite.md around lines 5 - 6, Update the changeset
description in .changeset/seven-facts-unite.md to accurately reflect that the
fix replaces truthy checks with explicit !== undefined checks not only in
clearGcTimeout but also in `#clearStaleTimeout` and `#clearRefetchInterval` inside
queryObserver.ts; mention all three methods so release notes correctly state
that timer IDs with falsy value 0 are now handled properly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In @.changeset/seven-facts-unite.md:
- Around line 5-6: Update the changeset description in
.changeset/seven-facts-unite.md to accurately reflect that the fix replaces
truthy checks with explicit !== undefined checks not only in clearGcTimeout but
also in `#clearStaleTimeout` and `#clearRefetchInterval` inside queryObserver.ts;
mention all three methods so release notes correctly state that timer IDs with
falsy value 0 are now handled properly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 87fe1f68-ef84-4589-817e-46877d8104b1

📥 Commits

Reviewing files that changed from the base of the PR and between eac62b8 and ef887c9.

📒 Files selected for processing (5)
  • .changeset/seven-facts-unite.md
  • packages/query-core/src/__tests__/queryObserver.test.tsx
  • packages/query-core/src/__tests__/removable.test.tsx
  • packages/query-core/src/queryObserver.ts
  • packages/query-core/src/removable.ts

@TkDodo
Copy link
Copy Markdown
Collaborator

TkDodo commented Apr 9, 2026

i merged the other PR that did this.

@TkDodo TkDodo closed this Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Falsy timer ID (0) prevents proper timeout/interval cleanup with custom timeout provider

2 participants