0

An ASP.NET invoice application has a save button, that triggers (among other things) the generation of a pdf file on disk using the form data, the userID etc.

An excerpt from the GeneratePDF method:

ManualResetEvent generateInvoiceEvent;
generateInvoiceEvent = new ManualResetEvent(false);
CultureInfo currentCulture = Thread.CurrentThread.CurrentCulture;
ThreadPool.QueueUserWorkItem(delegate
    {
        Thread.CurrentThread.CurrentCulture = currentCulture;
        Thread.CurrentThread.CurrentUICulture = currentCulture;
        InvoicePdfGenerator generator = new InvoicePdfGenerator();
        generator.SetTranslationFile(GetFileSharedFilePath());
        generator.DrawPdf(invoiceXml, pdfFilePath, invoiceLanguage.ValueCode,
            currentCulture, invoiceLayoutXml, imageRootFolder);
        generateInvoiceEvent.Set(); //Signal completion of invoice generation
    }
);
generateInvoiceEvent.WaitOne();

For some reason, the WaitOne() method throws an AccessViolation most of the time but not always, but I don't seem to able to debug further than that. Beyond that is mscorlib.

Three things are important to understand in this story:

  • This is not my code
  • I have little knowledge about threading. It's just something I rarely come into contact with in my job.
  • This worked fine up until a few weeks ago on my development machine. Chances are, that some external factor is causing this, but I cannot find what or why. There are no code updates between then and now.

It's quite possible, that this question needs additional info. As my understanding of threading is severely limited, I will update my question at your request.

Wim Ombelets
  • 5,097
  • 3
  • 39
  • 55
  • The problem indeed is fishy, never seen that before. But why are you doing an async request here anyway, when you're synchronously waiting for the result? – PMF Oct 30 '13 at 11:57
  • @PMF as I said, this is legacy code that suddenly throws an exception. Your guess is as good as mine. The person who wrote this, doesn't work here anymore. – Wim Ombelets Oct 30 '13 at 12:26
  • I would guess that the symptom you're observing is not directly related to the issue. It's probably some memory corruption in unmanaged code that just by accident crashes at this point. Have you tried i.e. commenting out the DrawPdf call (if possible)? – PMF Oct 30 '13 at 13:10
  • @PMF yes, the `DrawPdf` method is not the problem. As my own answer states, I managed to make the issue "go away", but I'm none the wiser – Wim Ombelets Oct 30 '13 at 13:17
  • But it obviously must be something that happens inside this function. Try commenting it out one-by-one. – PMF Oct 30 '13 at 13:39

1 Answers1

0

Apparently (and don't shoot me here, I realize this is horrible, HORRIBLE coding because it resolves nothing except not take the process down) I can make the problem go away by enclosing the aforementioned code block in a try-catch.
Sadly, this doesn't reveal any inkling about what might be causing the AccessViolation.

Wim Ombelets
  • 5,097
  • 3
  • 39
  • 55
  • Yes this is horrible. Suppressing the exception with `try-catch` also may mean you won't find out which invoices fail to get written. – groverboy Oct 30 '13 at 14:12
  • Did `AccessViolation` show up when you started using the code in a normal (not development) environment? I suspect there's a resource shared by multiple threads but no protection from concurrent access. – groverboy Oct 30 '13 at 14:21
  • I read somewhere that AccessViolationException cannot occur in managed code, but may be trapped by the runtime for unmanaged code. Therefore I'd say `WaitOne` is not the problem; the exception probably occurs during a thread transition from unmanaged code. – groverboy Oct 31 '13 at 10:20
  • Note that in .NET 4+ the process will terminate after `AccessViolationException`. Your exception handler will be ignored. This is among a group of exception types considered unrecoverable. In .NET 4+ you will need to customise the app config to override this behaviour. – groverboy Oct 31 '13 at 23:30
  • The group is called Corrupted State Exceptions, read about them [here](http://msdn.microsoft.com/en-us/magazine/dd419661.aspx). – groverboy Oct 31 '13 at 23:46