Finding corrupt LUUP Code

Without picking through all of them, it seems like quite a few are simple errors that have just gone unnoticed for some time. A syntax error in scene or startup Lua wouldn’t impact the entire system until after a reload, so to the casual observer all may seem fine, until suddenly it’s not.

That still does not explain all of them, that is true, but given what we know about memory consumption and leaks, it’s not entirely surprising that after some random time, if memory is impacted, a sudden inability to execute Lua cleanly occurs. Many have said that a reboot of their system has resolved their issue, which for me makes some memory leak/exhaustion a high likelihood. Consider, there may be code paths rarely taken, that suddenly and just for the moment and circumstances of the day are more frequent. With big transitions going on at Vera, and a recent spate of downtime events for various subsystems, who knows what ripple effect we might see. For example, what if there’s a memory leak in the code that checks for plugin updates? If apps.mios.com or its loadshared aliases aren’t available, an error exit path is repeatedly taken as your Vera keeps retrying, but this path leaks memory or fails to return some other used resource (file handle, socket, etc.), eventually to destruction. This could even happen outside of LuaUPnP, in any of the numerous scripts and daemons that run in the system, slowly eroding some resource until LuaUPnP itself is affected.

Im still getting the random error but i cant pinpoint it

Hi

Have been having some intermittent issues recently so I tried this script but as this thread has not been updated for a while, I was just wondering if there had been any developments with the original code @akbooer created - everything reports line 1 still and all of my scenes with luup code report an error, when running the script ?

All scenes with errors below report ‘Success’ when manually run…

Print output extract.

1     Midnight Shutdown     
     ok   
       
2     Switch Matrix to Sky TV     
     [string "scene_code_test"]:1: unexpected symbol near ''     
3     Switch Matrix to Apple TV     
     [string "scene_code_test"]:1: unexpected symbol near ''     
4     Switch Matrix to NAS     
     [string "scene_code_test"]:1: '=' expected near '=='     
5     HDMI Matrix Input 4     
     [string "scene_code_test"]:1: unexpected symbol near ''     
6     HDMI Matrix LED OFF     
     [string "scene_code_test"]:1: unexpected symbol near ''     
7     HDMI Matrix LED ON     
     [string "scene_code_test"]:1: unexpected symbol near ''     
8     Turn Matrix Off     
     [string "scene_code_test"]:1: '=' expected near '=='     
9     Turn Matrix On     
     [string "scene_code_test"]:1: unexpected symbol near ''     
10     Night Light     
     ok     
     
11     Day Light     
     ok     
     
12     Turn Everything On     
     [string "scene_code_test"]:1: unexpected symbol near ''     
13     Turn Everything Off     
     [string "scene_code_test"]:1: '=' expected near '=='     
14     Dim Dining Room Lights to 50%     
18     Kids Bedtime Countdown     
     [string "scene_code_test"]:1: '=' expected near ''     
20     Kids Joke     
     [string "scene_code_test"]:1: unexpected symbol near ''   `

Have you tried the LuaView plugin by @rigpapa?

1 Like

The scene Lua is probably base64 encoded if you are doing this on Vera. If the encoded_lua value is 1, then it is encoded and you need to decode it before passing it to loadstring():

local real_lua = sc.lua
if sc.encoded_lua == 1 then
    local mime = require "mime"
    real_lua = mime.unb64( real_lua )
end
local ok,err = loadstring( real_lua, "scene_code_test" )

Thanks

Do you mean like this… (although I’ve just run this the results look the same as before ?)

local json = require "dkjson"
for i,s in pairs (luup.scenes) do
    print (i, s.description)
    local _,j = luup.inet.wget ("127.0.0.1:3480/data_request?id=scene&action=list&scene=" .. i)
    local sc = json.decode (j)
	local real_lua = sc.lua
	if sc.encoded_lua == 1 then
    local mime = require "mime"
    real_lua = mime.unb64( real_lua )
end
    if sc and sc.lua then
        --print('', sc.lua)		
        local ok, err = loadstring (sc.lua, "scene_code_test")
        if ok then 
            print ('',"ok") 
            print (sc.lua)
        else 
            print ('', err) 
        end
    end
end

After making real_lua, that’s what you need to be using everywhere below that.

Corrected/improved:

local json = require "dkjson"
local mime = require "mime"
for i,s in pairs (luup.scenes) do
    print (i, s.description)
    local _,j = luup.inet.wget ("http://127.0.0.1:3480/data_request?id=scene&action=list&scene=" .. i)
    local sc = json.decode (j)
    if sc then
        local real_lua = sc.lua or ""
        if sc.encoded_lua == 1 then
            real_lua = mime.unb64( real_lua )
        end
        if real_lua ~= "" then
            --print('', real_lua)     
            local ok, err = loadstring (real_lua, "scene_code_test")
            if ok then 
                print ('',"ok") 
                print (real_lua)
            else 
                print ('', err) 
            end
        end
    end
end

Great many thanks, that does it and the results are very different now too.

The majority are ‘ok’ - but a few do report an issue/error.

Some are also blank, is that expected too ?
Print extract below (removed lua code)

Print output

1     Midnight Shutdown     
2     Switch Matrix to Sky TV     
     ok     
3     Switch Matrix to Apple TV     
     ok     
4     Switch Matrix to NAS     
     ok     
5     HDMI Matrix Input 4     
     ok     
6     HDMI Matrix LED OFF     
     ok     
7     HDMI Matrix LED ON     
     ok     
8     Turn Matrix Off     
     ok     
9     Turn Matrix On     
     ok     
10     Night Light     
11     Day Light     
12     Turn Everything On     
     ok     
13     Turn Everything Off     
     ok 

And one very interesting one…

No idea where that symbol/character came from and certainly can’t see it in the code ???

37     Prowl - Test Scene     
     [string "scene_code_test"]:1: unexpected symbol near 'â'

Ok, found the source of that last one, and it’s only visible within the Scene creator view

But not in the Luup/LUA code view - however checking online, the quote/speech mark looks to the same type as the other one. What Unicode character is this ?

Is this a UTF-8 issue or something like that ?

Ok, it looks like it was indeed the quote/speech marks - they seem to have been the issue…

Using the site I shared earlier, the original Unicode version which resulted in that strange symbol/character was a …

U+201C : LEFT DOUBLE QUOTATION MARK {double turned comma quotation mark}

Using a coding app, I changed it , which turned out to be the follow, and it’s now OK

U+0022 : QUOTATION MARK

I’m surprised there is not something that runs that converts the characters used in any lua code to the correct format/type?

Guessing what is “incorrect” is guaranteed to be… incorrect, for something/someone.

So true :slight_smile: - does that mean, in the absence of anything else - the code shared here by you and @akbooer is the only way to identify if any characters used ‘successfully’ in a scene are actual the wrong format and therefore ‘incompatible’ ??

Well, @akbooer had earlier suggested LuaView, which will highlight them pretty quickly, and also serves as an editor for all scenes and startup Lua on one page, so I think it’s less error-prone. Immediate feedback is a Good Thing. LuaView uses the same, ubiquitous JS library for its editor as AltUI, so that’s also an option. There are also many options for Lua on the command line, including your Vera itself (ssh into it and type lua), any *nix platform (RPi, NUC, desktop, etc.), and even Windows itself. That’s all short of going to an IDE, which is a matter of taste, IMO. But really, give LuaView a try. It uses about no resources, because it has no real runtime on the Lua side; it’s just a Lua plugin stub with a JavaScript UI for doing to the editing.

Imagine doing this without a web interface.

Thanks - I’ll take a look at LuaView…

If LuaView & LuaTest were to somehow merge, that might well be my holy grail;)

@rigpapa

Does the LUAView plugin just list all your Vera scenes LUA code in one view? Or will it also highlight any issues with any of the LUA code?

I’ve been seeing the “Error in lua for scenes and events” message for quite some time and it’s really starting to annoying me as I can’t find if I have any code errors or typos.

Thank you.

Yes, one page, Lua for all scenes, plus the startup Lua. It uses the same JavaScript-based syntax-highlighting editor that’s used in ALTUI.

Remember, the LuaUPnP log file is another place you can look. Do a Luup reload, then look at the startup messages. The assembly of startup and scene Lua happens early, before plugins are started.

1 Like

OK thanks. So any code errors will be highlighted in red? Or obvious to spot?

I’ll have a look at the LuaUPnP log as well.

I dont think it’s LUA code in any of my PLEG actions as that comes up with a different message in the blue banner if there is a syntax error in LUA code in PLEG.

Had a quick look in LUAView I can’t see anything obvious. I’m normally pretty good at checking and testing my codes.

Maybe it’s related to what @Sorin said back here.

You may also have a runtime error (like a reference through nil). The LuaUPnP log is the place to find things like that.

I’ve followed Sorin’s instructions about looking at the user data and looking for scenes LUA code that is not encoded.

I have found one scene that wasn’t encoded. So I removed the code, saved the scene and then added the code back in.

I’ve also removed two old line of none needed code from my Startup Lua.

Will see if this fixes things.