Page 1 of 1

[0.17.42] Data race in LuaGameScript::create_surface

Posted: Wed May 22, 2019 6:09 pm
by cogito123
Hi,

Snippet that roughly demonstrates the problem:

Code: Select all

1. local surface = game.surfaces[1]
2. local map_settings = surface.map_gen_settings
3. local new_surface = game.create_surface("test", map_settings)
...
4. script.on_event(defines.events.on_surface_created, function(event)
5.   if event.surface_index == 2 then
6.      local map_settings = game.surfaces[2].map_gen_settings
7.      -- map_settings is nil here and produces unrecoverable error
8.   end
9. end)
OK behavior:
map_settings from line 6 should be exactly the same as in line 2.

NOK behavior:
map_settings from line 6 is nil and causes unrecoverable error.

Re: [0.17.42] Data race in LuaGameScript::create_surface

Posted: Wed May 22, 2019 7:34 pm
by tehfreek
That's not a race condition, that how scoping in Lua works.

Re: [0.17.42] Data race in LuaGameScript::create_surface

Posted: Wed May 22, 2019 9:52 pm
by orzelek
tehfreek wrote: Wed May 22, 2019 7:34 pm That's not a race condition, that how scoping in Lua works.
Code is actually trying to get map settings from newly created surface and that value is nil. That doesn't look like anything to do with scope since it's creating entirely new variable.
Unless I'm not understanding something from the link above.

Re: [0.17.42] Data race in LuaGameScript::create_surface

Posted: Wed May 22, 2019 10:03 pm
by tehfreek
Fair enough, bad ordering in the internal code then, or insufficient documentation (regarding when a surface is actually "created").

Re: [0.17.42] Data race in LuaGameScript::create_surface

Posted: Thu May 23, 2019 3:54 pm
by cogito123
tehfreek wrote: Mon Jan 12, 1970 1:46 pm Fair enough, bad ordering in the internal code then, or insufficient documentation (regarding when a surface is actually "created").
If I receive event on_surface_created I expect surface object to be fully initialized, if it's not, then what's the point of this event if I can't do anything meaningful with the created surface? That's a definitely a problem in internal code.

Re: [0.17.42] Data race in LuaGameScript::create_surface

Posted: Fri May 24, 2019 12:34 am
by Rseding91
Thanks for the report however I can't reproduce what you're describing. The code snip you have in your original post isn't even valid Factorio Lua since you can't use game outside of an event.

Additionally you will never get a surface_created event where the surface_index is invalid. If you get the event the surface_index will be valid and you will be able to read map_gen_settings off of it.

I *highly* suspect your actual code/mods are broken.

Re: [0.17.42] Data race in LuaGameScript::create_surface

Posted: Fri May 24, 2019 4:31 pm
by cogito123
Rseding91 wrote: Fri May 24, 2019 12:34 am Thanks for the report however I can't reproduce what you're describing. The code snip you have in your original post isn't even valid Factorio Lua since you can't use game outside of an event.

Additionally you will never get a surface_created event where the surface_index is invalid. If you get the event the surface_index will be valid and you will be able to read map_gen_settings off of it.

I *highly* suspect your actual code/mods are broken.
I confirm that the problem was indeed in my code, I was trying to reach global variable that was initialized from create_surface. Since create_surface doesn't return before calling on_surface_created, it caused an error.