Community discussions

MikroTik App
just joined
Topic Author
Posts: 1
Joined: Fri Sep 25, 2020 3:30 pm

Function call error in ROS

Thu Nov 05, 2020 1:41 pm

I came across a strange bug in the scripts and Scheduler Mikrotik.
It consists in the fact that if a script or task in the Scheduler calls any function more than once - for example, twice, then in fact it is called a specified number of times +1 (that is, in our case, three times).
You can check this as follows:

1.Create a test function FuncTest that prints to the log how many times it has worked, also storing the number of its calls in the global variable FuncTestCount

: global FuncTest do = {
: global FuncTestCount
: set FuncTestCount ($ FuncTestCount + 1)
: log warning ("proverka". "$ FuncTestCount")

Now let's create a task in the Scheduler (or a second script) with a one-time execution and with the following content:

: global FuncTest
[$ FuncTest]
[$ FuncTest]

As you can see, the test function is called twice.
But, look in the log! You see that she worked three times!

The most interesting thing is that the last wrong time, someone calls the function. Also, the function does not receive its name in the known parameter $ 0 (it is empty).
Thus, thank God! you can bypass this Ros jamb as follows - add to the function itself Check for the presence of the $ 0 parameter. If it is not there - this is an erroneous call - we just do nothing.
Let's rewrite our test function like this:

: global FuncTest do = {
: if ([: len $ 0] =! 0) do = {
: global FuncTestCount
: set FuncTestCount ($ FuncTestCount + 1)
: log warning ("check". "$ FuncTestCount")
Then it will be called as many times as specified. It turns out that such a check needs to be inserted into all functions. Don't know if it depends on whether any commands were executed between calls. Tried inserting some commands and a pause like: delay 5s - without checking for an empty name, the wrong number of times is performed. Seems like a real developer mistake.

It is noticed that it is the second call of the function that is erroneous, while the subsequent ones work out correctly.

Tested on latest ROS versions since at least 6.46.3

Who is online

Users browsing this forum: No registered users and 18 guests