C
C#4w ago
zed

✅ Stack overflow, but can't change the code

My issue is that I have a calculation that overflows the stack by a little bit (it's deterministic and will never increase past around 1.15 MB). I can't change the code to not do that, unless business approves some funds for serious refactoring, so the question is, how to proceed. I wanted to increase the stack size to 2MB for the one calculation thread, which seems pretty reasonable. But not sure what the best way to do that would be. Starting a new thread with the specified amount will mean I have to do some extra exception processing. How would I do that? Try/catch around the thread's code, and hoist the exception object out of the stack? Then rethrow from the outside? I will lose internal stack information that way, no? Throw a new exception on the outside, with the exception object as the inner exception? A bit more annoying to handle, but could work... are there any better ideas? This isn't a normal "thread" scenario, since I start the thread and immediately join it, I don't actually want anything to run in parallel, I only do it to increase the stack size. So does this give me other options? Runtime cost of spawning/joining a new thread in this scenario is irrelevant. Alternatively, is there a better way to handle the stack size increase?
6 Replies
canton7
canton74w ago
You can use ExceptionDispatchInfo to capture and rethrow an exception without losing the stack trace. It's what all of the async machinery uses
zed
zedOP4w ago
Oh that sounds good, thanks I'll experiment with that
canton7
canton74w ago
Or use a TaskCompletionSource I guess
zed
zedOP4w ago
Too late! Both for that, and this reply lol, sorry, but your suggestion worked great 👍
canton7
canton74w ago
Cool, glad to hear! $close
MODiX
MODiX4w ago
If you have no further questions, please use /close to mark the forum thread as answered

Did you find this page helpful?