Curious about long-running background tasks an AppDomain shutdown

Topics: Core, General, Troubleshooting
Developer
Mar 30, 2012 at 11:51 PM
Edited Mar 30, 2012 at 11:58 PM

I was wondering about that after coming across an old post by Phil Haack: http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx

Currently, the AppDomain doesn't know about the background tasks, so there is a risk they may get interrupted if AppDomain gets torn down. For long running tasks this might be a problem and corrupt the data being processed (if eg. the interruption would happen in the middle of some operation).

That looks like an issue. Correct me please if I'm wrong.

Coordinator
Apr 1, 2012 at 3:22 AM

Transactions should take care of data corruptions.

Developer
Apr 5, 2012 at 6:32 AM

Not quite - what about long tasks that do not rely on database transactions, like eg. involving talking to web services, sending e-mails? Having a stop event would be good to have, to allow interrupting a task nicely, do some cleanup etc..

Developer
Apr 23, 2012 at 10:43 AM
Edited Apr 23, 2012 at 10:48 AM

Aren't finally blocks run when the AppDomain shuts down? That still wouldn't protect from e.g. powering off the server, but still...

Developer
Apr 23, 2012 at 2:49 PM

@Piedone Good point - yes the unexecuted finalizers are being executed on Thread.Abort. This may help to some degree.