Bug Fixes: race conditions where the System.Threading.Time callbacks can be called before constructor returns. #1439
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Bug: Instances of System.Threading.Timer can start a callback before the constructor of the Timer has returned. Particularly when the dueTime parameter is set to 0. In certain cases the timer attempts to dispose itself via its callback. This can cause exceptions that will crash the running app.
This was witnessed in the RtpIceChannel.CheckIceServers
Application: App.exe CoreCLR Version: 8.0.1124.51707 .NET Version: 8.0.11 Description: The process was terminated due to an unhandled exception. Exception Info: System.NullReferenceException: Object reference not set to an instance of an object. at SIPSorcery.Net.RtpIceChannel.CheckIceServers(Object state) in C:\Git\App\Common\sipsorcery\src\net\ICE\RtpIceChannel.cs:line 1184 at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state) --- End of stack trace from previous location --- at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.TimerQueueTimer.Fire(Boolean isThreadPool) at System.Threading.ThreadPoolWorkQueue.Dispatch() at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()I created a simple app to illustrate the issue:

The instances of the System.Threading.Timer have been changed to use the pattern of starting the timer after the contructor has returned. This ensures that the race is avoided.