Skip to content

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

Open
ljjunh wants to merge 1 commit intoTanStack:mainfrom
ljjunh:fix/falsy-timer-id-zero-not-cleared
Open

fix(query-core): use !== undefined for timer ID checks to handle falsy 0#10402
ljjunh wants to merge 1 commit 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 timer cleanup logic to correctly handle falsy timer IDs during garbage collection and timeout clearing operations.

* **Tests**
* Added test coverage for edge cases involving falsy timer IDs in cleanup behaviors.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 6, 2026

📝 Walkthrough

Walkthrough

This PR fixes a bug where falsy timer IDs (e.g., 0) returned by custom timeout providers were not being cleared due to reliance on truthy checks. The fix replaces truthy checks with explicit !== undefined checks in timer cleanup logic and adds comprehensive test coverage for the edge case.

Changes

Cohort / File(s) Summary
Changeset
.changeset/seven-facts-unite.md
Added patch release entry documenting the fix for falsy timer ID handling in clearGcTimeout.
Timer Cleanup Logic
packages/query-core/src/queryObserver.ts, packages/query-core/src/removable.ts
Updated #staleTimeoutId, #refetchIntervalId, and #gcTimeout clearing to use explicit !== undefined checks instead of truthy checks, ensuring timer IDs of 0 are properly cleared.
Test Coverage
packages/query-core/src/__tests__/queryObserver.test.tsx, packages/query-core/src/__tests__/removable.test.tsx
Added two new test suites validating that cleanup functions are called with falsy timer IDs (0) when triggered by option updates, unsubscription, and garbage collection lifecycle events.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 A zero once hiding from sight,
Now clears with explicit might,
No falsy tricks block the way,
Timers clean at end of day,
Query observers skip and play! 🎪

🚥 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

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

1 participant