Task continuation sometimes not scheduled. Threadsafe bug in PPL ? RRS feed

  • Question

  • Hi,

    Most of the time, I create task chains from MTA context and I get the expected behavior working well.
    But, in a specific case, I have to create a task chain from the STA (in my case, when the spend event is raise).

    The pseudocode :

    task<TRes> firstTask;

         IASyncOperation<TRes>^ asyncOp = ....;
         firstTask = task<TRes>(asyncOp);
         IASyncOperation<TRes>^ anotherAsyncOp = ....;
         firstTask = task<TRes>(anotherAsyncOp );

    tasl<TRes2> secondTask = firstTask.then(......);

    As said, when this runs on MTA, all works fine, but on STA, it appers that the secondTask (continuation of firstTask) is never executed nor scheduled. If I break the code and look at tasks variables, fristTask._M_TaskState value is "_Completed", while secondTask._M_TaskState value is "_Created" and secondTask._M_Sceduled is "not set".

    So the continuation has never been scheduled. Into the ppltask implementation It confirms that if the task is already completed, the continuation should be scheduled directly. If I run inside the .then(..) code step by step, this is actually what happens, the ascendent firstTask is seen completed and the secondTask is immediately scheduled....and finally the application works as expected. But without step by step, this is not the case !
    Could it be possible that a non threadsafe code could leads to this unexpected behavior (Maybe the completed state of the ascendent task could change while the then() method is creating the continuation task after it decided to not schedule it ?)

    Currently, to woek around the issue, I've encapsulated the whole task chain creation within another big task, so it runs on MTA, but I should probably not have to do this to have my code working. And If I'm not doing something forbidden there's probably a bug in task implementation to look for isn't it ?


    Wednesday, April 17, 2013 10:21 AM

All replies

  • Hello,

    Would you please provide us the whole codes to reproduce this issue,

    You can upload your project to SkyDrive

    It is not necessary that you send out the whole of your project. We just need a simplest sample to reproduce the problem. You can remove any confidential information or business details from it.

    If you don't want to upload the sample code, you can try to get the paid support from https://getsupport.microsoft.com/

    Best regards,

    Jesse Jiang
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, April 18, 2013 3:06 AM