Zylann / godot_voxel

Voxel module for Godot Engine
MIT License
2.72k stars 251 forks source link

Thread dead locked when flying and mainthread is waiting the lock #693

Closed lenesxy closed 2 months ago

lenesxy commented 2 months ago

When applicable, please describe your setup:

Screenshot ThreadA: DevCap_20240902_154905

ThreadB: DevCap_20240902_154927

MainThread: DevCap_20240902_154815

ThreadA:VoxelData::get_blocks_grid() will read lock [data_lod.map_lock] and the reference_area_block_coords() will read lock [data_lod.spatial_lock] in sl->lock_read(). Should Spatial lock need to be locked first here? ThreadB:VoxelData::unload_blocks() will write lock [lod.spatial_lock] and then write lock [lod.map_lock]

This may cause dead lock

Zylann commented 2 months ago

ThreadA:VoxelData::get_blocks_grid() will read lock [data_lod.map_lock] and the reference_area_block_coords() will read lock [data_lod.spatial_lock] in sl->lock_read(). Should Spatial lock need to be locked first here?

If both the map and the spatial lock need to be locked (i.e access to chunks AND voxels inside the chunks are both needed), then spatial lock must be locked first, and the map second while the spatial lock is acquired.

VoxelData::get_blocks_grid and reference_area_block_coords look wrong because they do the opposite, the map is locked first and spatial lock second, hence the possible deadlock. I don't recall why it ended up this way, but should probably be swapped.

Zylann commented 2 months ago

Should be fixed in 5da379d10004c6726ae43f7d43cfc6d5e3bc797e