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

This subforum contains all the issues which we already resolved.
Post Reply
User avatar
Muppet9010
Filter Inserter
Filter Inserter
Posts: 278
Joined: Sat Dec 09, 2017 6:01 pm
Contact:

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

Post 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 :)
Attachments
factorio-dump-current.dmp
(796.77 KiB) Downloaded 94 times
factorio-current.log
(20.84 KiB) Downloaded 97 times
loco remove.lua
(675 Bytes) Downloaded 95 times
test.zip
(1.21 MiB) Downloaded 90 times

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2250
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

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

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

robot256
Filter Inserter
Filter Inserter
Posts: 596
Joined: Sun Mar 17, 2019 1:52 am
Contact:

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

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

Post Reply

Return to “Resolved Problems and Bugs”