Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Task.Run hang in unity relative with il2cpp

Discussion in 'Scripting' started by astrokoo3, Dec 14, 2020.

  1. astrokoo3

    astrokoo3

    Joined:
    Jan 31, 2020
    Posts:
    22
    hello.

    my project use heavy network using ssl stream in unity

    and some random case task.run get hang in MobileAuthenticatedStream.cs below line

    Code (CSharp):
    1. int num = await (!sync ? authenticatedStream.InnerStream.ReadAsync(authenticatedStream.readBuffer.Buffer, authenticatedStream.readBuffer.EndOffset, len, cancellationToken) : (Task<int>) Task.Run<int>((Func<M0>) (() => this.InnerStream.Read(this.readBuffer.Buffer, this.readBuffer.EndOffset, len)))).ConfigureAwait(false);
    im digging to find what the problem is

    and it's il2cpp related problem

    the task not dispatch in threadPoolWorkerQueue.cs

    cause memory barrier in ios

    i change the il2cpp code in xcode for LocalPop and TryStreal like below

    LocalPop function
    Code (CSharp):
    1.  
    2. Interlocked_Exchange_mD5CC61AF0F002355912FAAF84F26BE93639B5FD5((int32_t*)L_7,
    3.           ((int32_t)il2cpp_codegen_add((int32_t)L_8, (int32_t)1)), /*hidden argument*/NULL);
    4. il2cpp_codegen_memory_barrier();
    5. int32_t L_9 = V_1;
    6. int32_t L_10 = __this->get_m_tailIndex_3();
    7. il2cpp_codegen_memory_barrier();
    8. if ((((int32_t)L_9) >= ((int32_t)L_10)))
    9.  

    TrySteal function
    Code (CSharp):
    1.  
    2.  
    3. Interlocked_Exchange_mD5CC61AF0F002355912FAAF84F26BE93639B5FD5((int32_t*)L_5, L_6, /*hidden argument*/NULL);
    4. il2cpp_codegen_memory_barrier();
    5. int32_t L_7 = __this->get_m_headIndex_2();
    6. il2cpp_codegen_memory_barrier();
    7. int32_t L_8 = V_0;
    8. if ((((int32_t)L_7) > ((int32_t)L_8)))
    9.  
    i insert the barrier right after the Interlocked_Exchange

    then problem disappear

    so ios build is ok

    but android can't modify the cpp file


    How could I solve this problem?

    please help me out.

    thanks a lot
     
  2. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,773
    Can you submit a bug report for this issue? We can investigate it more closely then.
     
  3. astrokoo3

    astrokoo3

    Joined:
    Jan 31, 2020
    Posts:
    22
    is it possible to report about ios device?

    i just print out the head, and tail index for this https://referencesource.microsoft.com/#mscorlib/system/threading/threadpool.cs,149
    when it hang on

    i think head index shouldn't be get larger than tail index so localPop and TryStreal cannot get the same task

    but it success when localPop and TryStreal go throught at same time so twice pop the task and header index get bigger than tail index

    below is footprint of localPush, localPop, TryStreal

    first one is thread_id, next is task address, then head and tail index

    Code (CSharp):
    1.  
    2. localPush            1741748, 0x13f1a7f60,    563,564
    3. localPop              1741748, 0x13f1a7f60,    564,563,563,563
    4. TrySteal               1741747, 0x13f1a7f60,    564,563,563,564
    5.  
    number 564,563,563,563 order by

    __this.get_m_headIndex_2(), __this.get_m_tailIndex_2(), L_9(head), L_10(tail)

    you can see the L_9, L_10 is true on if statement https://referencesource.microsoft.com/#mscorlib/system/threading/threadpool.cs,312
    to get task and right after print it out from this pointer

    the head index is bigger then tail index but head index shouldn't be change.

    so i think the Interlocked.Exchange on ios not guarantee the memory barrier




    it has same problem on android but window didn't

    thanks a lot
     
    Last edited: Dec 14, 2020
  4. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,773
    It is possible to submit a bug to Unity for and iOS player build. You can find details about how to submit a bug report here: https://unity3d.com/unity/qa/bug-reporting

    This looks like a pretty complex situation, so I can't comment on it without investigating, unfortunately.
     
    Joe-Censored likes this.
  5. astrokoo3

    astrokoo3

    Joined:
    Jan 31, 2020
    Posts:
    22
  6. astrokoo3

    astrokoo3

    Joined:
    Jan 31, 2020
    Posts:
    22
  7. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,773
    It looks like our QA team has not processed it yet. I'll see if they can have a look soon.
     
    astrokoo3 likes this.