Page 1 of 1

[boskid][1.1.53] Crash connecting rolling stock to neighbour while its being removed (on_train_created)

Posted: Thu Jan 27, 2022 5:10 pm
by Muppet9010
If you interact with a train carriage that is mid removal and get it to re-connect to another carriage you can crash the game. The on_train_created event gives you access to this mid removal stage.

Logic:
remove a locomotive from a 2 entity train. The game has to disconnect the loco first before removal. Using an event react to the train being changed (losing its loco) and tell the loco to re-join back to the train. Game crashes at this point.

Reproduction steps:
  1. load attached save (test.zip), just 2 locomotives on a rail connected as a train.
  2. run the attached Lua script to crash the game. It sets up the event function to try and rejoin the locomotives when their train changes and then removes one of the locomotives.
Log and crash dump attached.

I found this as I noticed that the carriage didn't have a train attribute in the same setup and this surprised me (and crashed my mod). So I found this while quickly exploring the risks of this situation :)

Re: [boskid][1.1.53] Crash connecting rolling stock to neighbour while its being removed (on_train_created)

Posted: Fri Jan 28, 2022 10:43 am
by boskid
I was really thinking about this but the issue is annoying to fix the way i would like in the middle of 1.1.x. Basically the issue is that when a rolling stock is being destroyed it has to detach from other rolling stocks first but this disconnect happens when rolling stock is not yet marked as to be deleted (its a preparation to the destroy) so the LuaEntity for that rolling stock is still saying that rolling stock is valid and can be used. That rolling stock during the event is in a quite interesting state because it is already detached from the surface and is going to be destroyed literally after the event so doing anything here when LuaEntity::train reads nil is going to be a suspicious. I really do not want to change the behavior of marking entity as to be deleted too early as it may have multiple consequences for the rolling stock disconnect code and other effects i may not know and most likely i cannot make those events to be sent later. So for now i made a tiny workaround fix: LuaEntity::connect_rolling_stock will fail when a rolling stock does not belong to any trains. I am not considering it to be a bulletproof fix but that is what there will happen for 1.1.54.

Re: [boskid][1.1.53] Crash connecting rolling stock to neighbour while its being removed (on_train_created)

Posted: Fri Jan 28, 2022 1:28 pm
by robot256
Connecting/disconnecting wagons inside on_train_created already had the potential for infinite recursion, so there shouldn't be many besides Muppet bothered by this :? My mods that need to do things like this make a queue so that trains are only modified if they still exist at the next tick.