Skip to content

Add mutex lock in get_shader_rid()#118202

Open
Infiland wants to merge 1 commit into
godotengine:masterfrom
Infiland:issue-118151
Open

Add mutex lock in get_shader_rid()#118202
Infiland wants to merge 1 commit into
godotengine:masterfrom
Infiland:issue-118151

Conversation

@Infiland
Copy link
Copy Markdown
Contributor

@Infiland Infiland commented Apr 4, 2026

CanvasItemMaterial::get_shader_rid() accesses the static shader_map and current_key without holding material_mutex, while flush_changes() (idle callback) modifies both under the mutex via _update_shader(). When the rendering thread calls get_shader_rid() concurrently, it can read a stale key that was just erased from the map, causing a use-after-free crash.

This is most easily triggered by rapidly toggling the Blend Mode property on a CanvasItemMaterial assigned to a GPUParticles2D node, but affects any CanvasItemMaterial during rendering.

The fix adds MutexLock lock(material_mutex) to get_shader_rid(), matching the locking convention already used by flush_changes(), _queue_shader_change(), and ~CanvasItemMaterial(). CanvasItemMaterial was the only material class missing this protection, BaseMaterial3D, ParticleProcessMaterial, and others all synchronize correctly.

@Infiland Infiland requested a review from a team as a code owner April 4, 2026 19:41
Acquire material_mutex in CanvasItemMaterial::get_shader_rid() before accessing shader_map/current_key. Fixes godotengine#118151
@Nintorch Nintorch changed the title Add mutex lock in get_shader_rid(), fixes #118151 Add mutex lock in get_shader_rid() Apr 4, 2026
@Nintorch Nintorch added this to the 4.x milestone Apr 4, 2026
@KoBeWi KoBeWi requested a review from a team April 4, 2026 20:12
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.

Crash when changing CanvasItemMaterial's Blend Mode in GPUParticles2D

2 participants