# Avoiding race conditions on flow start

**URL:** https://discourse.nodered.org/t/avoiding-race-conditions-on-flow-start/22421
**Category:** General
**Created:** [29 February 2020 19:07 UTC](https://discourse.nodered.org/t/avoiding-race-conditions-on-flow-start/22421 "2020-02-29T19:07:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![techraf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/techraf/32/17389_2.png) [@techraf](https://discourse.nodered.org/u/techraf)
#### Post date: [29 February 2020 19:07 UTC](https://discourse.nodered.org/t/avoiding-race-conditions-on-flow-start/22421/1 "2020-02-29T19:07:10Z")

</div>

Let's consider I have two generic processes being triggered on flow start and I want to withhold message(s) from one of them (`Process 2`) until some condition calculated by the other (`Process 1`) is met, and then pass all messages intact and with no delay.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/0/40bfa7f8627eb6eaa92461f754b693b44e599fe8.png)

I could withhold a message with either `delay`, `trigger`, or `join` node, but then I would need to do it for every message, not only the initial one.

With `delay` I thought of modifying the delay through `msg.delay` but I would need to add it to all messages except for the first one, and I end up with a requirement to move the logic ahead.

With `trigger` and `join` I could merge two messages (either with `msg.reset` or `msg.complete`), but then it would be required for each and every message.

* * *

Another idea: use context to store a Boolean value to distinguish between the first and subsequent messages - but then on flow restart, I would need to manually clear the context and thus have a third contender in the initial race.

I could add another Boolean to mark "context cleared", but this gets ugly.

Similarly, I tried adding a `trust_level` to `Process 1` and `Process 2` messages and use a simple condition in `function`, but then I need to use context and have the same problem with clearing.

* * *

Whatever I think of, gets patchy and ugly, and I am stuck in some dead-end street with ideas.

Isn't there a clean way to achieve the above?

---

<div class="post-metadata">

### Author: ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)
#### Post date: [29 February 2020 20:08 UTC](https://discourse.nodered.org/t/avoiding-race-conditions-on-flow-start/22421/2 "2020-02-29T20:08:44Z")

</div>

Could you make use of `node-red-contrib-queue-gate` in the 'Process 1' flow.  
Set the 'default state' as closed, then open the gate via the condition from 'Process 2'?  
'Process 1's messages would be queued, and then released.

---

<div class="post-metadata">

### Author: ![techraf](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/techraf/32/17389_2.png) [@techraf](https://discourse.nodered.org/u/techraf)
#### Post date: [29 February 2020 21:16 UTC](https://discourse.nodered.org/t/avoiding-race-conditions-on-flow-start/22421/3 "2020-02-29T21:16:33Z")

</div>

A real gem. 🥳 Exactly what I was looking for!

Thank you

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [14 March 2020 21:16 UTC](https://discourse.nodered.org/t/avoiding-race-conditions-on-flow-start/22421/4 "2020-03-14T21:16:34Z")

</div>

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