Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange System.__Canon exception

I have a windows service that uses a singleton class ThreadQueue<T>. When the service starts it makes a call to ThreadQueue<string>.Start() this class then accepts and queues tasks limiting the concurrency to a configurable number of threads.

ThreadQueue<string>.Start() is called once and only once when the service starts up.

On occasion, after a few hours of the service running i receive the following exception:

Application: myservice.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.NullReferenceException
Stack:
   at Apollo.Business.Framework.Threading.ThreadQueue.ThreadQueue`1[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].Start()
   at System.Threading.ThreadHelper.ThreadStart_Context(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.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ThreadHelper.ThreadStart()

What is System.__Canon and what is making this call passing in this as a type argument?

Can anyone shed any light?

like image 754
jaywayco Avatar asked May 31 '13 09:05

jaywayco


2 Answers

Now that the runtime and framework has been open sourced, it is much easier to answer this type of question. The definition of __Canon is available here. Quote:

// Internal methodtable used to instantiate the "canonical" methodtable for generic instantiations.
// The name "__Canon" will never been seen by users but it will appear a lot in debugger stack traces
// involving generics so it is kept deliberately short as to avoid being a nuisance.

[Serializable]
[ClassInterface(ClassInterfaceType.AutoDual)]
[System.Runtime.InteropServices.ComVisible(true)]
internal class __Canon
{
}

As the comment explains, it is an implementation detail for generics and is an abbreviation for "canonical".

like image 157
Dono Avatar answered Sep 22 '22 12:09

Dono


You should not read anything in the method argument type name. System.__Canon is an implementation detail related to the way generics are implemented in the CLR. I don't know the exact usage for it but strongly suspect it is used by Ngen.exe, the optimization tool in .NET that pre-jits assemblies. Generics are a problem in pre-jitting since the concrete type is instantiated at runtime. You'll get multiple copies of a method that takes an argument type of a type parameter. Just one method that handles any reference type, additional methods for each value type, if any. System.__Canon can be the substitute type that's the place holder for any reference type, allowing Ngen.exe to prejit that method even though it cannot guess what actual type will be used at runtime. Something like that.

The shoe fits, Apollo.Business.Framework.Threading.ThreadQueue sounds like the kind of class that's present in a framework style library that would be pre-jitted when it is installed since it was meant to be used by multiple programs.

So ignore the type name, focus on the actual exception. NullReferenceException is a very common exception of course. Nothing is otherwise visible from the stack trace that would give a hint what causes it. I'd guess at an initialization problem with that "Apollo" framework, some object that should have a value but is still null. Having a peek at the source code for the ThreadQueue constructor should give a hint. A call to the vendor for help if you don't have it. A bug in a 8 year old version of the jitter doesn't explain it well, those bugs have been fixed a long time ago.

like image 32
Hans Passant Avatar answered Sep 24 '22 12:09

Hans Passant