heal-research / HeuristicLab

HeuristicLab - An environment for heuristic and evolutionary optimization
https://dev.heuristiclab.com
GNU General Public License v3.0
39 stars 16 forks source link

Race condition in AsynchronousContentView leads to exception and HL crash #3069

Open HeuristicLab-Trac-Bot opened 4 years ago

HeuristicLab-Trac-Bot commented 4 years ago

Issue migrated from trac ticket # 3069

component: MainForm.WindowsForms | priority: medium

2020-05-15 18:00:19: @gkronber created the issue


There is a race condition in the AsynchronousContentView whereby in the following code 'result' is null, or already disposed within the WaitOrTimerCallback. This leads to a KeyNotFoundExceptionException in the callback and consequently a crash of HL. The error is hard to reproduce but usually occurs when algorithms are running and working with the Hive Job Manager (which often locks views and shows progress bars).

#!csharp
    /// Asynchronous call of GUI updating.
    /// </summary>
    /// <param name="method">The delegate to invoke.</param>
    /// <param name="args">The invoke arguments.</param>
    protected new void Invoke(Delegate method, params object[] args) {
      // prevents blocking of worker thread in Invoke, if the control is disposed
      IAsyncResult result = BeginInvoke(method, args);
      result.AsyncWaitHandle.WaitOne(1000, false);
      if (result.IsCompleted) try { EndInvoke(result); }
        catch (ObjectDisposedException) { } else {
        ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle,
          new WaitOrTimerCallback((x, b) => { try { EndInvoke(result); } catch (ObjectDisposedException) { } }),
          null, -1, true);
      }
    }
HeuristicLab-Trac-Bot commented 4 years ago

2020-05-15 18:00:57: @gkronber commented


I also observed this when importing CSV files into a regression problem.

The bug is difficult to reproduce but happens maybe once or twice a week for me.

HeuristicLab-Trac-Bot commented 4 years ago

2020-05-15 18:01:58: @gkronber edited the issue description