Page 1 of 1

[kovarex] [0.17.17-2] Trains collide when using stations disabled via circuits

Posted: Fri Mar 29, 2019 5:08 am
by Darkwar
What did you do?
Using a base with roundabouts/track junctions and Train Stations which are often enabled/disabled by circuit network.


What happened?
Trains occasionally collide with one another themselves while going through roundabouts, and often create deadlocks in RCS controlled junctions.

What I think is happening is that multiple trains are passing through a junction in a way such that both trains have selected paths through the junction that don't collide with each other, so they proceed through. Then, while navigating through the intersection, one or more train stations that the involved trains were pathing towards becomes inactive, and the train(s) pick new routes which are not clear or collision safe.

This is possibly exacerbated by a separate possible bug where trains which have entered a segment of blocks linked with rail-chain-signals doesn't seem to care if new trains enter, even if there is room to stop/re-route. See attachment "Factorio Signal Bug 2.gif" for demo. (I say possible bug, because i'm not sure if it's technically a bug, practically speaking this should never happen except for really weird circumstances, like manually placing train cars. Even with poorly designed rails, the train system should always block any train from entering any segment intersecting anything in the path of rail-chain-signals. Maybe it's different if the two trains are _already_ in intersecting segments but on paths that don't intersect? I'm not sure, and all my experiments to dig deeper into this particular aspect have failed.) . Possibly trains which have entered the junctions rail-chain-signal segments don't see other trains in other parts of the segment, and re-route dangerously?

Note: I use primarily roundabouts for my train junctions., however I don't think that is strictly relevant. Based on my understanding of this bug, there is no reason why this couldn't happen on any other junction which is using rail-chain-signals. Using roundabouts, Trains are able to path through and collide with themselves, however using non-roundabout junctions don't seem to have this collision problem. Instead, non-roundabout junctions experience frequent deadlocks.


What did you expect to happen instead? It might be obvious to you, but do it anyway!
I expected trains to re-route more gracefully/safely, and not collide with or block one another.


Does it happen always, once, or sometimes?
It happens somewhat rarely in my primary games, maybe once or twice over a 6-hour period. However, I created a worst-case-scenario simulation (See below) in which it can be observed happening extremely frequently.

Possibly related to this report >> viewtopic.php?f=29&t=58460&hilit=train+collision << at least, some parts, there are multiple similar occurrences described in that thread, which is over a year old.


Write down the steps to reproduce the bug if you know them.
Build a system of rails with stations that are disabled via circuit network. For instance, in my current save, my factory trains run in a fetch pattern; they will sit in my base until all of the cargo has been unloaded, then they head to an ore station to fill up. The ore stations I use are disabled until they have buffered enough ore to fill up a train. That way, the trains don't all just pile up at the nearest ore station, they will skip empty stations and go to farther away ones to fill up instead. Often times, a train on its way to a particular station will find that that station has been drained/disabled by another train which got there first, and it will re-route to a new station. If it's in a junction when it re-routes, bad things happen. I have seen trains switch courses and collide with each other, and in the case of roundabouts I've even seen them collide with themselves, presumably re-routing through a junction exit which they had already passed.

I've included a setup demo with 40-trains seeking a network of train stations which enable/disable somewhat realistically (They are enabled for a period of time, then are disabled the rest of the time), which is on par with the setup I have in my main bases where I experience this issue, albeit designed in such a way as to increase the frequency of collisions (1 per about 5-10 minutes)
https://drive.google.com/file/d/1TwhJ2a ... sp=sharing

I've also included a worst-case-scenario demo with 40-trains seeking a network of train stations which rapidly enable/disable randomly, and within two minutes or so all of the trains have reduced each other to rubble.
https://drive.google.com/file/d/1IwV6_a ... sp=sharing

In both scenarios, the player starts near a Constant combinator (Marked on the map as "Train Controller") which should be turned off to start the trains running.

Note: The latest experimental (0.17) has serious perf problems on most of my bases, so I play mostly in latest stable (0.16.51), so most of my testing/experiences are _not_ from latest experimental, so experiences there may differ, however I did load my demo saves in 0.17 and confirmed that the bug is still an issue in the latest experimental build.

Re: [0.17.17-2] Trains collide when using stations disabled via circuits

Posted: Fri Mar 29, 2019 5:35 am
by Darkwar
For the curious, the second demo takes 40 4:6 trains running on fully-automatic pathfinding, no player trains, and within a couple minutes reduces them to this:

It's fun to watch, I haven't seen trains go this badly before. For the most part they end up clipping their own tails and then, because they lost an attached car, switch from automatic to manual and coast into the next intersection on pure momentum, hitting or being hit by other trains, etc.

Re: [0.17.17-2] Trains collide when using stations disabled via circuits

Posted: Sun Mar 31, 2019 8:18 pm
by Darkwar
Update: I modified the scenario a bit to use only non-roundabout intersections, and there were no more collisions (At least, I haven't seen any collisions yet. It doesn't mean they can't/wont still happen rarely). The primary culprit from what I saw was Trains hitting themselves, and now they can't anymore.

Instead, though they are super-easily getting stuck in the intersections and creating some nasty deadlocks. Its common to see trains creeping through intersections which are not clear, as they are re-routing frequently and changing the path they want to take through the intersection even when they're part way through, and getting stuck on other trains.

New scenario/save file:
https://drive.google.com/file/d/1iEoo-_ ... sp=sharing

Re: [0.17.17-2] Trains collide when using stations disabled via circuits

Posted: Sun Mar 31, 2019 8:42 pm
by quyxkh
I think trains shouldn't repath while the last signal they passed was a chain, but then, I told my kid growing up I didn't ever want to hear a sentence containing the word "should".

Re: [kovarex] [0.17.17-2] Trains collide when using stations disabled via circuits

Posted: Tue May 07, 2019 3:15 pm
by kovarex
I'm working on a solution now, but I need to add tests and make it properly, as the change isn't that simple.

The current state is: When the train is in a chain signal section, it will not spontaneously recalculate path to avoid problems like this.
But (as you pointed out), this doesn't stop the train from repathing when stations are enabled/disabled or when player adds a temporary stop/rail to the schedule.

The solution I'm working on is, that when a train is in a chain signal section and needs to recalculate path, it will put a constraint on the path finder: Until the chain section is exited, it can't path through a reserved block.
In other words, it needs to find a path that will not leave the train stuck in the chain segment. The resulting path could follow the original path through the chain section, or it might find another one.
This change could (apart fixing this bug) also remove the current contraint of no spontaneous repathing when in chain section, as, with the constraint, it will not break anything, and can possibly find a better valid exit from the chain section.

P.S. To make it clear, it will not fix the roundabound problem of train crashing to itself, that might even be considered as not a bug, as your roundabout is just supposed to be big enough, or you can avoid them.

Re: [kovarex] [0.17.17-2] Trains collide when using stations disabled via circuits

Posted: Wed May 08, 2019 10:32 am
by kovarex
Fixed for 0.17.38, thanks for the help.

Re: [kovarex] [0.17.17-2] Trains collide when using stations disabled via circuits

Posted: Wed May 08, 2019 4:27 pm
by robot256
This is an impressive corner case to find, and probably the perfect solution to implement. But what happens if the train tries to repath through an unoccupied exit to the chain block, but there is none? For example if its original path, and only unoccupied exit to the circle, was to a dead-end station, and it can't repath to another station without going through an occupied exit to the roundabout? This will be avoided for properly designed networks, but is another corner case nonetheless.

Re: [kovarex] [0.17.17-2] Trains collide when using stations disabled via circuits

Posted: Wed May 08, 2019 6:57 pm
by kovarex
robot256 wrote:
Wed May 08, 2019 4:27 pm
This is an impressive corner case to find, and probably the perfect solution to implement. But what happens if the train tries to repath through an unoccupied exit to the chain block, but there is none? For example if its original path, and only unoccupied exit to the circle, was to a dead-end station, and it can't repath to another station without going through an occupied exit to the roundabout? This will be avoided for properly designed networks, but is another corner case nonetheless.
The pathing will fail (no path found), and the train will act as when no path is found. But in this state, it should lose the flag of being in a chain signal section, so the next time it tries to search a path, it will search it without the constraint.

Re: [kovarex] [0.17.17-2] Trains collide when using stations disabled via circuits

Posted: Fri May 10, 2019 5:55 pm
by knightelite
So does this mean that there's a chance that now a train can end up deadlocking in the same scenario due to no-pathing while in the middle of an intersection?

Also, does this change at all impact the ease of making the change discussed in this thread?

Re: [kovarex] [0.17.17-2] Trains collide when using stations disabled via circuits

Posted: Sun May 12, 2019 5:47 pm
by kovarex
knightelite wrote:
Fri May 10, 2019 5:55 pm
So does this mean that there's a chance that now a train can end up deadlocking in the same scenario due to no-pathing while in the middle of an intersection?

Also, does this change at all impact the ease of making the change discussed in this thread?
It could, if the rail system had weird kind of dead-ends. I would consider doing some kind of "no-path-stopping" state, which would pathfind any valid path and follow it until it stops safely, as ketting no-path basically means, that the train will go straight ignoring any signals.