Chat freely about anything...

User avatar
By wjr
#62067 I try to work my assignments for my first nodeMCU pull request:
https://github.com/nodemcu/nodemcu-firm ... w-17892825

I tried to set up a error handler using the task api as requested by Philip Gladstone.

Initail hints on the tastk api came from
http://nodemcu.readthedocs.io/en/master ... loper-faq/
and task.h

Two problems:
  • I expected the param field being handled transparentely, but my content gets modified
  • The very reason to use the task api was to get sure to have a lua context again, but instead I get a lua panic resultiong in module reboot

I tagged and pushed the code to github for reference:
https://github.com/wolfgangr/nodemcu-fi ... net_info.c

As my debug print shows, registering the callback, placing the task and receiving the task callback basically works.
This is the essence of my task callback:

Code: Select allstatic void net_if_error (task_param_t param, uint8_t prio ) {
  NET_INFO_DEBUG(" entering net_if_error \n");
  // get lua state (I'm supposed to be able in the task, right?)
  lua_State *L = lua_getstate();
  NET_INFO_DEBUG("   ... in error got lua state: 0x%x\n", L);

  char *errmsg = (char*) param ;
  // char errmsg[] = "lets try it with a dummy message";
  NET_INFO_DEBUG("   ... in error got message: %s - pointers were 0x%x from 0x%x  \n", errmsg, errmsg , param );
 
  lua_pushstring(L , errmsg);
  NET_INFO_DEBUG("   ... pushed my msg to the stack...\n" );
 lua_error(L);
  NET_INFO_DEBUG("   ... and called a lua error (will I see this?)\n" );
}


A test run on the module yields

Code: Select allnet_info.dummy()
 ### DEBUG: net_info ### :  entering task error dummy
 ### DEBUG: net_info ### :     ... in dummy got lua state: 0x3ffefb98
 ### DEBUG: net_info ### :     and my message is I am the stupid hello task dummy
 ### DEBUG: net_info ### :     have casted my param from 0x3ffffd30 to 0x3ffffd30
 ### DEBUG: net_info ### :     ... have postet my msg task now...
>  ### DEBUG: net_info ### :   entering net_if_error
 ### DEBUG: net_info ### :     ... in error got lua state: 0x3ffefb98
 ### DEBUG: net_info ### :     ... in error got message: V - pointers were 0x3ffffd30 from 0x3ffffd30 
 ### DEBUG: net_info ### :     ... pushed my msg to the stack...
PANIC: unprotected error in call to Lua API (V)

 ets Jan  8 2013,rst cause:2, boot mode:(3,6)


As we see, the pointer 0x3ffffd30 was not modified from the task machine, but the content changed from "I am the stupid hello task dummy" to "V" (this is reproducible).

However, even when I bypass the param handling, using a literal dummy string like "lets try it with a dummy message", I still get the panic. So there must something wrong with the lua state, although it is still the same pointer as in the function registering the task.

When I call lua_err without the task api, it behavs as expected and documented in https://www.lua.org/manual/5.1/manual.html#lua_error : a controlled lua error with error message, stack trace and return to the command line - no reboot:

Code: Select all=net_info.panic()
=net_info.panic()
 ### DEBUG: net_info ### :     ... this is my panicking message: Does lua_error really produce a panic?
 ### DEBUG: net_info ### :     ... pushed my msg to the stack...
Does lua_error really produce a panic?
stack traceback:
   [C]: in function 'panic'
   stdin:1: in main chunk


From the traces, I suspect that the task api modifes the data referred by param, thus screwing the lua state.
But what then should go to task_post param?
Where can I find doucmentation?
I grepped the source tree and the chain of include files, but found no help there.
User avatar
By wjr
#62095 As I figured out, the two problems are not interrelated the way I thought.

The string issue can be solved by declaring the error message (which ia a string literal) outside of the scope of the function.
I thought that I can be sure that string literals are allocated in the program code and live forever, independent of the scoep of their declaration.
A beginners mistake?
Googling I found lots of stackexchange discussions confiming my view. I'm not a C pro, but looks like I've hit a peculiarity of the nodemcu compiler.

I could malloc / free the string, but that just requires additional RAM requirement, but does not save the flash space for the literal. But when I want dynamic error messages, I will have to something like that.

May be I try luaL_Buffer.
User avatar
By wjr
#62321 After getting the message string to the error handler, I see that the panic is labelled with my own dummy message:

Code: Select all ### DEBUG: net_info ### :     ... in error got message: I am the outside hello task dummy - pointers were 0x4026b6a0 from 0x4026b6a0 
 ### DEBUG: net_info ### :     ... pushed my msg to the stack...
PANIC: unprotected error in call to Lua API (I am the outside hello task dummy)


So I conclude that there is no real reason to panic, but the normal error handler function is lost / redirected to panic instead.
Is this expected behaviour?