title: How to detect if code is being run in a "branched" async context?
body: <p>First of all I want to apologize for the potential invalid terminology used in the title. I'm not really sure what to call it or if there even is a right word for it. But stick with me and I'll try to explain.</p>
  <p>So I've been fiddling around with <code>AsyncLocal&lt;T&gt;</code> for a while and it works well. It's really great that I am able to await stuff and still access my ambient values using it. However, as it occurs, I am also able to access the same ambient value when I at some point do a <code>Task.Run(() =&gt; ...)</code>. While I realize this is likely to actually be what is usually desired. In my particular case it is not. I would like code that is explicitly being run in parallel to the original sequence of awaited statements to start on a clean slate from <code>AsyncLocal&lt;T&gt;</code>perspective.</p>
  <p>From what I have been finding out this seems to be possible by explicitly fiddling around with the <code>ExecutionContext</code> but since the use of the <code>AsyncLocal&lt;T&gt;</code> is hidden inside a framework I don't want users to have to explicitly do stuff like this every time they do a <code>Task.Run()</code> in order to get the expected behavior.</p>
  <p>One solution that I come to think of is if there is any way to detect that the code currently being run has been &quot;branched&quot; off of the previous sequence of (a)sync calls.</p>
  <p>i.e.</p>
  <pre><code>
  // In primary flow...
  
  await Foo();
  
  // Still here...
  
  Task.Run(() =&gt; 
  {
     // No longer in the primary flow. But any AsyncLocal&lt;T&gt; assigned outside still has the same value. 
  
     // Can I detect that this is not the same flow as outside Task.Run(...)?
  });
  
  // Still in primary...
  
  await Foo().ConfigureAwait(false);
  
  // Yep, still here...
  
  </code></pre>
  <p>Edit:</p>
  <p>Upon request from Stephen Cleary I'll elaborate a little on why I am currently in this rabbit hole.</p>
  <p>We have a system in which we have a tree of operations that is being executed. Each node is implemented as a class and may of course execute an arbitrary number of child operations. As the operations are being executed they build a parallel tree of OperationTrace nodes. Which is basically a serializable representation of the input/output, timing, validation, logging etc from each exection. Quite handy for debugging  purposes, statistics etc.</p>
  <p>The execution of the operations are of course async, and multiple trees/branches can be executed in parallel.</p>
  <p>We store the currently executed node in each &quot;execution path&quot; as OperationContext.Current, impl. by <code>AsyncLocal&lt;T&gt;</code>, and whenever a child operation is completed it pushes its trace into it's parents list of traces for the child operations.</p>
  <p>This works really well, except when someone decides to parallelize the the execution of the child operations. In fact it works rather well here too but the debugging experience is somewhat broken since the order of the child operation executions are no longer predictable.</p>
  <p>Typically we want the experience to be like this:</p>
  <pre><code>await childOperationX.ExecuteAsync(...);
  
  // Break here and watch 'OperationTrace.Last' to view the trace of X.
  </code></pre>
  <p>Now if the code is something like this:</p>
  <pre><code>var t0 = Task.Run(async () =&gt; 
  {
       await childOperationX.ExecuteAsync(...);
       await childOperationY.ExecuteAsync(...);
  
  });
  
  var t1 = Task.Run(async () =&gt; 
  {
       await childOperationP.ExecuteAsync(...);
       await childOperationQ.ExecuteAsync(...);
  
  
      // Break here and OperationTrace.Last may be any of [Q, X, Y]
  });
  
  await Task.WhenAll(t0, t1);
  
  </code></pre>
  <p>So what we did to solve this was to add:</p>
  <pre><code>OperationContext.BranchAsync(async () =&gt; ...);
  </code></pre>
  <p>which can be used instead of <code>Task.Run()</code> and that creates an intermediary trace node under which the traces for each child operation is put.</p>
  <p>While this works well, it would be really nice to not have to use that, and simply detect in each operation that the &quot;async context&quot; in which this operation is executed is not the same as the one the parent was executed and therefore there needs to be an extra trace node which represents the branch of parallelization.</p>