1

We use a third party library to manipulate Pdf's. Our application runs as a Windows service and handles thousands of files every month. Once in a while someone uploads a malformed Pdf, which makes the library run amok and eventually throw a StackOverflowException.

The library manufacturer has not fixed the error over the last 2 years, and we can't have our production crash when someone feels like it.

Automatically restarting the service does not seem like an option, as the application would then retry the malformed file. Since we process many files in parallel, we cannot know which is the malformed when starting.

Since stackoverflows can't be catched by default, I would like to know if I can tweak the CLR of the service to catch the exception anyway.

Andreas
  • 1,061
  • 1
  • 11
  • 26
  • Is it clear what kinds of malformations cause the service to lock up? Can you perform a 'health check' on files before processing them? – Marisa Oct 04 '17 at 18:36
  • No, the file even displays fine in Acrobat. Since the library is closed source, we can't know the reason and prevent it. Would have been nice though. – Andreas Oct 04 '17 at 18:41
  • @Simon Sure we notified the manufacturer. An unstable state would probably be fine, if we could just log which thread handled the malformed file. Then we could mark it, restart and ignore it. An application with a hosted CLR may change the behaviour, but I don't know if this applies to services. https://msdn.microsoft.com/en-us/library/w6sxk224.aspx – Andreas Oct 04 '17 at 18:46
  • Although it's not likely to absolutely solve your problem, you could do two things to reduce its occurrence: 1) increase the stack size of threads you create to run the code that calls that library, and 2) create a watcher thread to try and detect and terminate threads that are headed toward overflow (see the second answer to this question: [How do I prevent and or handle a StackOverflowException?](https://stackoverflow.com/questions/206820/how-do-i-prevent-and-or-handle-a-stackoverflowexception) – hatchet - done with SOverflow Oct 04 '17 at 19:06

1 Answers1

3

You could re-architect the application to create new child processes, each with its own instance of the library, to perform the work.

Most importantly, with this approach the failing instance doesn't crash the entire application or take the other child processes down with it. You also have the advantage that the manager process can keep track of which files are in progress (and on which process) so it would know which files to not retry after a failure.

Jaquez
  • 920
  • 1
  • 13
  • 21