Miscellaneous Debris

Avner Kashtan's Frustrations and Exultations
Allowing timeout on long-running operations - possible bug

A while ago, I wrote about a simple pattern to allow us to put a timeout limitation on a long running operations. Simply put, it allows us to replace this:

public MyObject GetData()
{
   try
   {
      MyObject data = BigComponent.LongRunningOperation();
      return data;
   }
   catch (Exception ex)
   {
      // Log and rethrow.
      throw;
   }
}

with this:

public MyObject GetData()
{
   try
   {
      MyObject data;
      Thread t = new Thread(
                           delegate()
                           {
                              data = BigComponent.LongRunningOperation();
                           });
      t.Start();
      bool success = t.Join(timeoutForOperation);
      if (success)
      {
         reutrn data;
      }
      else
      {
         throw new TimeoutException();
      }
  
      return data;
   }
   catch (Exception ex)
   {
      // Log and rethrow.
      throw;
   }
}

This pattern, while simple at first, introduced a bug into my design that in retrospect should be glaringly obvious. The bug is that although my component previously caught exceptions in LongRunningOperation, this method is now called in a different thread and not handled by my try/catch. This is hard to see when using anonymous delegates, since you get the feeling that it's still a part of the parent method.

In .NET 2.0, the default behavior for an unhandled exception in a thread is to abort the whole process. In my case, it was an out-of-process COM+ service doing some heavy data crunching for us, and the result was a pop-up(!) on the server complaining that the COM Surrogate crashed. Took me a while to figure out what I did wrong.

One way of solving this is to simply wrap the code i nside the delegate with try/catch and swallow the exception. But that way I lose the information about the inner exception, which I want to propagate upwards. What I ended up doing is somewhat uglier, and involves passing the exception backwards the way I did with the data:

MyObject data;
Exception exceptionInProcess;
Thread t = new Thread(
                     delegate()
                     {
                        try
                        {
                           data = BigComponent.LongRunningOperation();
                        }
                        catch (Exception ex)
                        {
                           exceptionInProcess = ex;
                        });
...
if (exceptionInProcess != null)
{
   throw exceptionInProcess;
}

Published Monday, August 27, 2007 10:18 AM by Avner Kashtan

Filed under: , ,

Comments

# University Update-C#-Allowing timeout on long-running operations - possible bug@ Monday, August 27, 2007 11:52 AM

Pingback from  University Update-C#-Allowing timeout on long-running operations - possible bug

University Update-C#-Allowing timeout on long-running operations - possible bug

# re: Allowing timeout on long-running operations - possible bug@ Wednesday, October 14, 2009 3:49 PM

I took some pictures on my iphone and i want them onto my computer. does anyone know how to transfer them? thanks.

________________

<a href="www.youtube.com/watch iphone 3gs</a>

poori

# re: Allowing timeout on long-running operations - possible bug@ Wednesday, October 14, 2009 4:05 PM

I'm planning to buy Apple iPhone 8 GB which is launching soon in India. Can any one tell me what will be the price of it in Mumbai.And also let me know is it a good phone or not anyone have used it please share your experience. Also give price of 16GB. Please!

________________

<a href="www.youtube.com/watch iphone 3gs</a>

poori

# re: Allowing timeout on long-running operations - possible bug@ Tuesday, October 20, 2009 7:38 PM

What are the steps and processes to develop and submit an iPhone app to App store? I want to become a developer. Is it all about hardcore software programming like Java, or is there any software out there that can create apps by using a design view perhaps? Becuase I see 10'000's of apps and a lot of pointless ones, and am pretty sure people wouldnt have wasted weeks making a pointless app in hard java code? Cheers

________________

<a href="http://unlockiphone3g.webs.com">how to unlock iphone</a>

poori

# re: Allowing timeout on long-running operations - possible bug@ Tuesday, October 20, 2009 7:45 PM

I got an iPhone 3Gs and I didn't set up an Apple/iTunes account. How can I download apps without one. I saw you can do that but I forgot how.

________________

<a href="http://unlockiphone3g.webs.com">how to unlock iphone</a>

poori

# re: Allowing timeout on long-running operations - possible bug@ Wednesday, November 18, 2009 12:31 AM

i was thinking about gettin the <a href="unlockiphone22.com/">click here</a>  for christmas, but i heard some bad things about it :S can any one tell me a few things about it? x

poori

# re: Allowing timeout on long-running operations - possible bug@ Wednesday, November 18, 2009 1:31 AM

I just got an iphone 3g and I got so excited that i filled it with loads of applications and songs (about $30 worth). The other day when i plugged my <a href="unlockiphone22.com/">click here</a>  into the computer on itunes it said to reset the iphone to factory settings. I dont want to loose all my apps so what can i do? PLEASE REPLY A.S.A.P.

poori

Leave a Comment

(required) 
(required) 
(optional)
(required)