Bug in Ninject?

2019-07-31 14:56发布

Looking for advice on whether I have a real bug or not and if it is, how to fix it best:

I have a highly multi-threaded process that runs 24/7. There are a few objects that are injected with bindings provided in ThreadScope by Ninject.

As the load on the process has been ever increasing, the process started crashing more and more often. Error message in the event log was this:

> Framework Version: v4.0.30319 Description: The process was terminated
> due to an unhandled exception. Exception Info:
> System.NullReferenceException Stack:    at
> **Ninject.Activation.Caching.GarbageCollectionCachePruner.PruneCacheIfGarbageCollectorHasRun(System.Object)**
> at System.Threading.ExecutionContext.runTryCode(System.Object)    at
> System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode,
> CleanupCode, System.Object)    at
> System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext,
> System.Threading.ContextCallback, System.Object, Boolean)    at
> System.Threading._TimerCallback.PerformTimerCallback(System.Object)

After downloading the source for Ninject and googling, it appears to be a somewhat of an issue either with Ninject or .NET framework (depending on whoever you listen).

Looks like Pruning of binding cache is attempting to restart a disposed timer object in the finally clause. There was an attempt to fix an issue with that logic a year ago, but it does not appear to have gone far enough: http://groups.google.com/group/ninject/browse_frm/thread/cedf5d129120ee18/27119d7d3761eedd?tvc=1#27119d7d3761eedd

Microsoft explains how the System Timer works in this MSDN article: http://msdn.microsoft.com/en-us/library/system.threading.timer.aspx

According to MSDN article, it is possible that GC has cleaned up the timer object BEFORE the TimerCallback is called. Thus, restarting the timer in the "finally" clause causes a crash.

Question: how do I fix this in the Ninject code? Should I simply ignore the error in such a case? Should I create a new timer? Should I do something else? I've currently modified the code to ignore the error so that I can simply stop my process from crashing... but fear it is not a great solution, any advice is appreciated.

Related articles: http://groups.google.com/group/ninject/browse_thread/thread/8cdf8362a41153c7

.NET 3.5 C# Bug with System.Timer System.ObjectDisposedException: Cannot access a disposed object

Ninject Garbage Collection

1条回答
The star\"
2楼-- · 2019-07-31 15:40

There is a bug in 2.2 that causes a race condition when the cache (Ninject) is disposed. This is fixed in 3.0

See https://github.com/ninject/ninject/blob/fa46b56b683d5ddf570d00c1bd057ecfa0b3b487/src/Ninject/Activation/Caching/GarbageCollectionCachePruner.cs

Normally, you shouldn't see any problems unless you are creating and disposing kernels during runtime- This isn't a good way to use Ninject anyway and should be avoided whenever possible. That way the only situation where this problem occurs should be on shutdown.

查看更多
登录 后发表回答