Skip to content

refactor(react/runtime): improve cleanup of snapshot instances#1293

Merged
Yradex merged 1 commit intolynx-family:mainfrom
Yradex:bsi-delay-destroy
Jul 17, 2025
Merged

refactor(react/runtime): improve cleanup of snapshot instances#1293
Yradex merged 1 commit intolynx-family:mainfrom
Yradex:bsi-delay-destroy

Conversation

@Yradex
Copy link
Copy Markdown
Collaborator

@Yradex Yradex commented Jul 15, 2025

Summary

This pull request refactors the lifecycle management of background snapshot instances to ensure their timely and reliable destruction.

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

Summary by CodeRabbit

  • Bug Fixes

    • Updated background snapshot cleanup to ensure proper teardown of instances after removal.
    • Adjusted test expectations for background snapshot instance removal.
  • New Features

    • Added a dedicated teardown method for background snapshot instances to improve resource management.
  • Refactor

    • Centralized and deferred reference cleanup logic for background snapshots.
  • Style

    • Relaxed type constraints for reference cleanup functions to allow more flexible return values.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Jul 15, 2025

🦋 Changeset detected

Latest commit: 5c79da2

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

This PR includes changesets to release 0 packages

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

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

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Jul 15, 2025

Walkthrough

The changes introduce a new empty changeset file, adjust the expected snapshot in a test, and update the handling of reference cleanup and teardown logic in background snapshot instances. The Ref type is relaxed to allow optional cleanup functions, and instance removal now uses an explicit tearDown method instead of direct deletion.

Changes

File(s) Change Summary
.changeset/rude-seas-design.md Added a new empty changeset file with only frontmatter delimiters.
packages/react/runtime/test/preact.test.jsx Updated test assertion: removed 3 from the expected globalBackgroundSnapshotInstancesToRemove array in a snapshot.
packages/react/runtime/src/backgroundSnapshot.ts Refactored reference cleanup in removeChild to use queueRefAttrUpdate; added tearDown method for instance cleanup.
packages/react/runtime/src/lifecycle/patch/commit.ts Changed background snapshot instance removal to use tearDown() method instead of direct deletion.
packages/react/runtime/src/snapshot/ref.ts Relaxed the Ref type to allow cleanup functions to be optional (may return void).

Sequence Diagram(s)

sequenceDiagram
    participant CommitTask
    participant BackgroundSnapshotInstance
    participant Manager

    Note over CommitTask: After commit, schedule cleanup (10s delay)
    CommitTask->>Manager: Get instance IDs to remove
    loop For each instance ID
        Manager->>BackgroundSnapshotInstance: Retrieve instance
        BackgroundSnapshotInstance->>BackgroundSnapshotInstance: tearDown()
        BackgroundSnapshotInstance->>Manager: Remove self from manager
    end
Loading

Poem

A bunny hops through code anew,
Tidying refs with a lighter view.
Snapshots cleaned with gentle care,
Teardowns handled everywhere.
Types now softer, tests align—
In this patch, the carrots shine! 🥕

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

 ERROR  Cannot resolve version $@rspack/core in overrides. The direct dependencies don't have dependency "@rspack/core".
For help, run: pnpm help install

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Yradex Yradex marked this pull request as ready for review July 15, 2025 11:40
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.

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a1efa6a and 5c79da2.

📒 Files selected for processing (5)
  • .changeset/rude-seas-design.md (1 hunks)
  • packages/react/runtime/__test__/preact.test.jsx (0 hunks)
  • packages/react/runtime/src/backgroundSnapshot.ts (2 hunks)
  • packages/react/runtime/src/lifecycle/patch/commit.ts (1 hunks)
  • packages/react/runtime/src/snapshot/ref.ts (1 hunks)
💤 Files with no reviewable changes (1)
  • packages/react/runtime/test/preact.test.jsx
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/react/runtime/src/lifecycle/patch/commit.ts (1)
packages/react/runtime/src/snapshot.ts (1)
  • backgroundSnapshotInstanceManager (136-181)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: build / Build (Windows)
🔇 Additional comments (3)
packages/react/runtime/src/snapshot/ref.ts (1)

13-14: Type relaxation looks good.

The change to allow ref functions and _unmount to return void provides more flexibility while maintaining backward compatibility. The applyRef function already handles both cases correctly by checking typeof ref._unmount == 'function' before invoking it.

packages/react/runtime/src/lifecycle/patch/commit.ts (1)

98-98: Confirmed BackgroundSnapshotInstance.tearDown() implementation

I found the BackgroundSnapshotInstance class and its tearDown() method in packages/react/runtime/src/backgroundSnapshot.ts. The method correctly calls traverseSnapshotInstance to reset __parent, and similar cleanup exists in other snapshot-related classes. No further action needed.

  • packages/react/runtime/src/backgroundSnapshot.ts: export class BackgroundSnapshotInstance { … tearDown(): void { … } }
packages/react/runtime/src/backgroundSnapshot.ts (1)

186-193: Well-implemented tearDown method.

The tearDown method properly handles cleanup by:

  1. Breaking circular references by nullifying parent and sibling pointers
  2. Removing all nodes in the subtree from the manager

This provides a clean separation of concerns compared to the previous direct deletion approach.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Jul 15, 2025

CodSpeed Performance Report

Merging #1293 will not alter performance

Comparing Yradex:bsi-delay-destroy (5c79da2) with main (a1efa6a)

Summary

✅ 10 untouched benchmarks

@relativeci
Copy link
Copy Markdown

relativeci bot commented Jul 15, 2025

React Example

#2962 Bundle Size — 234.2KiB (+0.03%).

5c79da2(current) vs a1efa6a main#2956(baseline)

Bundle metrics  Change 2 changes
                 Current
#2962
     Baseline
#2956
No change  Initial JS 0B 0B
No change  Initial CSS 0B 0B
Change  Cache Invalidation 37.74% 0%
No change  Chunks 0 0
No change  Assets 4 4
No change  Modules 153 153
No change  Duplicate Modules 61 61
Change  Duplicate Code 45.84%(-0.02%) 45.85%
No change  Packages 2 2
No change  Duplicate Packages 0 0
Bundle size by type  Change 1 change Regression 1 regression
                 Current
#2962
     Baseline
#2956
No change  IMG 145.76KiB 145.76KiB
Regression  Other 88.45KiB (+0.09%) 88.37KiB

Bundle analysis reportBranch Yradex:bsi-delay-destroyProject dashboard


Generated by RelativeCIDocumentationReport issue

@relativeci
Copy link
Copy Markdown

relativeci bot commented Jul 15, 2025

Web Explorer

#2953 Bundle Size — 303.56KiB (0%).

5c79da2(current) vs a1efa6a main#2947(baseline)

Bundle metrics  Change 1 change
                 Current
#2953
     Baseline
#2947
No change  Initial JS 139.94KiB 139.94KiB
No change  Initial CSS 31.88KiB 31.88KiB
No change  Cache Invalidation 0% 0%
No change  Chunks 4 4
No change  Assets 5 5
Change  Modules 205(-0.49%) 206
No change  Duplicate Modules 17 17
No change  Duplicate Code 4.23% 4.23%
No change  Packages 3 3
No change  Duplicate Packages 0 0
Bundle size by type  no changes
                 Current
#2953
     Baseline
#2947
No change  JS 220.79KiB 220.79KiB
No change  Other 50.89KiB 50.89KiB
No change  CSS 31.88KiB 31.88KiB

Bundle analysis reportBranch Yradex:bsi-delay-destroyProject dashboard


Generated by RelativeCIDocumentationReport issue

@Yradex Yradex added this pull request to the merge queue Jul 17, 2025
Merged via the queue into lynx-family:main with commit 0d8ad61 Jul 17, 2025
45 checks passed
@Yradex Yradex deleted the bsi-delay-destroy branch July 17, 2025 09:43
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