Need help with a script that fails on some routers

This is just part of a bigger script, but for some reason it starts to log error to the log when this part is run.

When cut/paste this to a terminal on a hex (750g3) router:

{
:local CmdHistory true
:local train [:tonum [:pick [/system resource get version] 0 1]] 

:if ($train > 6 and $CmdHistory) do={
:global cmd
:local f 0
:foreach i in=[/system history find] do={
:if ($i = $cmd) do={ :set f 1 }
:if ($f != 1) do={
:put "StartCMD"
:put [/system history get $i]
:put "EndCMD"
}
}
:global cmd  [:pick [/system history find] 0]
}
}

I get the error marked in red:

[admin@750] > {
{… :local CmdHistory true
{… :local train [:tonum [:pick [/system resource get version] 0 1]]
{…
{… :if ($train > 6 and $CmdHistory) do={
{{… :global cmd
{{… :local f 0
{{… :foreach i in=[/system history find] do={
{{{… :if ($i = $cmd) do={ :set f 1 }
{{{… :if ($f != 1) do={
{{{{… :put “StartCMD”
{{{{… :put [/system history get $i]
{{{{… :put “EndCMD”
{{{{… }
{{{… }
{{… :global cmd [:pick [/system history find] 0]
{{… }
{… }
interrupted
error - contact MikroTik support and send a supout file (10)

[admin@750] >

Even if this part give error message, rest of script runs fine. It may be a bug on one of my router.

It works on 7.14beta8 on RB1100AHx4. It does not have a long history (4 records). But $cmd has a value at end without error and :put did there thing.

All my hEX-S run Dude+V6, so can’t test it there. So if it’s platform/version thing, IDK…

Strange thing that it only seems to be one router with problem.
RB951 with 17.3.3 works fine.

Maybe is some memory stack issue with large history on that device… Regarding code, you can optimize loop using :while instead :foreach to iterate while condition $f!=1

All script error did go a way after upgrading from 7.13.3 to 7.13.4

Error still there in 7.14.1 …

[eddieb@ccr1009] > /system/history/print 

error - contact MikroTik support and send a supout file (10)

It might indeed have something to do with the size of the history.
On my CCR there is a lot of history and it gives that error
On a HAPAX3 I had this error and a couple of hours later it was gone
SUP-148095 created