Skip to content

fix(core): Resume polling from controller.subscribe() after the store pauses - #4639

Open
ntucker wants to merge 7 commits into
masterfrom
claude/subscription-manager-resume
Open

ntucker wants to merge 7 commits into
masterfrom
claude/subscription-manager-resume

Conversation

@ntucker

@ntucker ntucker commented Nov 8, 2026 •

Copy link
Copy Markdown
Collaborator

Requested by Nathaniel · project thread

Motivation

Bug fix. An app polls an endpoint with controller.subscribe(), with no component that owns the subscription, and an <Activity> above DataProvider hides it and later shows it again.

SubscriptionManager.cleanup() stops every PollingSubscription but keeps tracking them. It has no init(), so when the store resumes, polling stays stopped until the app unsubscribes. useSubscription() already recovers, because the hidden component unsubscribes and then subscribes again on show ("resumes polling after showing" in activity.tsx). Found in a review of reactive/native-binance#47.

Solution

  • Subscription gets an optional init() that resumes work cleanup() stopped and does nothing while the subscription is running. Because it is optional, custom Subscriptions keep compiling and keep their old behavior.
  • SubscriptionManager.init() calls init() on every subscription it tracks. A subscription created while the store is paused is stopped at once, if it has init(), and the next init() starts it. A subscription without init() keeps running, as before, because nothing could resume it. This matters because a remounted component subscribes before DataProvider resumes the Managers.
  • PollingSubscription implements init(). Its public cleanup() now marks it stopped, while going offline still only clears timers. run() does nothing while stopped, so adding a faster frequency while paused cannot restart polling.
  • When SubscriptionManager.middleware() binds a different controller, it cleans up and drops the subscriptions it tracks, and it passes on actions from the old controller untouched, like NetworkManager. This happens when a remounted DataProvider reuses the same Managers. Without it, a controller.subscribe() that outlived the old store would resume against the old controller and take in the new store's SUBSCRIBE for the same key, and a late UNSUBSCRIBE from the old store could stop the new store's polling.
  • DevToolsManager.cleanup() keeps its polling counts. SubscriptionManager.init() resumes a poll without a new SUBSCRIBE, so clearing the counts would stop capping that key in __DC__.actions.
  • The v0.19 blog post lists the fix under Client > Loading and error states > Other changes.
class StreamSubscription implements Subscription {
  cleanup() {
    this.socket?.close();
    this.socket = undefined;
  }
  init() {
    this.socket ??= this.connect();
  }
}

Assumptions and validation

  • High: the new React test "resumes polling from controller.subscribe() after showing" fails on master and passes here.
  • High: these new tests fail without the matching change and pass with it: "polls for the new store after controller.subscribe() outlived the old one" (React), "drops subscriptions from a previous store when bound to a new one" and "ignores actions from a previous store" (unit).
  • High: new unit tests cover these cases. PollingSubscription.init() resumes polling, does nothing while already running, ignores add() while stopped, and waits for the connection to come online when resumed offline. SubscriptionManager resumes every tracked subscription, stops one with init() created while paused, and keeps one without init() running. DevTools keeps capping a polling key after cleanup() and init().
  • High: after the rebase on master, yarn jest --selectProjects ReactDOM Node --testPathPatterns 'packages/(core|react)' gives 759 passed. tsc -p packages/core passes.
  • Medium: StrictMode is not affected in practice. Its double Effects run when everything mounts, before any imperative subscribe. I did not test React Native or Vue, but they share SubscriptionManager.

Goals impact

Goal Impact
Bundle size (CI size check, gzip) +188 B total: rdcClient.js +96 B, rdcNextjs.js +92 B
Runtime One reference comparison per action in SubscriptionManager, the same as NetworkManager. Other new code runs only on subscribe, pause, resume and store setup. The React benchmark shows only run-to-run spread.
Cache misses None. Memo code is not touched.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Y5C5Z9ziZLXvEhvQd1y4Yx

@changeset-bot

changeset-bot Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 81d34df

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 7 packages
Name Type
@data-client/core Patch
@data-client/react Patch
@data-client/vue Patch
example-benchmark Patch
example-benchmark-react Patch
test-bundlesize Patch
coinbase-lite Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ntucker ntucker self-assigned this Nov 8, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T17:42:18.198529Z 81d34df New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@github-actions

github-actions Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Docs preview: https://docs-site-pr-4639-data-client.vercel.app

Built from 81d34df merged into master (this build). Locales: en (the merge queue builds every locale).

@github-actions

github-actions Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Size Change: +188 B (+0.19%)

Total Size: 97.7 kB

📦 View Changed
Filename Size Change
examples/test-bundlesize/dist/rdcClient.js 9.71 kB +96 B (+1%)
examples/test-bundlesize/dist/rdcNextjs.js 11.3 kB +92 B (+0.82%)
ℹ️ View Unchanged
Filename Size
examples/test-bundlesize/dist/App.js 1.25 kB
examples/test-bundlesize/dist/polyfill.js 244 B
examples/test-bundlesize/dist/rdcEndpoint.js 7.47 kB
examples/test-bundlesize/dist/rdcPipeableStream.js 8.65 kB
examples/test-bundlesize/dist/react.js 58.5 kB
examples/test-bundlesize/dist/webpack-runtime.js 577 B

compressed-size-action

@github-actions github-actions Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Benchmark React

Details
Benchmark suite Current: 81d34df Previous: 008d874 Ratio
data-client: getlist-100 436.21 ops/s (± 7.1%) 305.11 ops/s (± 3.5%) 0.70
data-client: getlist-500 122.74 ops/s (± 3.6%) 83.65 ops/s (± 3.6%) 0.68
data-client: update-entity 766.28 ops/s (± 1.9%) 719.42 ops/s (± 1.5%) 0.94
data-client: update-user 947.87 ops/s (± 6.3%) 597.01 ops/s (± 3.1%) 0.63
data-client: getlist-500-sorted 119.05 ops/s (± 4.0%) 76.02 ops/s (± 2.9%) 0.64
data-client: update-entity-sorted 1095.9 ops/s (± 4.9%) 499.38 ops/s (± 2.4%) 0.46
data-client: update-entity-multi-view 860.22 ops/s (± 6.7%) 634.92 ops/s (± 2.9%) 0.74
data-client: list-detail-switch-10 33.15 ops/s (± 1.4%) 16.26 ops/s (± 5.8%) 0.49
data-client: update-user-10000 140.01 ops/s (± 2.9%) 134.05 ops/s (± 2.7%) 0.96
data-client: invalidate-and-resolve 133.24 ops/s (± 2.3%) 93.72 ops/s (± 3.1%) 0.70
data-client: update-ticker-stream 19.9 ops/s (± 2.3%) 12.54 ops/s (± 1.6%) 0.63
data-client: unshift-item 483.68 ops/s (± 4.4%) 371.75 ops/s (± 1.7%) 0.77
data-client: delete-item 811.52 ops/s (± 6.6%) 608.83 ops/s (± 0.9%) 0.75
data-client: move-item 422.83 ops/s (± 4.3%) 263.35 ops/s (± 7.2%) 0.62

This comment was automatically generated by workflow using github-action-benchmark.

@codecov

codecov Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.64%. Comparing base (881814e) to head (81d34df).
⚠️ Report is 5 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4639   +/-   ##
=======================================
  Coverage   98.63%   98.64%           
=======================================
  Files         163      163           
  Lines        3161     3183   +22     
  Branches      627      633    +6     
=======================================
+ Hits         3118     3140   +22     
  Misses          9        9           
  Partials       34       34           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 611d02510d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

ntucker commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author

Staff engineer (Cursor agent):

Resuming from SubscriptionManager.init() is the right seam. It matches #4593 (cleanup is a pause, initManager calls init(state)) and the way stream Managers reopen counted keys from init(). The subscription.init guard in d92ea54 correctly keeps custom Subscriptions without init() running when they're created while paused (the Codex P2): useSubscription subscribes in a child effect, before DataStore runs manager.init().

One remaining change (dev-only, caused by this PR): packages/core/src/manager/DevtoolsManager.ts cleanup() still runs this.pollingSubscribers.clear(), with the comment "SubscriptionManager.cleanup() stops polling without dispatching unsubscribes". After this PR that is no longer true. SubscriptionManager.init() resumes a controller.subscribe() poll without dispatching SUBSCRIBE, so DevTools has forgotten the key. logAction then stops capping it to maxPollingActions, and every poll's FETCH and SET_RESPONSE pushes other entries out of the 100-entry __DC__.actions log. Delete the clear and its comment so counts survive a pause the way SubscriptionManager's subscriptions do. Hook subscriptions still balance: hide and unmount run the manager cleanup before the hooks' UNSUBSCRIBE (React disconnects parent before child), and show re-SUBSCRIBEs. Then flip __tests__/devtoolsManager.ts "forgets polling keys on cleanup" to assert that the key stays capped after cleanup → init.

Follow-up (after merge, not blocking): a PollingSubscription keeps the controller it was built with. With shared managers (managers={() => managers}, the "remounted with the same managers" case), an unbalanced controller.subscribe() that outlives a DataProvider unmount stays tracked. The next store's init() now resumes it against the unmounted store's controller, and a SUBSCRIBE for the same key from the new store add()s to it, so the new store gets no polling. Before this PR the same subscription stayed stopped and the new store didn't poll either. Consider cleaning up and dropping tracked subscriptions when middleware() binds a different controller.

Validated: cleanup()/logAction/trackPolling in DevtoolsManager.ts, SubscriptionManager.init() and handleSubscribe (the subscription.init guard), and PollingSubscription.init()/update() at d92ea54; React disconnectPassiveEffect (parent before child) and reconnectPassiveEffects (children first); the existing DevTools test "forgets polling keys on cleanup". Inferred: how much this affects real DevTools logs (the Activity tests run with DevTools disabled), and the shared-manager follow-up path, which I did not run.

@github-actions github-actions Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Benchmark

Details
Benchmark suite Current: 81d34df Previous: 008d874 Ratio
normalizeLong 731 ops/sec (±1.69%) 761 ops/sec (±0.27%) 1.04
normalizeLong Values 627 ops/sec (±0.58%) 634 ops/sec (±1.83%) 1.01
normalizeLong Scalar 570 ops/sec (±2.72%) 564 ops/sec (±3.33%) 0.99
normalizeLong Scalar update 1666 ops/sec (±0.81%) 1639 ops/sec (±2.14%) 0.98
denormalizeLong 331 ops/sec (±6.07%) 354 ops/sec (±7.14%) 1.07
denormalizeLong Values 297 ops/sec (±5.22%) 340 ops/sec (±4.48%) 1.14
denormalizeLong donotcache 1779 ops/sec (±1.85%) 1815 ops/sec (±0.58%) 1.02
denormalizeLong nested 25% missing 357 ops/sec (±4.70%) 364 ops/sec (±5.01%) 1.02
denormalizeLong nested 25% null 363 ops/sec (±4.61%) 387 ops/sec (±4.52%) 1.07
denormalizeLong nested 25% INVALID 371 ops/sec (±4.44%) 414 ops/sec (±4.59%) 1.12
denormalizeLong Values donotcache 1198 ops/sec (±4.38%) 1282 ops/sec (±1.28%) 1.07
denormalizeLong Scalar donotcache 1834 ops/sec (±1.82%) 1823 ops/sec (±1.31%) 0.99
denormalizeShort donotcache 500x 2530 ops/sec (±0.49%) 2282 ops/sec (±0.68%) 0.90
denormalizeShort 500x 929 ops/sec (±6.04%) 896 ops/sec (±6.84%) 0.96
denormalizeShort 500x withCache 9474 ops/sec (±1.72%) 9285 ops/sec (±0.69%) 0.98
queryShort 500x withCache 5224 ops/sec (±1.77%) 4892 ops/sec (±1.40%) 0.94
buildQueryKey All 84612 ops/sec (±2.94%) 85419 ops/sec (±1.99%) 1.01
query All withCache 9416 ops/sec (±6.53%) 9394 ops/sec (±4.83%) 1.00
denormalizeLong with mixin Entity 301 ops/sec (±7.19%) 327 ops/sec (±7.11%) 1.09
denormalizeLong withCache 13399 ops/sec (±1.18%) 12214 ops/sec (±1.16%) 0.91
denormalizeLong withCache (Scalar churn) 13349 ops/sec (±1.02%) 12183 ops/sec (±0.94%) 0.91
denormalizeLong nested 25% missing withCache 11425 ops/sec (±0.77%) 11489 ops/sec (±1.48%) 1.01
denormalizeLong nested 25% null withCache 12189 ops/sec (±0.37%) 12254 ops/sec (±0.51%) 1.01
denormalizeLong nested 25% INVALID withCache 11718 ops/sec (±1.00%) 11203 ops/sec (±0.82%) 0.96
denormalizeLong Values withCache 9185 ops/sec (±0.69%) 9073 ops/sec (±0.50%) 0.99
denormalizeLong Scalar withCache 8164 ops/sec (±1.13%) 9379 ops/sec (±1.03%) 1.15
denormalizeLong Scalar update withCache 5646 ops/sec (±1.41%) 5803 ops/sec (±0.38%) 1.03
denormalizeLong All withCache 11956 ops/sec (±0.45%) 11183 ops/sec (±1.07%) 0.94
denormalizeLong Query-sorted withCache 10101 ops/sec (±6.18%) 9669 ops/sec (±5.12%) 0.96
denormalizeLongAndShort withEntityCacheOnly 2800 ops/sec (±1.63%) 2750 ops/sec (±1.43%) 0.98
denormalize bidirectional 50 6527 ops/sec (±10.95%) 6450 ops/sec (±9.70%) 0.99
denormalize bidirectional 50 donotcache 68819 ops/sec (±3.26%) 69728 ops/sec (±1.21%) 1.01
getResponse 6709 ops/sec (±6.09%) 6970 ops/sec (±5.54%) 1.04
getResponse (null) 18460003 ops/sec (±1.58%) 18475732 ops/sec (±1.22%) 1.00
getResponse (clear cache) 271 ops/sec (±10.80%) 295 ops/sec (±9.89%) 1.09
getSmallResponse 5413 ops/sec (±1.26%) 5846 ops/sec (±1.31%) 1.08
getSmallInferredResponse 4563 ops/sec (±0.54%) 4277 ops/sec (±0.46%) 0.94
getResponse Collection 7379 ops/sec (±4.77%) 6933 ops/sec (±3.39%) 0.94
get Collection 5266 ops/sec (±1.28%) 5276 ops/sec (±1.09%) 1.00
get Query-sorted 8472 ops/sec (±3.79%) 7971 ops/sec (±3.67%) 0.94
setLong 774 ops/sec (±0.50%) 730 ops/sec (±0.28%) 0.94
setLongWithMerge 428 ops/sec (±1.57%) 405 ops/sec (±0.98%) 0.95
setLongWithSimpleMerge 452 ops/sec (±1.99%) 436 ops/sec (±0.41%) 0.96
setSmallResponse 500x 1563 ops/sec (±3.26%) 1527 ops/sec (±0.38%) 0.98
setMany 50x one-per-row 212 ops/sec (±1.95%) 205 ops/sec (±0.49%) 0.97
setMany 50 batch 6533 ops/sec (±0.39%) 6085 ops/sec (±1.29%) 0.93
setMany 500x one-per-row 20.94 ops/sec (±3.71%) 20.1 ops/sec (±2.53%) 0.96
setMany 500 batch 2746 ops/sec (±1.93%) 2695 ops/sec (±0.85%) 0.98

This comment was automatically generated by workflow using github-action-benchmark.

ntucker commented Nov 8, 2026

Copy link
Copy Markdown
Collaborator Author

Both done in 44d28c2.

  • DevTools: DevToolsManager.cleanup() no longer clears pollingSubscribers, and the stale comment is gone. The test is now "keeps capping a polling key after cleanup() and init()". It asserts that only 2 FETCH entries stay for the key.
  • Shared managers: I folded this in instead of leaving it for later. This PR is what makes the old subscription resume, and it would then poll against the unmounted store. When middleware() binds a different controller, SubscriptionManager now cleans up and drops the subscriptions it tracks. New tests: a unit test, "drops subscriptions from a previous store when bound to a new one", and a React test, "polls for the new store after controller.subscribe() outlived the old one". Both fail without the change. The changeset mentions it.

yarn jest --selectProjects ReactDOM Node --testPathPatterns 'packages/(core|react)': 730 passed. tsc and eslint: 0 errors.


Generated by Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 44d28c2ec9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 877aa2c259

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 4eac13d. Configure here.

… pauses

SubscriptionManager.cleanup() stopped every Subscription, and nothing
restarted the ones that outlive the pause. Subscription gets an
optional init() that resumes work cleanup() stopped; SubscriptionManager
calls it from its own init(), and stops a Subscription created while
paused until then. PollingSubscription implements it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y5C5Z9ziZLXvEhvQd1y4Yx
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y5C5Z9ziZLXvEhvQd1y4Yx
…le paused

A component remounted by StrictMode or <Activity> subscribes before
DataProvider resumes the managers. A custom Subscription without init()
could never resume, so stopping it would leave it stopped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y5C5Z9ziZLXvEhvQd1y4Yx
…subscriptions

SubscriptionManager.init() now resumes polls without a new SUBSCRIBE, so
DevToolsManager must keep its polling counts through cleanup().

A remounted DataProvider that shares Managers binds a new controller. Polls
from the old store would resume against it and swallow the new store's
SUBSCRIBE, so the manager drops them on rebind.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y5C5Z9ziZLXvEhvQd1y4Yx
Like NetworkManager, a late UNSUBSCRIBE from an unmounted store that shares
this manager must not remove the new store's subscription.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Y5C5Z9ziZLXvEhvQd1y4Yx

This branch was successfully deployed

1 active deployment
docs-preview — 81d34dfe Deployed Nov 17, 2026 by ntucker via preview / docs deploy #6245
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants