Files
Bubberstation/code/modules/mapping/space_management/zlevel_manager.dm
SkyratBot cd94063668 Fix race condition in SSmapping z-level creation (#59560) (#6207)
As described in issue #56733, there is a possibility of a race condition in SSmapping.add_new_zlevel - either during Z level incrementing, or the proc itself, as it only expands the amount of registered z-levels after being finished.

While the first hopefully shouldn't happen, it can still manifest because of the CHECK_TICK within. A practical result is that two concurrent calls to this proc will both create a new Z-level, but actually report having made the same first one.

This is problematic for map_templates that will then load blindly to the reported Z-level when using load_new_z, overwriting each other. We sometimes encounter that end result on CM13 codebase because of a map load that can be triggered by an user topic - if two people click at the same time, it's not unlikely for the first call to sleep in CHECK_TICK and let the second run, causing double-loading.

Co-authored-by: fira <loyauflorian@gmail.com>
2021-06-10 08:55:34 +12:00

37 lines
1.4 KiB
Plaintext

// Populate the space level list and prepare space transitions
/datum/controller/subsystem/mapping/proc/InitializeDefaultZLevels()
if (z_list) // subsystem/Recover or badminnery, no need
return
z_list = list()
var/list/default_map_traits = DEFAULT_MAP_TRAITS
if (default_map_traits.len != world.maxz)
WARNING("More or less map attributes pre-defined ([default_map_traits.len]) than existent z-levels ([world.maxz]). Ignoring the larger.")
if (default_map_traits.len > world.maxz)
default_map_traits.Cut(world.maxz + 1)
for (var/I in 1 to default_map_traits.len)
var/list/features = default_map_traits[I]
var/datum/space_level/S = new(I, features[DL_NAME], features[DL_TRAITS])
z_list += S
/datum/controller/subsystem/mapping/proc/add_new_zlevel(name, traits = list(), z_type = /datum/space_level)
UNTIL(!adding_new_zlevel)
adding_new_zlevel = TRUE
SEND_GLOBAL_SIGNAL(COMSIG_GLOB_NEW_Z, args)
var/new_z = z_list.len + 1
if (world.maxz < new_z)
world.incrementMaxZ()
CHECK_TICK
// TODO: sleep here if the Z level needs to be cleared
var/datum/space_level/S = new z_type(new_z, name, traits)
z_list += S
adding_new_zlevel = FALSE
return S
/datum/controller/subsystem/mapping/proc/get_level(z)
if (z_list && z >= 1 && z <= z_list.len)
return z_list[z]
CRASH("Unmanaged z-level [z]! maxz = [world.maxz], z_list.len = [z_list ? z_list.len : "null"]")