smsohan / MvcMailer

A Mailer for ASP.Net MVC that forms the Email Body using MVC Views (Razor etc.) following Ruby on Rails ActionMailer style
MIT License
582 stars 178 forks source link

SendAsync() failing semi-silently #93

Open JayVDZ opened 11 years ago

JayVDZ commented 11 years ago

SendAsync() was working for me, but has recently started failing seemingly silently, but then I discovered some async/task related exceptions in the event log that seem to occur around the same time mails are meant to be sent out.

For now I've stopped using SendAsync() and am just using Send() and that's working fine. Here's the event log entries. Does anyone know what might be wrong?


Faulting application name: w3wp.exe, version: 8.0.9200.16384, time stamp: 0x50108835 Faulting module name: KERNELBASE.dll, version: 6.2.9200.16451, time stamp: 0x50988aa6 Exception code: 0xe0434352 Fault offset: 0x000000000003811c Faulting process id: 0xe64 Faulting application start time: 0x01ce33ca1b29a6f5 Faulting application path: c:\windows\system32\inetsrv\w3wp.exe Faulting module path: C:\Windows\system32\KERNELBASE.dll Report Id: 646c29a6-9fbd-11e2-93ed-003048fccbf3 Faulting package full name:

Faulting package-relative application ID:

Application: w3wp.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.NullReferenceException Stack: at System.Threading.Tasks.AwaitTaskContinuation.b__1(System.Object) at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()

at System.Threading.ThreadPoolWorkQueue.Dispatch()

An unhandled exception occurred and the process was terminated.

Application ID: /LM/W3SVC/3/ROOT

Process ID: 3684

Exception: System.NullReferenceException

Message: Object reference not set to an instance of an object.

StackTrace: at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext) at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext) at System.Web.LegacyAspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state) at System.Web.LegacyAspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state) at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task& currentTask) --- End of stack trace from previous location where exception was thrown --- at System.Threading.Tasks.AwaitTaskContinuation.b__1(Object s) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()

at System.Threading.ThreadPoolWorkQueue.Dispatch()

Also, this one isn't in the same time-frame, but does precede the exceptions so I thought I'd include it for completeness in case it's related somehow:

A process serving application pool 'xxx' suffered a fatal communication error with the Windows Process Activation Service. The process id was '2464'. The data field contains the error number.

smsohan commented 11 years ago

What .net version is your project using? If its 4.0, please keep using MvcMailer v 4.0 — Sent from Mailbox for iPhone

On Mon, Apr 8, 2013 at 1:12 AM, Jay Van Der Zant notifications@github.com wrote:

SendAsync() was working for me, but has recently started failing seemingly silently, but then I discovered some async/task related exceptions in the event log that seem to occur around the same time mails are meant to be sent out.

For now I've stopped using SendAsync() and am just using Send() and that's working fine. Here's the event log entries. Does anyone know what might be wrong?

Faulting application name: w3wp.exe, version: 8.0.9200.16384, time stamp: 0x50108835 Faulting module name: KERNELBASE.dll, version: 6.2.9200.16451, time stamp: 0x50988aa6 Exception code: 0xe0434352 Fault offset: 0x000000000003811c Faulting process id: 0xe64 Faulting application start time: 0x01ce33ca1b29a6f5 Faulting application path: c:\windows\system32\inetsrv\w3wp.exe Faulting module path: C:\Windows\system32\KERNELBASE.dll Report Id: 646c29a6-9fbd-11e2-93ed-003048fccbf3 Faulting package full name:

Faulting package-relative application ID:

Application: w3wp.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.NullReferenceException Stack: at System.Threading.Tasks.AwaitTaskContinuation.b__1(System.Object) at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()

at System.Threading.ThreadPoolWorkQueue.Dispatch()

An unhandled exception occurred and the process was terminated. Application ID: /LM/W3SVC/3/ROOT Process ID: 3684 Exception: System.NullReferenceException Message: Object reference not set to an instance of an object. StackTrace: at System.Web.ThreadContext.AssociateWithCurrentThread(Boolean setImpersonationContext) at System.Web.HttpApplication.OnThreadEnterPrivate(Boolean setImpersonationContext) at System.Web.LegacyAspNetSynchronizationContext.CallCallbackPossiblyUnderLock(SendOrPostCallback callback, Object state) at System.Web.LegacyAspNetSynchronizationContext.CallCallback(SendOrPostCallback callback, Object state) at System.Threading.Tasks.AwaitTaskContinuation.RunCallback(ContextCallback callback, Object state, Task& currentTask) --- End of stack trace from previous location where exception was thrown --- at System.Threading.Tasks.AwaitTaskContinuation.b__1(Object s) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()

at System.Threading.ThreadPoolWorkQueue.Dispatch()

Also, this one isn't in the same time-frame, but does precede the exceptions so I thought I'd include it for completeness in case it's related somehow:

A process serving application pool 'preview.hotrid.es' suffered a fatal communication error with the Windows Process Activation Service. The process id was '2464'. The data field contains the error number.


Reply to this email directly or view it on GitHub: https://github.com/smsohan/MvcMailer/issues/93

JayVDZ commented 11 years ago

Version 4.5.

So a breaking change has been released by NuGet? That's not good practice. Thanks for the tip, I'll strip out the NuGet version and go manual.

Though the previous version didn't actually work asynchronously, it sent but it still blocked the request thread. With the new release it seemed to fix this and work asynchronously but has just stopped working. A shame.

WIll a new version that fixes this be forthcoming?

smsohan commented 11 years ago

With .Net 4.5, please use MvcMailer 4.5 — Sent from Mailbox for iPhone

On Tue, Apr 9, 2013 at 7:37 AM, Jay Van Der Zant notifications@github.com wrote:

Version 4.5.

So a breaking change has been released by NuGet? That's not good practice. Thanks for the tip, I'll strip out the NuGet version and go manual.

Reply to this email directly or view it on GitHub: https://github.com/smsohan/MvcMailer/issues/93#issuecomment-16112730

JayVDZ commented 11 years ago

I'm using MvcMailer 4.5 (latest via NuGet). I think this is a regular a bug of some kind as some views are working but others aren't, but those that aren't working were working and I've made no changes.

JayVDZ commented 11 years ago

It's definitely intermittent. Using the same code, sometimes it sends, sometimes it doesn't. I've checked the two variables being used in the view and they're fine. There's some kind of issue here.

RobertDM commented 11 years ago

I am using MvcMailer 4.5 with .NET 4.5 and experienced an intermittent null reference exception from using SendAsync as well. This is on a development machine using the ASP.NET development server. Sorry can't give you the call stack at the moment (it decided to start working), but it was deep in the threading code in MSCORLIB and there was only Microsoft stuff on the whole stack.

My attempted workaround is to just dispatch a synchronous Send() from a new thread instead of using the SendAsync function with its additional complexity, callbacks, etc. . ie:

Task.Factory.StartNew( () => { msg.Send(); });

smsohan commented 11 years ago

This is exactly what MvcMailer does. Please see the code.

Sent from my iPad

On 2013-07-12, at 8:23 AM, RobertDM notifications@github.com wrote:

I am using MvcMailer 4.5 with .NET 4.5 and experienced an intermittent null reference exception from using SendAsync as well. This is on a development machine using the ASP.NET development server. Sorry can't give you the call stack at the moment (it decided to start working), but it was deep in the threading code in MSCORLIB and there was only Microsoft stuff on the whole stack.

My attempted workaround is to just dispatch a synchronous Send() from a new thread instead of using the SendAsync function with its additional complexity, callbacks, etc. . ie:

Task.Factory.StartNew( () => { msg.Send(); });

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/93#issuecomment-20879398 .

RobertDM commented 11 years ago

Well the difference would be I'm just calling your synchronous Send(), not SendAsync().

When my code was getting that null reference exception, it was after the mail was actually sent (ie. it was successfully written to the specified pickup directory I'm testing with, prior to the exception). So if there is an issue, perhaps it is with the callback event handling in SendAsync. I noticed your event handler references an instance function of SmtpClientWrapper used in the MvcMailMessage.SendAsync function... it is possible in some circumstances that object no longer exists at the time SmtpClient.SendAsync calls it? I'm more familiar with C++ than C# so not sure about the nuances of object scope/garbage collection.

JayVDZ commented 11 years ago

Good idea. Thanks Rob. Will try running on another thread.

Jay On 12 Jul 2013 20:05, "RobertDM" notifications@github.com wrote:

Well the difference would be I'm just calling your synchronous Send(), not SendAsync().

When my code was getting that null reference exception, it was after the mail was actually sent (ie. it was successfully written to the specified pickup directory I'm testing with, prior to the exception). So if there is an issue, perhaps it is with the callback event handling in SendAsync. I noticed your event handler references an instance function of SmtpClientWrapper used in the MvcMailMessage.SendAsync function... it is possible in some circumstances that object no longer exists at the time SmtpClient.SendAsync calls it? I'm more familiar with C++ than C# so not sure about the nuances of object scope/garbage collection.

— Reply to this email directly or view it on GitHubhttps://github.com/smsohan/MvcMailer/issues/93#issuecomment-20893322 .

lruckman commented 11 years ago

Robert, many thanks this fixed the crash for me!

andrewflierman commented 11 years ago

Same problem here, MVC 4 and .NET 4.5. Robert's solution worked fine, see also this SO question: http://stackoverflow.com/questions/12201353/send-async-e-mails