concurrency and race conditions

249 views
Skip to first unread message

iurly

unread,
Oct 31, 2017, 9:59:35 AM10/31/17
to Node-RED
Hi,

I was under the impression that Node.JS executes javascript within a single thread.
My understanding was therefore that it would be safe to write functions within function nodes which access global variables, with a guarantee that no two instances would be executing at the same time.
Is my understanding wrong?

Thank you!

Colin Law

unread,
Oct 31, 2017, 10:20:15 AM10/31/17
to node...@googlegroups.com
I believe that is correct. Assuming that by instances you mean nodes.

Obviously if you use javascript timers within a function node then
other nodes are free to run whilst the node is waiting for the timer,
so you cannot assume a flow or global var will not change during that
time.

Colin
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/92d58e02-9441-4f97-ae56-8a9962ae2126%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Nick O'Leary

unread,
Oct 31, 2017, 10:49:55 AM10/31/17
to Node-RED Mailing List
Obviously if you use javascript timers

add to that, any asynchronous function call with its own callback.

Nick

On 31 October 2017 at 14:19, Colin Law <cla...@gmail.com> wrote:
I believe that is correct. Assuming that by instances you mean nodes.

Obviously if you use javascript timers within a function node then
other nodes are free to run whilst the node is waiting for the timer,
so you cannot assume a flow or global var will not change during that
time.

Colin

On 31 October 2017 at 13:59, iurly <gerlando...@gmail.com> wrote:
> Hi,
>
> I was under the impression that Node.JS executes javascript within a single
> thread.
> My understanding was therefore that it would be safe to write functions
> within function nodes which access global variables, with a guarantee that
> no two instances would be executing at the same time.
> Is my understanding wrong?
>
> Thank you!
>
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an

> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/92d58e02-9441-4f97-ae56-8a9962ae2126%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
http://nodered.org

Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send an email to node...@googlegroups.com.

Gerlando Falauto

unread,
Oct 31, 2017, 1:20:57 PM10/31/17
to node...@googlegroups.com


Il 31 ott 2017 3:49 PM, "Nick O'Leary" <nick....@gmail.com> ha scritto:
Obviously if you use javascript timers

add to that, any asynchronous function call with its own callback.


You mean you have no control on when such callbacks are called, correct? Yet there will be only one of those running at any given time, right? 
I mean, the callback will only be called after the function (the one calling the asynchronous function) has terminated, right?

You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/SFx6N8faJr8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Julian Knight

unread,
Nov 2, 2017, 9:14:25 AM11/2/17
to Node-RED
I don't think it is quite that simple. Because Node.JS uses a single thread with a main loop, there might be any number of async functions "running" at any point in time. Only one is being actively processed at once but many could be part processed but be waiting for a promise or callback. Of course, you also have the call stack so parent functions are also active but not directly running.

It is possible to create pseudo-threads (there are a number of modules to do this) and will soon be possible to create webworker threads too (currently in beta in v8 I think). You can also use forks which create a new Node.JS environment (and hence a new thread) but that maintain links between the 2.

Threads are rarely needed in most JavaScript programming since most JS is about I/O rather than CPU bound processes. There are other tools better at CPU intensive processes though I expect JS will catch up there in time.

Nick


> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/92d58e02-9441-4f97-ae56-8a9962ae2126%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
http://nodered.org

Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to a topic in the Google Groups "Node-RED" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/node-red/SFx6N8faJr8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to node-red+u...@googlegroups.com.

Gerlando Falauto

unread,
Nov 2, 2017, 9:23:41 AM11/2/17
to node...@googlegroups.com
Hi Julian,
I'm not quite sure I'm following...
Put a different way, would it be safe to assume that a block of code (written anywhere in Node.JS) which sets/checks/increments a global variable will never be preempted and therefore will always run as expected?
Of course, whenever you call async functions you're explicitly giving up that assumption.

Thanks again!



To unsubscribe from this group and all its topics, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Julian Knight

unread,
Nov 2, 2017, 9:44:08 AM11/2/17
to Node-RED
If you had a relatively simple Node.JS app where you didn't use any modules, only your own code and known built-in modules. Yes. As long as you don't use any async or promise-based coding.

That is because your code is pretty linear and is executed using the hidden loop that Node uses.

However, once you start getting into more complex settings, this becomes harder to predict. Can you be sure that a dependent module is not using a promise, an async function or a timer?

In JS, you should expect things to happen asynchronously not necessarily linearly.

PS: Bear in mind that I am an IT person not a dedicated programmer so my terminology and knowledge may not be 100% on this. But this is my current understanding.

One last word though, this is all a very theoretical discussion. It isn't clear why you are concerned about this. More information might lead to alternative approaches.
To unsubscribe from this group and all its topics, send an email to node-red+u...@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
Reply all
Reply to author
Forward
0 new messages