<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/56307>56307</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Inconsitency in task_state after enabling tasking in the serial team
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          mikaoP
      </td>
    </tr>
</table>

<pre>
    ```cpp
#include <stdio.h>
#include <unistd.h>
#include <omp.h>

int main() {
    omp_event_handle_t evt;
    #pragma omp task detach(evt)
    {
        omp_fulfill_event(evt);
    }

    #pragma omp parallel num_threads(2)
    #pragma omp master
    {
        #pragma omp task
        printf("hello 1\n");
    }
    int a;
    #pragma omp parallel num_threads(3)
    #pragma omp master
    {
        #pragma omp task depend(out:a)
        printf("hello 2\n");
        #pragma omp task depend(in:a)
        printf("hello 3\n");
    }
}
```
The execution hangs after doing all printfs, and it only happens if the number of threads of the second parallel is greater that the first one.

This program (compiled as `clang -fopenmp t1.c`) enables tasking in the serial team because of creating a detached task. I've been debugging a bit and it seems that task_state stack is used. Without the detached task this data structure is not modified.

I not an expert of this part of the runtime, but I found some places that may be related with the problem: https://github.com/llvm/llvm-project/blob/main/openmp/runtime/src/kmp_runtime.cpp#L2142 and https://github.com/llvm/llvm-project/blob/main/openmp/runtime/src/kmp_runtime.cpp#L2620 **(1)**

The inconsistency I found is in https://github.com/llvm/llvm-project/blob/main/openmp/runtime/src/kmp_runtime.cpp#L5386
where the task_state of the new threads is modified. In a regular nested parallel region the value assigned to the new threads is the same as the value restored by the master thread. In this case this does not happen, since we're in the `level==0` and we assign the value of the master thread at this moment, but the restoring **(1)** happens after that.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy9VU2PozgQ_TXkYg0ipvN14NDTvZEi7WEPI-2xZaAAbxsb2SbZ_Pt9NiSdaDozu9JqIkQwro_nV6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