L0laapk3 / FactorioMaps

L0laapk3's FactorioMaps mod
https://mods.factorio.com/mod/L0laapk3_FactorioMaps
Other
122 stars 22 forks source link

use psutil.Process to track and terminate factorio #103

Closed saulrh closed 1 year ago

saulrh commented 3 years ago

Polling psutil.process_exists and manually invoking things like killall factorio can have some pretty nasty bugs if PIDs are reused or factorio exits before we expect. psutil protects us from all of this. It also means we don't have to write our own platform-specific code to kill factorio.

Should fix #102. That said, while this works on my machine (:tm:) I'm sure there was some reason #37 was raised, so it might be useful to track down some more information about how factorio behaves when invoked. It feels weird that factorio would try to daemonize itself, though.

saulrh commented 3 years ago

Another 6 hours running FactorioMaps flat-out on my biggest box says that this probably did not fix #102, but it still appears to have cleaned things up quite a bit. I'm starting to suspect that factorio is doing something like throwing away the termination request rather than queueing it for after screenshots are done.

Is there a way to figure out the current tick on a save without starting Factorio up to read and execute it? If we can figure out the current tick, we could probably use factorio's --until-tick argument to ensure termination.

L0laapk3 commented 3 years ago

Is there a way to figure out the current tick on a save without starting Factorio up to read and execute it? If we can figure out the current tick, we could probably use factorio's --until-tick argument to ensure termination.

I have explored this option, and iirc its possible to extract it from the raw savefile and I was able find it in there, however the reason I avoided this is that the savefile is just raw serialized data and the location of the tick value in a savefile could change with any minor update.

One thing I have not tried is stuff like --until-tick 0 and then hope it runs a single tick :p

L0laapk3 commented 3 years ago

I'm starting to suspect that factorio is doing something like throwing away the termination request rather than queueing it for after screenshots are done. From what I can tell, set_wait_for_screenshots_to_finish() simply blocks the main thread in the render function until all the screenshotting threads are finished. Normally, if you send 1 termination request to factorio, it will print something in the log along the lines of "finishing up and safe exit" and allows it to finish the current tick I believe, and if you send a second termination request it will print something else in the log and terminate immediately. I only send a single request and it should show up in the log (--verbosegame)