Page 1 of 1

Crash on LuaEntity.get_train_stop_trains() + train change

Posted: Wed Jul 25, 2018 11:34 pm
by shanemadden
I've found a specific case where a call to get_train_stop_trains() will cause a crash with specific conditions from a mod.

Code: Select all

Factorio crashed. Generating symbolized stacktrace, please wait ...
c:\cygwin64\tmp\factorio-build-rlyqnq\libraries\stackwalker\stackwalker.cpp (924): StackWalker::ShowCallstack
c:\cygwin64\tmp\factorio-build-rlyqnq\src\util\logger.cpp (408): Logger::writeStacktrace
c:\cygwin64\tmp\factorio-build-rlyqnq\src\util\logger.cpp (521): Logger::logStacktrace
c:\cygwin64\tmp\factorio-build-rlyqnq\src\util\logging.cpp (89): Logging::logAndAbortOrThrow
c:\cygwin64\tmp\factorio-build-rlyqnq\src\util\logging.cpp (83): Logging::logAndAbortOrThrow
c:\cygwin64\tmp\factorio-build-rlyqnq\src\script\luatrain.cpp (77): LuaTrain::LuaTrain
c:\cygwin64\tmp\factorio-build-rlyqnq\src\script\luaentity.cpp (4130): LuaEntity::luaGetTrainStopTrains
c:\cygwin64\tmp\factorio-build-rlyqnq\src\script\luabinder.hpp (326): LuaBinder<LuaLogisticPoint>::callWrapper
c:\cygwin64\tmp\factorio-build-rlyqnq\libraries\lua\ldo.c (319): luaD_precall
c:\cygwin64\tmp\factorio-build-rlyqnq\libraries\lua\lvm.c (710): luaV_execute
c:\cygwin64\tmp\factorio-build-rlyqnq\libraries\lua\lapi.c (1018): f_call
c:\cygwin64\tmp\factorio-build-rlyqnq\libraries\lua\ldo.c (131): luaD_rawrunprotected
c:\cygwin64\tmp\factorio-build-rlyqnq\libraries\lua\ldo.c (590): luaD_pcall
c:\cygwin64\tmp\factorio-build-rlyqnq\src\script\luagamescript.cpp (4362): LuaGameScript::signallingPCall
c:\cygwin64\tmp\factorio-build-rlyqnq\src\script\luagamescript.cpp (796): LuaGameScript::runNthTickHandler
c:\cygwin64\tmp\factorio-build-rlyqnq\src\script\luaeventdispatcher.cpp (148): LuaEventDispatcher::dispatch
c:\cygwin64\tmp\factorio-build-rlyqnq\src\scenario\scenario.cpp (880): Scenario::update
c:\cygwin64\tmp\factorio-build-rlyqnq\src\mainloop.cpp (1007): MainLoop::gameUpdateStep
c:\cygwin64\tmp\factorio-build-rlyqnq\src\mainloop.cpp (874): MainLoop::gameUpdateLoop
c:\cygwin64\tmp\factorio-build-rlyqnq\src\util\workerthread.cpp (36): WorkerThread::loop
c:\program files (x86)\microsoft visual studio\2017\buildtools\vc\tools\msvc\14.12.25827\include\thr\xthread (232): std::_LaunchPad<std::unique_ptr<std::tuple<void (__cdecl RouterBase::*)(void) __ptr64,ServerRouter * __ptr64>,std::default_delete<std::tuple<void (__cdecl RouterBase::*)(void) __ptr64,ServerRouter * __ptr64> > > >::_Go
c:\program files (x86)\microsoft visual studio\2017\buildtools\vc\tools\msvc\14.12.25827\include\thr\xthread (211): std::_Pad::_Call_func
d:\th\minkernel\crts\ucrt\src\appcrt\startup\thread.cpp (115): thread_start<unsigned int (__cdecl*)(void * __ptr64)>
ERROR: SymGetLineFromAddr64, GetLastError: 487 (Address: 00007FFADD4D3034)
00007FFADD4D3034 (KERNEL32): (filename not available): BaseThreadInitThunk
ERROR: SymGetLineFromAddr64, GetLastError: 487 (Address: 00007FFADDA41431)
00007FFADDA41431 (ntdll): (filename not available): RtlUserThreadStart
Stack trace logging done
  31.209 Error LuaTrain.cpp:77: Invalid state for train.
Logger::writeStacktrace skipped.
  31.209 Error CrashHandler.cpp:174: Map tick at moment of crash: 18530
I'm not completely certain of the exact conditions that trigger the issue, but the way this mod is triggering it is by calling get_train_stop_trains() against a station after having called .destroy() on one of the locomotives in a train that's scheduled to stop there during the same event handler - in this case it's the front (and first-built) carriage in the train, I'm unsure if deleting other carriages in the same way causes the same issue.

The crash is triggered by this line https://github.com/shanemadden/factorio ... .lua#L1037 (verified by adding log() calls - simply trying to print get_train_stop_trains() causes the crash) in cases where the locomotive destroy()'ed on line 991 had that station in its train's schedule prior to the event handler firing.

I've worked around letting this unintentionally crash users' games in the current version of the mod by just clearing the schedule from this train in the case where it might have picked one up, but manually adding the stop to the schedule during the train's construction process will still trigger the crash.

The attached save is set up to crash a few seconds after being loaded (just sync mods first) - the train currently under construction in the save has had the test station added to its schedule by hand, which triggers the crash as the train finishes construction (the orange builder locomotive is destroyed then the get_train_stop_trains() call triggers the crash).

Re: Crash on LuaEntity.get_train_stop_trains() + train change

Posted: Tue Jul 31, 2018 3:44 am
by Rseding91
Thanks for the report. It's now fixed for 0.17.