When triggers interrupt flows does chaos result?

Post your questions and help other users.

Moderator: Martin

Post Reply
Econdoc
Posts: 153
Joined: 28 May 2016 20:06

When triggers interrupt flows does chaos result?

Post by Econdoc » 04 Jun 2019 20:02

I am getting very confused about the sequence of events in several flows running at the same time. Here is my (simple?) example:

TriggerONE is initiated and its task is to turn on NFC. This NFC flow runs for a few milliseconds and is interrupted by TriggerTWO which is supposed to turn NFC off. From the logs, it appears that although the second NFC task runs to completion, the first NFC task ends after the second task has already finished.

I am sorry that this sounds so confusing, but it has me screaming in frustration.


Here is a second and related question that arose from trying to fix the first question. In both TriggerONE and TriggerTWO, I tried to use Execute Flows to run the two NFC flows. I checked the “wait for called flows to finish” box in each Execute Flows options. It did not seem to make any difference compared to just running the flow itself. If checking the box does not force the other flows to wait, what does it actually do? I do not see any difference between Execute Flows and just running the flow itself.

Here are my questions: Why does the first flow end after the second has ended? Why doesn’t “wait” work the way I expect it to? How can I fix this?

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: When triggers interrupt flows does chaos result?

Post by Desmanto » 05 Jun 2019 14:18

It is difficult to know the detail without looking at the flow. Are both triggers on the same flow or separate flow? What is the Flow Execution Policy used?
If trigger one has more elements to execute, and trigger two has less, it is possible trigger two flows/branch finish first event though it is triggered last.

For wait, same problem, difficult to understand the situation without example flow. Wait the executed flow, will make the caller flow stuck at there and not continuing to the next element, until the called flow is finished. If the called flow is very fast, then it seems there is not wait at all. You can try to add sleep 5 seconds at the end of the called flow, so you can see that 5 seconds is reflected to the parent flow when "wait" is checked.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Wibbly
Posts: 418
Joined: 17 Mar 2014 09:02

Re: When triggers interrupt flows does chaos result?

Post by Wibbly » 05 Jun 2019 15:55

So this is a race situation (between the flows), right? Without knowing what you are trying to do, it's best to change the approach/logic of your flows to avoid race situations (when the outcome depends on the time taken for one flow to complete compared to another flow).

Econdoc
Posts: 153
Joined: 28 May 2016 20:06

Re: When triggers interrupt flows does chaos result?

Post by Econdoc » 05 Jun 2019 18:28

I appreciate the advice. How do I slow down a flow? Sleep() does not appear to work! The flow that contains sleep() or a different flow that was triggered, just keeps on running. Sleep() seems to be largely ignored. I have tried Sleep(500); Sleep(1000); sleep(2000). This is much longer than the conflicting flows take to run to completion. I am at a loss.

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: When triggers interrupt flows does chaos result?

Post by Desmanto » 06 Jun 2019 05:31

How do you want to slow it down? I should be clear in my previous post. Try to use action sleep, not sleep() inside script. Action sleep is counted as separate element execution and will add toward AES. If both flow are exclusive and you need only one of them running at one time, you should combine both into single flow and separate the trigger using expression. Then at the flow execution policy, use SKIP. This will ensure only one of the branch will run at one time.

The other method if the trigger can't be combined into single flow, use condition Flow executing, choose the complement flow. This should be added to both flows. This way, when flow A triggered, it will check if flow B has been running. If yes, then do nothing (stop the flow right there). If no, then continue to the main branch.
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply