views:

28

answers:

1

My question centers on some Parallel.ForEach code that used to work without fail, and now that our database has grown to 5 times as large, it breaks almost regularly.

Parallel.ForEach<Stock_ListAllResult>( lbStockList.SelectedItems.Cast<Stock_ListAllResult>(), SelectedStock =>
{
    ComputeTipDown( SelectedStock.Symbol );
} );

The ComputeTipDown() method gets all daily stock tic data for the symbol, and iterates through each day, gets yesterday's data and does a few calculations and then inserts them into the database for each day.

We use this rarely to recalculate static data values when a formula changes.

The exception is this:

alt text

The database we are hitting has 16 gigs of ram and is a dual quad-core and nobody was using the system while I was recalculating. The machine running the application to regenerate the code is a laptop with 12 gigs of ram with a hyper-threaded octal-core. So there was no obvious resource contention.

This is my foray into using .NET 4 and parallel processing, so I am wondering if there is something I am missing. Any thoughts would be welcomed.

+1  A: 

This looks like you received an AggregateException, which is what Parallel.ForEach will raise if any of the loop body methods raise an exception.

If you debug this, you should be able to look at the InnerExceptions to see the actual exceptions thrown. It looks like ComputeTipDown raised an exception in one or more of your iterations, causing this to occur.

Reed Copsey
Reed, thank you very much for the suggestion, I'll put a breakpoint in the catch for ComputeTipDown and will see what happens.
MikeMalter
@MikeMalter: You can also wrap the Parallel.ForEach in a try/catch, and catch AggregateException. This will give you the details at that point...
Reed Copsey
I read that by clicking on the AggregateException link you provided in your answer. I just got another exception, and this time it was the "real" one. Max pool size was reached. Interesting. I upgraded the server this weekend to 2008 r2 and SQL Server to 2008 R2, and it was after that this started happing with regular frequency.
MikeMalter
@MikeMalter: Are you running this as an x64 process? In x86, the default max thread size is quite a bit smaller...
Reed Copsey
Reed, it is x64.
MikeMalter
@MikeMalter: Is "ComputeTipDown" using a database internally? If so, you're probably hitting the max db connection pool. If that's the problem, you may want to set ParallelOptions.MaxDegreeOfParalellism and use a different overload of ForEach: http://msdn.microsoft.com/en-us/library/system.threading.tasks.paralleloptions.maxdegreeofparallelism.aspx
Reed Copsey