[0.17.42] Data race in LuaGameScript::create_surface

Bugs that are actually features.
Post Reply
cogito123
Inserter
Inserter
Posts: 23
Joined: Thu Feb 28, 2019 7:05 pm
Contact:

[0.17.42] Data race in LuaGameScript::create_surface

Post 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.

tehfreek
Filter Inserter
Filter Inserter
Posts: 391
Joined: Thu Mar 17, 2016 7:34 am
Contact:

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

Post by tehfreek »

That's not a race condition, that how scoping in Lua works.
Last edited by tehfreek on Wed May 22, 2019 10:02 pm, edited 1 time in total.

orzelek
Smart Inserter
Smart Inserter
Posts: 3911
Joined: Fri Apr 03, 2015 10:20 am
Contact:

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

Post 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.

tehfreek
Filter Inserter
Filter Inserter
Posts: 391
Joined: Thu Mar 17, 2016 7:34 am
Contact:

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

Post by tehfreek »

Fair enough, bad ordering in the internal code then, or insufficient documentation (regarding when a surface is actually "created").

cogito123
Inserter
Inserter
Posts: 23
Joined: Thu Feb 28, 2019 7:05 pm
Contact:

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

Post 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.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13202
Joined: Wed Jun 11, 2014 5:23 am
Contact:

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

Post 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.
If you want to get ahold of me I'm almost always on Discord.

cogito123
Inserter
Inserter
Posts: 23
Joined: Thu Feb 28, 2019 7:05 pm
Contact:

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

Post 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.

Post Reply

Return to “Not a bug”