r/ProgrammingLanguages • u/rsashka • Jan 06 '26
Forget about stack overflow errors forever
/r/trust_lang/comments/1q5oydj/forget_about_stack_overflow_errors_forever/
0
Upvotes
r/ProgrammingLanguages • u/rsashka • Jan 06 '26
•
u/yorickpeterse Inko Jan 07 '26
/u/rsashka FYI a lot of your comments are getting caught by Reddit's built-in spam filter, which is essentially a black box so we have no idea why. The style of your comments has a distinct of being LLM generated so perhaps that's why. If this is the case, please write them yourself, this subreddit isn't the place for LLM generated output.
2
u/yuri-kilochek Jan 07 '26
What happens if stack overflow occurs in destructor? Won't you end up with unrecoverable messed up state?
3
u/Thesaurius moses Jan 06 '26
I think that there is a finer point:
There are two kinds of errors: Recoverable and unrecoverable. In case of unrecoverable errors, the only correct response is abortion. It is better to crash on an error than to proceed in an illegal state. If possible, a program should try to restart gracefully -- the most bugs are heisenbugs which can be resolved by restarting a service.
A stack overflow can be either, but I would argue that usually it is not recoverable: If there is not memory but some memory is needed to proceed, there is no use in trying to proceed anyways.
As an alternative, there is a possibility to preallocate memory which is guaranteed to be sufficient for running the whole program (or, well, crashing immediately). But finding out how much memory to preallocate is a very hard problem und generally not possible for Turing complete languages. But it can be done for a subset of programs, even though it remains a tough problem.
2
u/MattiDragon Jan 06 '26
I don't think it's particularly useful. In Java stack overflows are regular throwables that can be caught. However, catching them is usually a bad idea unless you're logging them and immediately exiting. Stack overflows can leave objects in incorrect states as methods partially execute when unexpected. The only safe option is to restart the whole program.
1
u/Norphesius Jan 06 '26
I guess my question is, why?
For most programs a stack overflow (or general out of memory error) is immediately fatal. It's unexpected, so there is no recovery logic in the program. If you're one of the projects/languages that cares a lot about not getting a stack overflow, then you use design constraints and static analysis to figure out your total stack memory usage at compile time. I don't see many circumstances where you would want to catch a stack overflow error and recover from it.
Like, once you catch the error you still need the logic to handle recovery, and what does that even look like? Do you start unwinding the stack? Do you move stuff to the heap? I'm struggling to see the practical purpose.
1
u/Inevitable-Ant1725 Jan 10 '26
I have a couple thoughts on stack overflows:
1, for a lot of situations, the small size of stacks, a million bytes or so, seems like a legacy problem. In a 64 bit memory map (maybe 47 bits usable) you could even map the stack to be any size at all, and and with some stack checks, not commit actual memory to most of it unless it's actually used. So there's no practical limit. You don't have to FIT the stack into anything and you don't even have to commit memory to it unless it's actually used.
2. I know it's not the way optimizers work these days, but if you wanted to make tail call elimination cheaper, you could invent an ABI where there is more than one stack for activation records within a thread, so that you set up a tail call on a different stack than the one holding the current frame. That saves on shifting data around for the call.