# Issue with Persistent Context when using q-gate

**URL:** <https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886>\
**Category:** General\
**Created:** [10 September 2021 18:10 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886 "2021-09-10T18:10:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![pranavthakkar](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@pranavthakkar](https://discourse.nodered.org/u/pranavthakkar)\
**Post date:** [10 September 2021 18:10 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/1 "2021-09-10T18:10:07Z")

</div>

Hi @zenofmud. I'm little confused over cache. If I set it to false. wouldn't it should store my values instantaneously?

Case: 1  
but here is my error on this code. and it is not storing.

```auto
[{"id":"2ad176d6.2631aa","type":"function","z":"8872dfa.15cfd2","name":"","func":"let msgCount = context.get(\"msgCount\") || 0;//get remembered count\nmsgCount++; //increment count\n\ncontext.set('msgCount', msgCount);\n\nmsg.payload = msgCount;\nreturn msg;\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":460,"y":300,"wires":[["abccbeba8b1946e7","d7d6820b747c07e8"]]},{"id":"d7d6820b747c07e8","type":"q-gate","z":"8872dfa.15cfd2","name":"","controlTopic":"control","defaultState":"queueing","openCmd":"open","closeCmd":"close","toggleCmd":"toggle","queueCmd":"queue","defaultCmd":"default","triggerCmd":"trigger","flushCmd":"flush","resetCmd":"reset","peekCmd":"peek","dropCmd":"drop","statusCmd":"status","maxQueueLength":"","keepNewest":true,"qToggle":false,"persist":true,"storeName":"file","x":550,"y":460,"wires":[["73417c9d5d868f88"]]},{"id":"3a69d2dbe6a2c80a","type":"inject","z":"8872dfa.15cfd2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":260,"y":280,"wires":[["2ad176d6.2631aa"]]},{"id":"73417c9d5d868f88","type":"debug","z":"8872dfa.15cfd2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":790,"y":460,"wires":[]},{"id":"08eef9e2a6c18ea2","type":"inject","z":"8872dfa.15cfd2","name":"flush","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"control","payload":"flush","payloadType":"str","x":250,"y":460,"wires":[["d7d6820b747c07e8"]]},{"id":"abccbeba8b1946e7","type":"debug","z":"8872dfa.15cfd2","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":850,"y":300,"wires":[]}]

```

> Error: File Store cache disabled - only asynchronous access supported

and  
Case 2:  
If I set cache to default and flush interval to 5 or 10 seconds. It is not flushing my all queue values. Suppose I push values at 1 sec rate. the after 20 counts I stop nodered and restart it. but It is never gonna give me all 20 values.

in my setting.js

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

code is same above.

Is there any limitation on minimum time?

Using ubuntu on Virtualbox.

---

<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:** [10 September 2021 18:38 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/2 "2021-09-10T18:38:35Z")

</div>

> [@pranavthakkar](#):
>
> Error: File Store cache disabled - only asynchronous access supported

The error means what it says. If you disable the cache, the store can only be accessed in async mode as described here: [Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions#asynchronous-context-access)

You will have to change your code to match.

> [@pranavthakkar](#):
>
> I stop nodered and restart it. but It is never gonna give me all 20 values.

How are you stopping Node-RED? Is it a clean stop or are you abruptly killing the process or killing the VM? If it isn't a clean stop, then Node-RED won't be able to flush out anything still in the cache.

---

<div class="post-metadata">

**Author:** ![pranavthakkar](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@pranavthakkar](https://discourse.nodered.org/u/pranavthakkar)\
**Post date:** [10 September 2021 19:22 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/3 "2021-09-10T19:22:46Z")

</div>

Yes It is `node-red-stop`. and then `node-red-restart`.

---

<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:** [10 September 2021 20:47 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/4 "2021-09-10T20:47:03Z")

</div>

I am seeing the same issue with the test flow. However, it appears to be a problem only with the q-gate node as if I modify the function to use global context in the 'file' store for the message count, then that aspect works ok. It is only the q-gate that does not keep its queue.

I suggest that you raise an issue on the [node's github page](https://github.com/drmibell/node-red-contrib-queue-gate). Or perhaps @drmibell is listening.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [10 September 2021 21:06 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/5 "2021-09-10T21:06:38Z")

</div>

I'm listening, but I haven't been following. Will the issue be obvious if I run the test flow, or should I be asking for more description?

@pranavthakkar, what is in your settings.js in Case 1?

---

<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:** [11 September 2021 05:05 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/6 "2021-09-11T05:05:36Z")

</div>

> [@drmibell](#):
>
> Will the issue be obvious if I run the test flow

It was for me, yes. With the cache timeout set to 5 click the inject about 20 times in quick succession then immediately restart node red. On restart, the queued message count shown in the status drops from 20 back to zero or a small number. With the timeout set to 600 it works ok.

I haven't experimented enough to know whether the timing is critical but it seems to fail reliably.

**[Edit]** This is for case 2. With settings as posted in post #86

---

<div class="post-metadata">

**Author:** ![pranavthakkar](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@pranavthakkar](https://discourse.nodered.org/u/pranavthakkar)\
**Post date:** [11 September 2021 08:33 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/7 "2021-09-11T08:33:50Z")

</div>

Hi @drmibell . In my case: 1 I just provided `cache:false`  
in config of file.

In that It is never queuing the values.

---

<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:** [11 September 2021 10:48 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/8 "2021-09-11T10:48:40Z")

</div>

[NOTE] - I've move this to a seperate topic since it seems to be related to the q-gate node.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [12 September 2021 13:21 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/9 "2021-09-12T13:21:24Z")

</div>

Thanks, Paul. Is it worth fixing the typo in the title? I worry that it might break links elsewhere in the forum.

---

<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:** [12 September 2021 13:24 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/10 "2021-09-12T13:24:55Z")

</div>

[admin] spelling in title corrected

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [12 September 2021 13:38 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/11 "2021-09-12T13:38:15Z")

</div>

> [@pranavthakkar](#):
>
> It is never queuing the values.

As @knolleary says, for the moment the solution to Case 1 is "don't." Neither your function nor the `queue-gate` node is able to deal with asynchronous access (cache = false). The reason you cannot queue messages is that the error prevents the node from registering its listener for input events. Once this happens, it will not respond to any incoming messages, including control messages. The only fix I have found is to delete the node and replace it with a new instance.

---

<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:** [12 September 2021 14:01 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/12 "2021-09-12T14:01:38Z")

</div>

@drmibell are you able to replicate case 2 or do you want me to attempt to narrow it down if possible?

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [12 September 2021 14:16 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/13 "2021-09-12T14:16:02Z")

</div>

Please have a go at it and let me know what you find. I have not been able to reproduce Case 2 on my development machine (mac laptop), so I plan to switch to a RPi. I can think of at least a couple of ways that this could depend on the OS, so I'll keep trying.

---

<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:** [12 September 2021 15:14 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/14 "2021-09-12T15:14:17Z")

</div>

This may be a clue. I have a 5 second flushInterval. I have modified the function in the flow to save its msgCount in persistent node context also (and this works correctly). I have set the log level in node-red to Trace.

If I click the inject button a few times then in the trace log I see it flushing the function node context a few seconds later, but the q-gate node does not get flushed (apart from the fact that it sometimes gets flushed at the first interval after startup). However, if I click the Flush inject, which flushes the queue in the gate then I _do_ see the gate node context getting flushed, and looking at the file itself I can see it has correctly flushed it. So presumably that means there is a difference in the way the context is handled in the node for adding to the queue vs flushing it.

---

<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:** [12 September 2021 15:41 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/15 "2021-09-12T15:41:51Z")

</div>

Aha! It is more subtle than I thought. I think it is not directly related to the size of the flushInterval, but only accidentally.

I think if node-red is started and the queue is added to and then node-red is restarted before the first flushInterval has elapsed then it works correctly. However if node-red is started and then it is left till after the first interval has elapsed and _then_ the queue is added to then that is the situation where the queue is not flushed on restart (or at all I think). I am not absoulutely certain I have fully analysed it but it is something like that anyway, and I am out of time at the moment.

With a short flushInterval this is a much more likely occurrence which is why it is easy to reproduce with a 5 second interval.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [12 September 2021 15:43 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/16 "2021-09-12T15:43:34Z")

</div>

Thanks! I think you've solved it. The published version of the node is not saving the queue properly. Unfortunately, I have been working with a new branch that "accidentally" eliminates the bug as part of a major refactoring. It was foolish to assume that nothing had changed in that regard. As soon as I figure out how to use git properly to coordinate work on two branches, I'll publish a fix.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [12 September 2021 19:13 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/17 "2021-09-12T19:13:08Z")

</div>

I have just published `node-red-contrib-queue-gate` version 1.5.4, which should resolve the issue described in @pranavthakkar's Case 2 and the behavior found by @Colin . Thanks both for helping fix this bug.

I still have questions about Case 1. The node could catch the error caused by cache = false, but what should it do about it? Also, does that mean that contributed nodes should always use the asynchronous forms of `context.set` and `context.get`? Any thoughts @knolleary?

---

<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:** [12 September 2021 20:48 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/18 "2021-09-12T20:48:27Z")

</div>

Working now for me (Case 2).

---

<div class="post-metadata">

**Author:** ![pranavthakkar](https://avatars.discourse-cdn.com/v4/letter/p/e99b99/32.png) [@pranavthakkar](https://discourse.nodered.org/u/pranavthakkar)\
**Post date:** [15 September 2021 09:13 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/19 "2021-09-15T09:13:54Z")

</div>

Thanks Mike. It is working on Case 2.

---

<div class="post-metadata">

**Author:** ![drmibell](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/drmibell/32/8424_2.png) [@drmibell](https://discourse.nodered.org/u/drmibell)\
**Post date:** [29 September 2021 19:17 UTC](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886/20 "2021-09-29T19:17:59Z")

</div>

I have just published `node-red-contrib-queue-gate` version 1.5.5, which should resolve the issue in Case 1. Since the node has no way of knowing whether the context store selected by the user supports synchronous operations, I have used the form for asynchronous access everywhere. Please let me know if you run into any further issues. I would be very interested in knowing whether you get any changes or improvements by running with the cache disabled.

[Next page](https://discourse.nodered.org/t/issue-with-persistent-context-when-using-q-gate/50886.md?page=2)
