ExecutionEngineException when raising the PropertyChanged event

If you’ve ever seen an ExecutionEngineException in .NET, you know that it’s nasty. It even sounds intimidating. It’s the error that’s thrown by Environment.FailFast() — MSDN describes it as "the exception that is thrown when there is an internal error in the execution engine of the common language runtime." Before last night, the only time I’d ever seen it was when I was generating and running my own IL assembly code and I did something wrong like pop too many values off the stack or something.

However, last night I got it while raising a PropertyChanged event on my INotifyPropertyChanged object, immediately after using PropertyChangedEventManager to subscribe to it. I scratched my head…how the hell did I manage to cause an internal error in the CLR? I restarted Visual Studio, I rebooted my machine, I tried it on other machines to see if it was some sort of crazy corruption on my system, but it was completely repeatable.

Finally, in desperation, I set up .NET source stepping (something I’ve been meaning to do for a while anyway) and ran it again — this time, instead of showing up on the line where I raised the PropertyChanged event, the exception stopped on this code in WeakEventManager.cs:

            bool condition = listener.ReceiveWeakEvent(managerType, sender, args);
            if (!condition)
            {
                Invariant.Assert(condition, SR.Get("ListenerDidNotHandleEvent"), SR.Get("ListenerDidNotHandleEventDetail", new object[] { listener.GetType(), managerType }));
            }

That’s right…when ReceiveWeakEvent returns false (indicating that the listener did not handle the event being raised), WeakEventManager calls Environment.FailFast(). This is the software equivalent of people in horror movies choosing to shoot themselves in the face rather than turn into a zombie and possibly hurt their friends. It writes an event to the Event Viewer that says "Unrecoverable system error."

This may be the most ridiculously over-reactive error processing I’ve ever seen in my life, and for some reason, Google is completely unhelpful on this subject. Searching for "PropertyChangedEventManager ExecutionEngineException" turns up virtually nothing — the only result in English was a 404, and Google’s cached version didn’t have either keyword in it, for some reason. Hopefully this will save someone the intense frustration I experienced when I accidentally returned false from a random event handler somewhere, and was told that my execution engine had become unrecoverably corrupt and would be shut down for its own protection. If this post helps you, please tell me so in the comments so I’ll know my time wasn’t completely wasted.

About these ads
This entry was posted in Computers and Internet. Bookmark the permalink.

6 Responses to ExecutionEngineException when raising the PropertyChanged event

  1. Guy says:

    Dude!You sure saved my time. Lucky for me your post popped up in my second google attempt ;)enjoy–Guy

  2. Andrew says:

    Mate! Thanks! Definitely a lifesaver… Indeed, Google results didn’t play nice for me either until I finally found your post.

  3. emddudley says:

    I was really stuck until I found your blog post. I’ve put a question on StackOverflow to help give this issue higher visibility.

    http://stackoverflow.com/q/9727435/111327

  4. wonkim00 says:

    Found my way here via emddudley’s StackOverflow question. Good info!

  5. herm says:

    This one was really helpful.

  6. Gary Campbell says:

    Very nice job! Thank you

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s