Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

IJobNativeMultiHashMapVisitKeyValue has race conditions when burst-compiled

Discussion in '2019.1 Beta' started by Abbrew, Jan 25, 2019.

  1. Abbrew

    Abbrew

    Joined:
    Jan 1, 2018
    Posts:
    417
    Or the output changes randomly between executions when a IJobNativeMultiHashMapVisitKeyValue job is burst compiled. Sometimes it gives the expected behavior, sometimes it produces incorrect results.
    Example:
    Code (CSharp):
    1. //[BurstCompile]
    2.     private struct CheckRequestCompletionJob : IJobNativeMultiHashMapVisitKeyValue<ResultOrder, Request>
    3.     {
    4.         [ReadOnly]
    5.         public ResultCache<Request,Result>.ReadOnly results;
    6.         [WriteOnly]
    7.         public NativeQueue<ResultOrder>.Concurrent fulfilledRequests;
    8.         [WriteOnly]
    9.         public NativeHashMap<ResultLine<Request, Result>, int>.Concurrent existingResults;
    10.  
    11.         public void ExecuteNext(ResultOrder order, Request request)
    12.         {
    13.             if(results.TryGetFirstValue(request,out var result,out var iterator))
    14.             {
    15.                 if (existingResults.TryAdd(new ResultLine<Request, Result>(
    16.                     request,
    17.                     result,
    18.                     order),
    19.                     0))
    20.                 {
    21.                     //Debug.Log("Adding " + request.ToString());
    22.                     fulfilledRequests.Enqueue(order);
    23.                 }
    24.                 while (results.TryGetNextValue(out result,ref iterator))
    25.                 {
    26.  
    27.                     if (existingResults.TryAdd(new ResultLine<Request, Result>(
    28.                     request,
    29.                     result,
    30.                     order),
    31.                     0))
    32.                     {
    33.                         //Debug.Log("Adding " + request.ToString());
    34.  
    35.                         fulfilledRequests.Enqueue(order);
    36.                     }
    37.                 }
    38.             }
    39.         }
    40.     }
    When burst compiled, this job, in a unit test where
    Code (CSharp):
    1. Debug.Log("Adding " + request.ToString());
    should be called 4 times, is calling it 6 times instead.
     
  2. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,132
    Could you please submit a bug report for this issue with a minimal reproduction project?