There are two solutions to an impending TPK like the one described. Even a TPK caused by freak bad luck is rarely a thing that just happens in an instant with no chance to react -UNLESS- the party was being reckless in the overestimation of their reserves (hp/spell slots/ ability charges/consumable magic items/etc). That TPK usually comes in the form of a slow moving trainwreck that just keeps rolling without anyone stopping to reconsider the current course of things & take action accordingly.Assuming running is possible, which it often isn't because most monsters move faster than most PCs.
As such, even a TPK caused by freak bad luck often indicates that the players were not reserving or failing to effectively deploy any hail Mary cards that should have been ready to go when things started to go sideways. Other times they are so poor at judging risk that they chose to continue as if nothing was at stake even when it should have been obvious that running away was the sane choice as Lanefan implied.
Even in a case where the monsters are faster than PCs who are being chased it still falls to the players to effectively run away by using their skills spells abilities & consumable resources. By doing that the players can make chasing them hard enough to get away or make not chasing them the more attractive option. "I move 30 feet & use my action to dash for another 30 feet" is rarely the only tool that players have at their disposal & shoud absolutely not be considered the case
As a GM it's incredibly frustrating to hand players what should have been a "break glass in case of emergency" type thing only to have them either use it as a normal thing (ie 5e's stupidly recharging wands) or continue to the death without ever scouring their inventory for doodads that could help the party steer the ship. Recognizing such needs & keeping tools in reserve for the possibility however requires a system where players feel those sorts of risks are even a plausible possibility, 5e has paradoxically poisoned that very well by ensuring that players consider it an implausible possibility.
* There's nothing wrong with it either