14

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?

2
  • 1
    Did you try to compile it in Debug mode ?
    – Ofiris
    May 31, 2013 at 9:48
  • 2
    It is already, why do you ask that?
    – jaywayco
    May 31, 2013 at 9:49

2 Answers 2

27

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.

2
  • "Just one method that handles any reference type" - I thought that the JIT can instantiate multiple implementations of a generic-type over reference-types for the purposes of making non-virtual calls to instance members?
    – Dai
    Sep 2, 2021 at 2:22
  • 1
    @Dai de-virtualization you describe can occur only after inlining. And after inling there is no need to instantiate separate method body. Nov 20, 2022 at 2:53
18

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".

1
  • 8
    I am not persuaded by this comment at all. I think someone thought that __Canon is a great WTF'ish name and used it. If they really needed something short and descriptive in call stacks why they didn't use something like __T or __RefT?
    – csharpfolk
    Aug 4, 2018 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.