Skip to content

fix(assets): guard Material/Texture event callbacks against teardown/…#170

Merged
star-e merged 1 commit into
cocos:v4.0.0from
bofeng-song:fix/material-event-callback-teardown-crash
Jul 6, 2026
Merged

fix(assets): guard Material/Texture event callbacks against teardown/…#170
star-e merged 1 commit into
cocos:v4.0.0from
bofeng-song:fix/material-event-callback-teardown-crash

Conversation

@bofeng-song

@bofeng-song bofeng-song commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

GC crash

Material::PassesUpdated and the SimpleTexture/TextureBase update events can be emitted from update()/copy()/doDestroy() while the script engine is being cleaned up or a GC is running. In those windows findFirst() may still return an se::Object whose V8 persistent points at a freed object, so calling into it crashes in ObjectWrap::handle() (Local::New on a dangling handle).

Resolve the live JS wrapper from the native emitter at emit time instead of capturing s.thisObject(), and bail out when the engine is invalid, in cleanup, or garbage collecting.

Re: #
https://forum.cocos.org/t/topic/172523/23

Changelog


Continuous Integration

This pull request:

  • needs automatic test cases check.

    Manual trigger with @cocos-robot run test cases afterward.

  • does not change any runtime related code or build configuration

    If any reviewer thinks the CI checks are needed, please uncheck this option, then close and reopen the issue.


Compatibility Check

This pull request:

  • changes public API, and have ensured backward compatibility with deprecated features.
  • affects platform compatibility, e.g. system version, browser version, platform sdk version, platform toolchain, language version, hardware compatibility etc.
  • affects file structure of the build package or build configuration which requires user project upgrade.
  • introduces breaking changes, please list all changes, affected features and the scope of violation.

Reproduce

Attach rt-passes-repro.ts to any node in the scene and run it to reproduce the crash.

rt-passes-repro.zip

…GC crash

Material::PassesUpdated and the SimpleTexture/TextureBase update events can be
emitted from update()/copy()/doDestroy() while the script engine is being
cleaned up or a GC is running. In those windows findFirst() may still return an
se::Object whose V8 persistent points at a freed object, so calling into it
crashes in ObjectWrap::handle() (Local<Object>::New on a dangling handle).

Resolve the live JS wrapper from the native emitter at emit time instead of
capturing s.thisObject(), and bail out when the engine is invalid, in cleanup,
or garbage collecting.
@bofeng-song
bofeng-song force-pushed the fix/material-event-callback-teardown-crash branch from 58d3772 to 2b3b94a Compare June 26, 2026 07:59
@bofeng-song
bofeng-song requested a review from star-e June 26, 2026 08:05
@github-actions

Copy link
Copy Markdown

Code Size Check Report

Wechat (WASM) Before After Diff
2D Empty (legacy pipeline) 1014459 bytes 1014459 bytes ✅ 0 bytes
2D All (legacy pipeline) 2681984 bytes 2681984 bytes ✅ 0 bytes
2D All (new pipeline) 2773761 bytes 2773761 bytes ✅ 0 bytes
(2D + 3D) All 10030977 bytes 10030977 bytes ✅ 0 bytes
Web (WASM + ASMJS) Before After Diff
(2D + 3D) All 16867390 bytes 16867390 bytes ✅ 0 bytes

Interface Check Report

This pull request does not change any public interfaces !

@star-e

star-e commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

@cocos-robot run-test-cases-custom

@star-e

star-e commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

@cocos-robot run test cases

@star-e star-e closed this Jun 29, 2026
@star-e star-e reopened this Jun 29, 2026
@star-e

star-e commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

@cocos-robot run test cases

@star-e star-e closed this Jul 2, 2026
@star-e star-e reopened this Jul 2, 2026
@star-e

star-e commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

@cocos-robot run test cases

1 similar comment
@star-e

star-e commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

@cocos-robot run test cases

@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown

@bofeng-song, Please check the result of run test cases:

Task Details

Platform build boot runned crashScene FailScene
web-mobile PASS PASS FAIL graphics-continuous-filling
ios PASS PASS PASS
mac PASS PASS PASS

@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown

@bofeng-song, Please check the result of run test cases:

Task Details

@star-e
star-e merged commit 8878e7a into cocos:v4.0.0 Jul 6, 2026
109 checks passed
@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown

@bofeng-song ❗ There was an error during the execution of the tasks. Please check the logs for more details.

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