# Error-Circular / Wrong usage of persistent global variables?

**URL:** <https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815>\
**Category:** General\
**Created:** [2 January 2020 11:48 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815 "2020-01-02T11:48:32Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![NrSteffen](https://avatars.discourse-cdn.com/v4/letter/n/e9c0ed/32.png) [@NrSteffen](https://discourse.nodered.org/u/NrSteffen)\
**Post date:** [2 January 2020 11:48 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/1 "2020-01-02T11:48:32Z")

</div>

First of all a happy new year to all of you!

I'm seeing a lot of error-circular messages and I'm wondering if this is caused by "my way" of handling persistent global variables (I'm storing some historic values, e.g. the highest/lowest temperature seen):

1. I enabled the Local Filesystem Context Storage

2. At startup I do the following for every global variable once:  
var globVar = global.get('globVar');  
if(globVar ===undefined)  
{  
globVar = false; // Example for a boolean value  
global.set('globVar ', globVar);  
}

3. If I want to access the variable I do the following:  
var globVar = global.get('globVar'); //  
... calculate something here...  
global.set('globVar', globVar);

Could this cause the error-circular warnings? What is the right way of handling global variables (I know, best way is to don't use them at all)?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [2 January 2020 11:51 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/2 "2020-01-02T11:51:12Z")

</div>

There's nothing inherently wrong with what you've shared.

Where/when do you see the error? Can you share the actual full output of the error?

What sorts of values are you storing in context? Simple values or more complex objects? If they are objects, how are they generated and do they contain circular references in them?

---

<div class="post-metadata">

**Author:** ![NrSteffen](https://avatars.discourse-cdn.com/v4/letter/n/e9c0ed/32.png) [@NrSteffen](https://discourse.nodered.org/u/NrSteffen)\
**Post date:** [2 January 2020 13:13 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/3 "2020-01-02T13:13:17Z")

</div>

Hi Nick,

thanks a lot for your help. Unfortunately I'm just beginning with Node Red and I'm struggling to provide the full output of the error - Where can I find this information?

Node-Red-Log just provides the warning itself without any additional information, e.g. "2 Jan 13:28:38 - [warn] error-circular". Same for my debug nodes, e.g. 2.1.2020, 13:57:09 msg : string[14] "error-circular".

So far I struggled to identify the affected node(s), but while writing this message I noticed that I can try to identify the affected nodes by setting one inject to 10s and the rest to 1 hour.

Q: What sorts of values are you storing in context? Simple values or more complex objects?  
A: Just simple values like int, float or boolean

Q: If they are objects, how are they generated and do they contain circular references in them?  
A: To be honest: I'm not sure, but as I'm just storing simple values I think the answer is "no circular references"

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 January 2020 15:19 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/4 "2020-01-02T15:19:28Z")

</div>

If you name your nodes then it should show the node name in the debug window above the message. It does show an id anyway, but it is easier to track them down if you use names. The Search feature will let you search the flow for a name or an id, or anything else for that matter.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [2 January 2020 18:17 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/5 "2020-01-02T18:17:42Z")

</div>

Toss a `catch` node connected to a `debug` node (set to display the complete msg object) and see if that helps locate the error,

---

<div class="post-metadata">

**Author:** ![NrSteffen](https://avatars.discourse-cdn.com/v4/letter/n/e9c0ed/32.png) [@NrSteffen](https://discourse.nodered.org/u/NrSteffen)\
**Post date:** [2 January 2020 19:24 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/6 "2020-01-02T19:24:52Z")

</div>

> [@Colin](#):
>
> If you name your nodes then it should show the node name in the debug window above the message. It does show an id anyway, but it is easier to track them down if you use names. The Search feature will let you search the flow for a name or an id, or anything else for that matter.

I dont think naming my nodes will help. Unfortunately the error message doesn't even show the usual node ID (see above). And \>95% of my nodes are named anyhow.

> [@zenofmud](#):
>
> Toss a `catch` node connected to a `debug` node (set to display the complete msg object) and see if that helps locate the error,

I added two nodes (a Catch node connected to a debug node) to see if I get more information. I tried that on every tab but it seems this kind of error is not caught by a catch node. At least I did not get any further information. Just the error message posted above without a node ID.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [2 January 2020 20:09 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/7 "2020-01-02T20:09:45Z")

</div>

At this point I would make a backup of your flow and then start tearing it apart till you find the node causing it.

And/or providing your flow so someone else can look at it.

---

<div class="post-metadata">

**Author:** ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)\
**Post date:** [2 January 2020 20:20 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/8 "2020-01-02T20:20:52Z")

</div>

You wrote “local file system” but you don’t defined it in your global.get / global.set calls.  
I.e `global.set(“globVar”,globVar,”file”)`

And to have a safe state it is most of the times a good idea to provide a default (not in point 2 but perhaps point 3)

`var globVar=global.get(“globVar”,”file”) || false;`

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [2 January 2020 20:22 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/9 "2020-01-02T20:22:04Z")

</div>

> [@Christian-Me](#):
>
> You wrote “local file system” but you don’t defined it in your global.get / global.set calls.  
> I.e `global.set(“globVar”,globVar,”file”)`

Depending on how context storage has been configured, this may not be needed. If there is only one store, it is the default one used if no store is identified in the api calls.

---

<div class="post-metadata">

**Author:** ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)\
**Post date:** [2 January 2020 20:29 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/10 "2020-01-02T20:29:44Z")

</div>

You are right. I assumed @NrSteffen took the example out of the docs were default is memory.

Another hint could be to take a look on the context tab. There you can inspect the state of global variables and where there stored.

I was typing on my phone having around -5 degrees centigrade... otherwise I wouldn’t pressed send so fast - to get my fingers back into my pocket. Happy New Year!

---

<div class="post-metadata">

**Author:** ![NrSteffen](https://avatars.discourse-cdn.com/v4/letter/n/e9c0ed/32.png) [@NrSteffen](https://discourse.nodered.org/u/NrSteffen)\
**Post date:** [2 January 2020 20:39 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/11 "2020-01-02T20:39:17Z")

</div>

> [@zenofmud](#):
>
> At this point I would make a backup of your flow and then start tearing it apart till you find the node causing it.

Agree. I will let you guys know if I find something. Thanks for your support.

---

<div class="post-metadata">

**Author:** ![NrSteffen](https://avatars.discourse-cdn.com/v4/letter/n/e9c0ed/32.png) [@NrSteffen](https://discourse.nodered.org/u/NrSteffen)\
**Post date:** [27 February 2020 14:37 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/12 "2020-02-27T14:37:08Z")

</div>

Ok, I still was not able to find the root cause of my problem, however I found out that it's related to the "Local Filesystem Context Store". I uninstalled NR completely and after a re-installation everything was fine. But once I activated the local file storage the error-circulars started again.

As a workaround I increased the flushInterval to 10 minutes and now the amount of errors has decreased significantly, but they are still there.

settings.js:

```
contextStorage: {
    default: {
    	module:"localfilesystem",
	config:{flushInterval:600}
    },
},

```

Doesn't this point towards the way I handle my global variables (see my first question in this thread)? Any ideas?

Thanks!

Btw. tearing my flows apart did not help at all. I increased all inject node periods to \>1h and still saw errors. Strange.

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 February 2020 14:46 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/13 "2020-02-27T14:46:59Z")

</div>

The error comes whenever the File Store has to encode your context data to JSON so it can be written to the file. This is why changing the flush interval to 10 minutes will mean you hit the error 10 minutes after updating the context rather than the default 30 seconds.

> [@NrSteffen](#):
>
> Doesn't this point towards the way I handle my global variables (see my first question in this thread)? Any ideas?

Yes. Somehow you are putting something into context that includes a circular reference.

Changing the rate of your inject nodes won't change the logic of your flows or the nature of the objects you are trying to store.

So the question again comes back to what sort of data are you trying to store in context.

A good example that came up today was a user trying to store the value returned by `setInterval()` into context. This value is a complex object with circular references and cannot be converted to JSON.

---

<div class="post-metadata">

**Author:** ![NrSteffen](https://avatars.discourse-cdn.com/v4/letter/n/e9c0ed/32.png) [@NrSteffen](https://discourse.nodered.org/u/NrSteffen)\
**Post date:** [27 February 2020 15:03 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/14 "2020-02-27T15:03:09Z")

</div>

Thanks, Nick!

I indeed didn't understand that the storing process could just be the symptom, not the issue. So thanks for pointing this out.

Does it make sense to compare the File Store (/home/pi/.node-red/context/global/global.json I guess?) with my context data shown in NR? My understanding is that a missing global in the file would indicate the problem, correct?

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [27 February 2020 15:05 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/15 "2020-02-27T15:05:23Z")

</div>

Yes, that would be one way to do it - look to see what _isn't_ being persisted as you'd expect.

---

<div class="post-metadata">

**Author:** ![NrSteffen](https://avatars.discourse-cdn.com/v4/letter/n/e9c0ed/32.png) [@NrSteffen](https://discourse.nodered.org/u/NrSteffen)\
**Post date:** [27 February 2020 16:05 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/16 "2020-02-27T16:05:30Z")

</div>

Everything seems to be ok. Every Global shown in NR is stored in /home/pi/.node-red/context/global/global.json with the value I would expect to see.

Out of 15 Globals 14 are simple types (String, Boolean, Int, Float) and one is automatically generated by a node I installed ([https://flows.nodered.org/node/node-red-contrib-homee](https://flows.nodered.org/node/node-red-contrib-homee)). The last one is huge, so I'm struggling to compare all values. But as it is stored I guess it should be ok.

Well, to be honest I'm running out of ideas... 😄

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [27 February 2020 17:48 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/17 "2020-02-27T17:48:19Z")

</div>

The last node is the reason. Looking at the underlying code of the `homee-api` library it shows that the object that is populated appears to contain circular references. They're passed by reference, and point to each other. [https://github.com/stfnhmplr/homee-api/blob/30362ce63f70a7e87cabe943cad5eeeb0892497e/homee.js](https://github.com/stfnhmplr/homee-api/blob/30362ce63f70a7e87cabe943cad5eeeb0892497e/homee.js) this file has that _magic_.  
Those objects are then put in the global context by the node.

---

<div class="post-metadata">

**Author:** ![NrSteffen](https://avatars.discourse-cdn.com/v4/letter/n/e9c0ed/32.png) [@NrSteffen](https://discourse.nodered.org/u/NrSteffen)\
**Post date:** [27 February 2020 17:56 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/18 "2020-02-27T17:56:42Z")

</div>

@afelix,

thanks a lot for your support - that's much more than I have expected. 👍

Is there a way for me to work around this issue or is this something I have to live with if I keep using both the homee node and Local File Store?

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [27 February 2020 18:39 UTC](https://discourse.nodered.org/t/error-circular-wrong-usage-of-persistent-global-variables/19815/19 "2020-02-27T18:39:37Z")

</div>

File an issue on the node’s repository, both the node and the underlying library are made by the same person.
