Forum Bugs

C# - Prince doesn't start reliably while multithreading

Zoba
Hi,

I have a couple thousand of documents i want to generate. When I run the loop in a simple foreach everything works fine. Now I've tried to increase performance by using a parallel.foreach loop and it works somewhat the same except multiple times faster (as expected) but every now and then (roughly 1 in 10000) Prince won't start up and return the following error instead. How can i assure Program start before my .Convert() call?

System.ApplicationException: Error starting Prince: C:\Program Files (x86)\Prince\engine\bin\prince.exe
at Prince.StartPrince(String args)
at Prince.Convert1(String args)
at Prince.Convert(String xmlPath, String pdfPath)
mikeday
Any idea how many Prince processes it is trying to launch in parallel? Perhaps it is hitting a file handle limit?
mikeday
Also are you creating a separate Prince object for each thread?
Zoba
I'm calling a static class that calls a new instance of prince with each run. I don't know how many instances get spawned but i'll check.

EDIT: Even as a non static method with a new instance per job this behaviour still exists.

Edited by Zoba

Zoba
I've limited the DegreeOfParallelism to 8 and the error persists. Now i'm trying non-static with a limited degree.

Is there any other way to make sure the program runs?
mikeday
I'm not sure exactly what is causing this. This is the significant code in the StartPrince() method:
if (!pr.HasExited)
{
    return pr;
}

throw new ApplicationException("Error starting Prince: " + mPrincePath);

But why is the process exiting immediately? Perhaps you could try changing the exception to include the pr.ExitCode value and see if that is informative?
Zoba
with a non-static method, limited to 8 simultaneous threads i don't encounter that error. So there indeed seems to be a bottleneck somewhere.. i'll keep looking
Zoba
On my production-server with 4 threads the problem sporadically persists!

Edited by Zoba

mikeday
Can you try logging the pr.ExitCode value?
Zoba
idk how i'd be able to log the pr.exitCode - the convert function solely return a bool, right?
mikeday
Yes, can you include the C# file directly in your project?
Zoba
until now i've used the nuget wrapper (v.9.00) - i'll use the source file now and will implement pr.exitcode into the log - i guess https://www.nuget.org/packages/Prince/ isn't your official build - right?

Edited by Zoba

Zoba
System.ApplicationException: Error starting Prince: C:\Program Files (x86)\Prince\engine\bin\prince.exe ExitCode:0
bei Prince.StartPrince(String args) in C:\Projekte\NancyViasVerbindung\NancyEmptyAspNetHost1\Prince.cs:Zeile 955.
bei Prince.Convert1(String args, List`1 dats)

So ExitCode:0 isn't that helpful i guess... but that's what i get
mikeday
That doesn't give us much of a clue, does it. :D

We will need to find if there is another way to launch the Prince process that gives more diagnostics, as I can't tell from this whether the program is even running and why not.
Zoba
Okay, i think i've figured it out. The creation of pdf files works most of the time, but sometimes prince will return that it couldn't quit (i have the pr.convert() in a try/catch block) => Exit(0) - which should be the default case anyway.
Now i've added a check to look if the file i wanted to create was created in the catch-block. If that's the case i'm ignoring the returned exception and just continue - i'll try to test if that fixes the behaviour.