TCP Exception Handling

Lua embedded application development for GX and LS series AirLink gateways

TCP Exception Handling

Postby stEngDev » Fri Jul 07, 2017 2:42 pm

Hi,

I know that Lua does not have exception handling so I'm wondering how I can handle network communication errors. E.g.

I have a Lua application that polls over TCP. I can test if there is a TCP connection before I do a TCP read or send but what happens if the connection is lost just before I do a read or just before a send?

Code: Select all
function n.xmlpoll(msg)
   log.trace("TCP",'INFO',"TX:"..msg)      
 
   local err = tcp:send(msg);                --- line 74

   if (err == nil) then
      print ("Tcp connect issue")
      return nil
     end


I got this error:

Code: Select all
Jul  7 13:26:50 notice AAF: Fri Jul  7 13:26:50 2017 SCHED-ERROR: In thread: 0x23768: ./tcpc.lua:74: attempt to index upvalue 'tcp' (a nil value) stack traceback:
Jul  7 13:26:50 notice AAF:    ./tcpc.lua:74: in function 'xmlpoll'


How can I protect for this. At the very least I want the whole application to restart automatically not stay dead as it did.

Thoughts and solutions very welcome.

Kind Regards,
Steve
stEngDev
 
Posts: 24
Joined: Wed Jul 29, 2015 7:33 am
Has thanked: 2 times
Have thanks: 0 time

Re: TCP Exception Handling

Postby lbarthelemy » Mon Jul 10, 2017 3:06 pm

Hi,

"Lua does not have exception handling"
-> Actually there are some API meant to handle Lua errors.
If there is a function you're calling that might produce a Lua errors you want to catch, you might want to use "copcall".
First load it:
Code: Select all
require"coxpcall"


Then use the new global "copcall" function, it has the same API as "pcall" function documented there:
https://source.sierrawireless.com/resou ... 1_3/#pcall
Code: Select all
local pcall_status, command_return1, command_return2 = copcall(cmd, cmd_param1, cmd_param2) 

(Note: copcall is similar to "standard" pcall but it also handles coroutine use cases).

" can test if there is a TCP connection before"
"I got this error: /tcpc.lua:74: attempt to index upvalue 'tcp'"
-> I would need tcp variable definition in your code to help you more on that topic.


"At the very least I want the whole application to restart automatically not stay dead as it did."
-> If your application process ends with a system exit code different from 0, then it will be restarted.

So you may use copcall to catch errors and then call os.exit(-1) to force the restart of your application.
But in your case, the upvalue error look like something that should be able to fix in the app.

You also can use this kind of code to catch your main "thread" termination.
This is to *replace*/improve the default and simple "sched.loop" call that must already be in your app.
Code: Select all
--register to 'die' to properly log exits
local mainthread = sched.run(main)
sched.sigOnce(mainthread, 'die', function(ev, status) log('MYAPP', 'INFO', 'Exiting [%d]', status and 0 or -1) os.exit(status and 0 or -1) end)
sched.loop()

Please note that this supposes that the "main" function will keep running until an error happens.
So if you also use copcall within the main "thread", you might not jump into that thread termination handler very often , but it remains a good practice anyway.

Let me know if it helps you or if you need more details on any of the above solutions.

Regards,
User avatar
lbarthelemy
 
Posts: 50
Joined: Thu Jan 08, 2009 9:57 am
Location: Toulouse, France
Has thanked: 6 times
Have thanks: 11 times


Topic Tags

comms, exception handling, tcp

Return to ALEOS Application Framework

Who is online

Users browsing this forum: No registered users and 1 guest