1
Vote

Race condition with Task.ContinueWith

description

There is a race condition in this libraries implementation of ContinueWith. The implementation assumes the action given to ContinueWith will only be run at the completion of the antecedent task. If the antecedent task completes before the call to ContinueWait the action/func passed to ContinueWith will never be executed. For example, the following code executes without assertion using .NET 4.0 TPL but asserts with PortableTPL:
        Task task = new Task(() => { });
        task.Start();
        task.Wait();
        Debug.WriteLine(task.IsCompleted);
        var waitHandle = new ManualResetEvent(false);
        Task task2 = task.ContinueWith((a) => { waitHandle.Set(); });
        Debug.Assert(waitHandle.WaitOne(1000));

comments