# Fixing race condition when channel items altered by later subworkflow are seen by earlier one

**URL:** <https://community.seqera.io/t/fixing-race-condition-when-channel-items-altered-by-later-subworkflow-are-seen-by-earlier-one/1004>\
**Category:** Ask for help\
**Created:** [July 24, 2024, 12:40am UTC](https://community.seqera.io/t/fixing-race-condition-when-channel-items-altered-by-later-subworkflow-are-seen-by-earlier-one/1004 "2024-07-24T00:40:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bskubi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/bskubi/32/545_2.png) [@bskubi](https://community.seqera.io/u/bskubi)\
**Post date:** [July 24, 2024, 12:40am UTC](https://community.seqera.io/t/fixing-race-condition-when-channel-items-altered-by-later-subworkflow-are-seen-by-earlier-one/1004/1 "2024-07-24T00:40:39Z")

</div>

Here is a toy example of a pipeline architecture with a somewhat counterintuitive behavior:

```auto
workflow A {
take:
ch

main:
ch | filter{it.containsKey("b")} | view

emit:
ch
}

workflow B {
take:
ch

main:
ch | map{it.b = true} | set{ch}
}

workflow {
ch = channel.fromList([[:], [b:false]])
ch | A | B
}

```

Output

```auto
[b:true]
[b:true]

```

In this example, an alteration to a channel item made in a subsequent subworkflow is seen by the previous subworkflow. This counterintuitive behavior makes some sense – B fires immediately because A doesn’t alter the channel items, and the changes it makes are seen by the filter and view operators in A due to the timing of when they fire.

The context in which this occurs in practice is where the same QC-launching subworkflow is called multiple times on the same channel. This channel contains hashmap channel items, where the hashmap may have several different keys associated with QCable stats files. Later subworkflows may add keys and a filename as the setup to calling a process that will produce the stats file itself. But the earlier QC subworkflow (analogous to A, above) can see that the key and filename was added by the later subworkflow (analogous to B), so it tries to pass that not-yet-created stats file to its QC process call, which creates an error.

I have workarounds, but it seems like there ought to be a more elegant solution. Using the toy example above, what would be the right way to achieve the intended behavior, where A should display `[b:false]` by filtering out the empty hashmap and calling view before B is run?

---

<div class="post-metadata">

**Author:** ![bentsherman](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/bentsherman/32/52_2.png) [@bentsherman](https://community.seqera.io/u/bentsherman)\
**Post date:** [July 24, 2024, 6:18pm UTC](https://community.seqera.io/t/fixing-race-condition-when-channel-items-altered-by-later-subworkflow-are-seen-by-earlier-one/1004/2 "2024-07-24T18:18:26Z")

</div>

You should never modify values in an operator closure like that. Instead, you can use the `.clone()` method or `+` operator to create a new map with modifications:

```groovy
['b': true] + ['b': false] == ['b': false]

```

I will probably add a compiler warning for this pattern in the future as it is a common pitfall for users.

---

<div class="post-metadata">

**Author:** ![robsyme](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/robsyme/32/342_2.png) [@robsyme](https://community.seqera.io/u/robsyme)\
**Post date:** [July 24, 2024, 9:09pm UTC](https://community.seqera.io/t/fixing-race-condition-when-channel-items-altered-by-later-subworkflow-are-seen-by-earlier-one/1004/3 "2024-07-24T21:09:46Z")

</div>

I gave a bytesize talk [here](https://www.youtube.com/watch?v=A357C-ux6Dw) outlined this problem and suggested mitigation strategies.

---

<div class="post-metadata">

**Author:** ![bskubi](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/bskubi/32/545_2.png) [@bskubi](https://community.seqera.io/u/bskubi)\
**Post date:** [July 26, 2024, 4:35pm UTC](https://community.seqera.io/t/fixing-race-condition-when-channel-items-altered-by-later-subworkflow-are-seen-by-earlier-one/1004/4 "2024-07-26T16:35:00Z")

</div>

Thanks to both of you, that makes sense.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/flex013/user_avatar/community.seqera.io/system/32/2402_2.png) [@system](https://community.seqera.io/u/system)\
**Post date:** [August 2, 2024, 4:35pm UTC](https://community.seqera.io/t/fixing-race-condition-when-channel-items-altered-by-later-subworkflow-are-seen-by-earlier-one/1004/5 "2024-08-02T16:35:03Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
