Skip to content

Conversation

@colinaaa
Copy link
Collaborator

@colinaaa colinaaa commented Jul 30, 2025

Summary by CodeRabbit

  • Bug Fixes

    • Resolved a crash issue by ensuring event listeners are properly removed during component destruction.
  • Tests

    • Added a new test suite to verify event listener cleanup during component unmounting.
    • Updated existing tests to include explicit event listener removal steps.
    • Enhanced test setup to conditionally skip specific test cases for improved reliability.

close: f-6739779600

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).
  • Changeset added, and when a BREAKING CHANGE occurs, it needs to be clearly marked (or not required).

@changeset-bot
Copy link

changeset-bot bot commented Jul 30, 2025

🦋 Changeset detected

Latest commit: 7faf71f

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

This PR includes changesets to release 1 package
Name Type
@lynx-js/react 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

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 30, 2025

📝 Walkthrough

Walkthrough

This change addresses event listener cleanup in the "@lynx-js/react" package to prevent crashes during component destruction. It introduces a new test suite, updates existing tests for proper event listener removal, adjusts test utility logic for selective test skipping, and reorders cleanup operations in the destroy lifecycle function.

Changes

Cohort / File(s) Change Summary
Patch & Documentation
.changeset/bright-rings-read.md
Adds a changeset documenting a patch for "@lynx-js/react" that fixes a crash by ensuring event listeners are properly removed during the destroy phase.
New Destroy Lifecycle Test
packages/react/runtime/__test__/lifecycle/destroy.test.jsx
Introduces a new test suite "Destroy" to verify that event listeners are correctly cleaned up and removed during component unmounting, especially when cleanup functions throw errors.
Event Listener Cleanup in Tests
packages/react/runtime/__test__/utils/runtimeProxy.test.js
Updates event dispatching tests to explicitly remove event listeners after assertions, ensuring proper cleanup in both positive and negative event context tests.
Test Utility Skipping Logic
packages/react/runtime/__test__/utils/setup.js
Modifies the afterEach hook to skip additional test cases by checking against a list of substrings, including the new destroy test, for more flexible and robust test skipping.
Destroy Lifecycle Operation Order
packages/react/runtime/src/lynx/tt.ts
Changes the order in tt.callDestroyLifetimeFun so that event listeners are removed before destroying worklets and background processes, ensuring cleanup occurs prior to resource teardown.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Suggested labels

framework:React

Poem

In the garden of code where listeners grew,
A crash was afoot—what’s a rabbit to do?
With cleanup in mind and a test or two,
We hopped through the lifecycle, removed quite a few.
Now the code is serene, with bugs out of view—
🐇✨ Hooray for the fix, and a patch that’s brand new!

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between a4ee4fd and 7faf71f.

📒 Files selected for processing (5)
  • .changeset/bright-rings-read.md (1 hunks)
  • packages/react/runtime/__test__/lifecycle/destroy.test.jsx (1 hunks)
  • packages/react/runtime/__test__/utils/runtimeProxy.test.js (1 hunks)
  • packages/react/runtime/__test__/utils/setup.js (1 hunks)
  • packages/react/runtime/src/lynx/tt.ts (1 hunks)
🧰 Additional context used
🧠 Learnings (4)
📓 Common learnings
Learnt from: colinaaa
PR: lynx-family/lynx-stack#1330
File: .changeset/olive-animals-attend.md:1-3
Timestamp: 2025-07-22T09:23:07.797Z
Learning: In the lynx-family/lynx-stack repository, changesets are only required for meaningful changes to end-users such as bugfixes and features. Internal/development changes like chores, refactoring, or removing debug info do not need changeset entries.
Learnt from: colinaaa
PR: lynx-family/lynx-stack#1238
File: packages/react/runtime/src/debug/component-stack.ts:70-90
Timestamp: 2025-07-18T04:27:18.291Z
Learning: The component-stack.ts file in packages/react/runtime/src/debug/component-stack.ts is a direct fork from https://github.com/preactjs/preact/blob/main/debug/src/component-stack.js. The team prefers to keep it aligned with the upstream Preact version and may contribute improvements back to Preact in the future.
Learnt from: PupilTong
PR: lynx-family/lynx-stack#1029
File: packages/web-platform/web-core-server/src/createLynxView.ts:0-0
Timestamp: 2025-07-16T06:26:22.230Z
Learning: In the lynx-stack SSR implementation, each createLynxView instance is used to render once and then discarded. There's no reuse of the same instance for multiple renders, so event arrays and other state don't need to be cleared between renders.
Learnt from: PupilTong
PR: lynx-family/lynx-stack#1292
File: packages/web-platform/web-core-server/src/createLynxView.ts:144-151
Timestamp: 2025-07-15T10:00:56.154Z
Learning: In the lynx-stack codebase, PupilTong prefers the "let it crash" approach over defensive null safety checks when the condition should never occur in normal operation. This applies to cases like the `element.getAttribute(lynxUniqueIdAttribute)!` call in SSR event capture where the attribute is expected to always be present.
Learnt from: colinaaa
PR: lynx-family/lynx-stack#1330
File: .changeset/olive-animals-attend.md:1-3
Timestamp: 2025-07-22T09:26:16.722Z
Learning: In the lynx-family/lynx-stack repository, CI checks require changesets when files matching the pattern "src/**" are modified (as configured in .changeset/config.json). For internal changes that don't need meaningful changesets, an empty changeset file is used to satisfy the CI requirement while not generating any release notes.
Learnt from: PupilTong
PR: lynx-family/lynx-stack#1029
File: packages/web-platform/web-core/src/uiThread/createRenderAllOnUI.ts:95-99
Timestamp: 2025-07-16T06:28:26.463Z
Learning: In the lynx-stack codebase, CSS selectors in SSR hydration are generated by their own packages, ensuring a predictable format that makes simple string manipulation safe and preferable over regex for performance reasons.
.changeset/bright-rings-read.md (4)

Learnt from: colinaaa
PR: #1330
File: .changeset/olive-animals-attend.md:1-3
Timestamp: 2025-07-22T09:23:07.797Z
Learning: In the lynx-family/lynx-stack repository, changesets are only required for meaningful changes to end-users such as bugfixes and features. Internal/development changes like chores, refactoring, or removing debug info do not need changeset entries.

Learnt from: colinaaa
PR: #1330
File: .changeset/olive-animals-attend.md:1-3
Timestamp: 2025-07-22T09:26:16.722Z
Learning: In the lynx-family/lynx-stack repository, CI checks require changesets when files matching the pattern "src/**" are modified (as configured in .changeset/config.json). For internal changes that don't need meaningful changesets, an empty changeset file is used to satisfy the CI requirement while not generating any release notes.

Learnt from: PupilTong
PR: #1292
File: packages/web-platform/web-core-server/src/createLynxView.ts:144-151
Timestamp: 2025-07-15T10:00:56.154Z
Learning: In the lynx-stack codebase, PupilTong prefers the "let it crash" approach over defensive null safety checks when the condition should never occur in normal operation. This applies to cases like the element.getAttribute(lynxUniqueIdAttribute)! call in SSR event capture where the attribute is expected to always be present.

Learnt from: PupilTong
PR: #1029
File: packages/web-platform/web-core-server/src/createLynxView.ts:0-0
Timestamp: 2025-07-16T06:26:22.230Z
Learning: In the lynx-stack SSR implementation, each createLynxView instance is used to render once and then discarded. There's no reuse of the same instance for multiple renders, so event arrays and other state don't need to be cleared between renders.

packages/react/runtime/__test__/utils/setup.js (1)

Learnt from: colinaaa
PR: #1238
File: packages/react/runtime/src/debug/component-stack.ts:70-90
Timestamp: 2025-07-18T04:27:18.291Z
Learning: The component-stack.ts file in packages/react/runtime/src/debug/component-stack.ts is a direct fork from https://github.com/preactjs/preact/blob/main/debug/src/component-stack.js. The team prefers to keep it aligned with the upstream Preact version and may contribute improvements back to Preact in the future.

packages/react/runtime/__test__/lifecycle/destroy.test.jsx (3)

Learnt from: colinaaa
PR: #1238
File: packages/react/runtime/src/debug/component-stack.ts:70-90
Timestamp: 2025-07-18T04:27:18.291Z
Learning: The component-stack.ts file in packages/react/runtime/src/debug/component-stack.ts is a direct fork from https://github.com/preactjs/preact/blob/main/debug/src/component-stack.js. The team prefers to keep it aligned with the upstream Preact version and may contribute improvements back to Preact in the future.

Learnt from: PupilTong
PR: #1029
File: packages/web-platform/web-core-server/src/createLynxView.ts:0-0
Timestamp: 2025-07-16T06:26:22.230Z
Learning: In the lynx-stack SSR implementation, each createLynxView instance is used to render once and then discarded. There's no reuse of the same instance for multiple renders, so event arrays and other state don't need to be cleared between renders.

Learnt from: PupilTong
PR: #1292
File: packages/web-platform/web-core-server/src/createLynxView.ts:144-151
Timestamp: 2025-07-15T10:00:56.154Z
Learning: In the lynx-stack codebase, PupilTong prefers the "let it crash" approach over defensive null safety checks when the condition should never occur in normal operation. This applies to cases like the element.getAttribute(lynxUniqueIdAttribute)! call in SSR event capture where the attribute is expected to always be present.

🧬 Code Graph Analysis (1)
packages/react/runtime/src/lynx/tt.ts (1)
packages/react/runtime/src/lifecycle/patch/error.ts (1)
  • removeCtxNotFoundEventListener (55-61)
🔇 Additional comments (6)
.changeset/bright-rings-read.md (1)

1-6: LGTM! Changeset properly documents the bugfix.

The changeset correctly identifies this as a patch-level change and provides a clear description of the fix for event listener cleanup during component destruction.

packages/react/runtime/src/lynx/tt.ts (1)

32-36: LGTM! Event listener cleanup moved to appropriate position.

Moving removeCtxNotFoundEventListener() before destroyWorklet() and destroyBackground() ensures event listeners are cleaned up even if subsequent destroy operations throw errors. This correctly addresses the crash issue described in the PR.

packages/react/runtime/__test__/utils/setup.js (1)

20-27: LGTM! Proper test exclusion setup.

Converting to an array format and adding the new destroy test to the skip list prevents interference with the afterEach cleanup checks. This aligns with the existing pattern for preact/debug tests.

packages/react/runtime/__test__/utils/runtimeProxy.test.js (2)

20-20: LGTM! Proper event listener cleanup.

Adding explicit removeEventListener call ensures the test properly cleans up after itself and prevents listener leakage between tests.


28-28: LGTM! Consistent cleanup pattern.

Good practice to clean up event listeners in all test cases that add them, maintaining test isolation.

packages/react/runtime/__test__/lifecycle/destroy.test.jsx (1)

1-46: LGTM! Comprehensive test for event listener cleanup during errors.

This test effectively verifies the core fix by:

  1. Proper mocking setup: Mocks lynx.getCoreContext() to track addEventListener/removeEventListener calls
  2. Error scenario testing: Uses a cleanup function that throws to simulate the crash scenario
  3. Cleanup verification: Ensures removeEventListener is called the same number of times as addEventListener
  4. Async handling: Properly handles React's lifecycle timing with Promise resolution

The test provides strong coverage for the event listener cleanup fix and validates that cleanup works even when errors occur.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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
🪧 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 generate unit tests to generate unit tests for 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.

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.

@codspeed-hq
Copy link

codspeed-hq bot commented Jul 30, 2025

CodSpeed Performance Report

Merging #1379 will not alter performance

Comparing colinaaa:colin/0730/remove-event-listener (7faf71f) with main (a4ee4fd)

Summary

✅ 10 untouched benchmarks

@relativeci
Copy link

relativeci bot commented Jul 30, 2025

React Example

#3476 Bundle Size — 235.18KiB (+0.02%).

7faf71f(current) vs a8c8e39 main#3462(baseline)

Bundle metrics  Change 1 change
                 Current
#3476
     Baseline
#3462
No change  Initial JS 0B 0B
No change  Initial CSS 0B 0B
Change  Cache Invalidation 38.01% 38.02%
No change  Chunks 0 0
No change  Assets 4 4
No change  Modules 156 156
No change  Duplicate Modules 63 63
No change  Duplicate Code 45.94% 45.94%
No change  Packages 2 2
No change  Duplicate Packages 0 0
Bundle size by type  Change 1 change Regression 1 regression
                 Current
#3476
     Baseline
#3462
No change  IMG 145.76KiB 145.76KiB
Regression  Other 89.42KiB (+0.05%) 89.38KiB

Bundle analysis reportBranch colinaaa:colin/0730/remove-event...Project dashboard


Generated by RelativeCIDocumentationReport issue

@relativeci
Copy link

relativeci bot commented Jul 30, 2025

Web Explorer

#3466 Bundle Size — 352.53KiB (0%).

7faf71f(current) vs a8c8e39 main#3452(baseline)

Bundle metrics  Change 1 change
                 Current
#3466
     Baseline
#3452
No change  Initial JS 142.12KiB 142.12KiB
No change  Initial CSS 31.83KiB 31.83KiB
No change  Cache Invalidation 0% 0%
No change  Chunks 7 7
No change  Assets 7 7
Change  Modules 214(-0.47%) 215
No change  Duplicate Modules 17 17
No change  Duplicate Code 3.94% 3.94%
No change  Packages 4 4
No change  Duplicate Packages 0 0
Bundle size by type  no changes
                 Current
#3466
     Baseline
#3452
No change  JS 226.4KiB 226.4KiB
No change  Other 94.29KiB 94.29KiB
No change  CSS 31.83KiB 31.83KiB

Bundle analysis reportBranch colinaaa:colin/0730/remove-event...Project dashboard


Generated by RelativeCIDocumentationReport issue

@colinaaa colinaaa marked this pull request as ready for review July 30, 2025 10:23
@colinaaa colinaaa requested review from Yradex and hzy July 30, 2025 10:23
@colinaaa colinaaa merged commit 2a51cfb into lynx-family:main Jul 31, 2025
93 of 100 checks passed
@colinaaa colinaaa deleted the colin/0730/remove-event-listener branch July 31, 2025 08:03
colinaaa pushed a commit that referenced this pull request Aug 1, 2025
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @lynx-js/[email protected]

### Minor Changes

- Add support for Lynx UI plugin system with configurable options.
([#1363](#1363))

- Introduced `lynxUIPlugins` option in `createLynxPreset`, allowing
userland opt-in to Lynx UI specific plugins.

- Implemented `uiVariants` plugin as the first UI plugin, supporting
`ui-*` variant prefixes (e.g. `ui-checked`, `ui-open`) with customizable
mappings.

## @lynx-js/[email protected]

### Patch Changes

- Fix crash caused by not removing event listeners during destroy.
([#1379](#1379))

- Fix missing "type" in "update-list-info" in hydrate
([#1392](#1392))

## @lynx-js/[email protected]

### Patch Changes

- Bump Rsbuild v1.4.12 with Rspack v1.4.11.
([#1326](#1326))

## @lynx-js/[email protected]

### Patch Changes

- Updated dependencies
\[[`e9edca0`](e9edca0),
[`6f37db2`](6f37db2)]:
    -   @lynx-js/[email protected]
    -   @lynx-js/[email protected]
    -   @lynx-js/[email protected]
    -   @lynx-js/[email protected]
    -   @lynx-js/[email protected]
    -   @lynx-js/[email protected]

## @lynx-js/[email protected]

### Patch Changes

- feat: support `__ElementFromBinary`
([#1391](#1391))

-   Updated dependencies \[]:
    -   @lynx-js/[email protected]

## @lynx-js/[email protected]

### Patch Changes

- feat: support `__ElementFromBinary`
([#1391](#1391))

- fix: crash on chrome&lt;96
([#1361](#1361))


<wasm-bindgen/wasm-bindgen#4211 (comment)>

    <WebAssembly/binaryen#7358>

The rust toolchain enables WASM feature `reference types` by default.

However this feature is not supported by chromium lower than version 96

    Therefore we found a workaround for it.

In this implementation we detect if browser supports `reference types`
first.

If user's browser supported it, we load the wasm file with `reference
types` on, otherwise we load the wasm file with `reference types` off.

- Updated dependencies
\[[`22ca433`](22ca433),
[`8645d12`](8645d12),
[`143e481`](143e481)]:
    -   @lynx-js/[email protected]
    -   @lynx-js/[email protected]
    -   @lynx-js/[email protected]
    -   @lynx-js/[email protected]

## @lynx-js/[email protected]

### Patch Changes

- refactor: bundle web-core-server
([#819](#819))

## @lynx-js/[email protected]

### Patch Changes

- feat: support color style for x-textarea
([#1382](#1382))

-   Updated dependencies \[]:
    -   @lynx-js/[email protected]

## @lynx-js/[email protected]

### Patch Changes

- feat: support `__ElementFromBinary`
([#1391](#1391))

- fix: crash on chrome&lt;96
([#1361](#1361))


<wasm-bindgen/wasm-bindgen#4211 (comment)>

    <WebAssembly/binaryen#7358>

The rust toolchain enables WASM feature `reference types` by default.

However this feature is not supported by chromium lower than version 96

    Therefore we found a workaround for it.

In this implementation we detect if browser supports `reference types`
first.

If user's browser supported it, we load the wasm file with `reference
types` on, otherwise we load the wasm file with `reference types` off.

- Updated dependencies
\[[`22ca433`](22ca433)]:
    -   @lynx-js/[email protected]
    -   @lynx-js/[email protected]

## @lynx-js/[email protected]

### Patch Changes

- optimize IO for all-on-ui: make startMainThreadWorker async and defer
import ([#1385](#1385))

- Updated dependencies
\[[`22ca433`](22ca433),
[`143e481`](143e481)]:
    -   @lynx-js/[email protected]
    -   @lynx-js/[email protected]
    -   @lynx-js/[email protected]

## @lynx-js/[email protected]

### Patch Changes

- Support Rspack v1.4.9.
([#1351](#1351))

## @lynx-js/[email protected]

### Patch Changes

- feat: support elementTemplate for web
([#1374](#1374))

## [email protected]



## @lynx-js/[email protected]



## [email protected]



## @lynx-js/[email protected]



## @lynx-js/[email protected]



## @lynx-js/[email protected]

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.

2 participants