[BUG in script engine??] Script sometimes breaks ...

Hello everybody,
i’m facing a strange issue with a mikrotik script that i have scheduled on startup.

The goal of the script is to retrieve a file that is loaded via TFTP from a client.
this file contains the IP address that the router have to set on its wan port (ETH1)
I declare some variables on the beginning and set them to zero.
If the script works correctly, it goes on a loop where it waits for the file, beeping every second.
Sometimes i find that the script does not reach the loop, and it exits from execution.
The script/job menu is empty and the environment list shows that variables are declared and initialized with zeros.
It means that the script is started but it stopped at some point.
I used a dummy var, $step, to check where the script breaks, then i noticed that when it breaks the “step” var is set to 0.
I removed the line “/interface ethernet reset-mac-address ether1;” then i noticed that when it breaks the “step” var is set to 1.
So it seems that the break is not related so much to a particular operation but rather to an amount of time after which the script stops.
This problem occurs randomly, sometimes it runs correctly for 5-6 times consecutively, and then fails..

This is the script:

# Script name: watchdog_TFTP
# Ver. 10-03-2017

# Delay script start for 1 sec.

:delay 1;

# Set local and global vars
# Use step var to check where the script exit.

:local lastIpfile lastip.txt;
:global step 0;
:global getIP 0;
:global fready 0;
:global IPconfig set.txt;
:global lastIP 0;
:global WanIP 0;
:global IPset 0;


# Reset Mac address on  Wan Ether1 port (usefull when this configuration is restored on other RBs)

/interface ethernet reset-mac-address ether1;

:delay 400ms;

:set step 1;

# Check IP address set on start

:set $getIP [/ip address get [find interface="ether1"] address];
:set step 2;
:set $WanIP $getIP;
:set step 3;

# Check if file lastipfile contains last saved ip address
# and retrieve ip address stored on it
# if file does not exist, create it and store IP address on it.

:if ([:len [/file find name=$lastIpfile]] > 0) do={
	:set $lastIP [/file get [/file find name=$lastIpfile] contents];
	} else={
		:delay 1; 
};
:set step 4;

# Check if a file with an IP address is loaded via TFTP
# retrieve new IP address e store it in the file lastipfile
# execute script autoconfig
# delete file loaded via TFTP
# if no file is loaded via TFTP stay in an endless loop and beep
# Check also if the IP is changed via remote command  (getip != wanip)

:while (true) do={
:set $getIP [/ip address get [find interface="ether1"] address];
:if ([:len [/file find name=$IPconfig]] > 0) do={
	:set $IPset [/file get [/file find name=$IPconfig] contents];
	:set $WanIP $IPset;
	:set $lastIP $IPset;
	:set $getIP $IPset;
                :set $fready 1;
                /ip address set [/ip address find interface="ether1"] address=$WanIP;
                /file print file=$lastIpfile; 
	/file set $lastIpfile contents="$IPset";
                :delay 1;
                /file remove $IPconfig;
                :delay 2;
                :execute "autoconfig";
                :delay 2;
                :set $IPset 0;
                 };
:if ($getIP != $WanIP) do= {
                           :set $WanIP $getIP;
	           :execute "autoconfig";
                           :set $lastIP $getIP;
	            /file print file=$lastIpfile;
	            /file set $lastIpfile contents="$lastIP";
	           :delay 1;
                           :beep frequency=2000 length=1000ms;
} else={
	           :delay 1;
                           :if ($fready = 0) do={
                           :beep frequency=2000 length=200ms;
                           };
                 }; 

};

I’m using a RB493G with firmware 6.38.1

Any help would be much appreciated.
Thank you.