Callback into mJS from another thread: is it safe?


I have a C API which takes a callback from the mJS world (including the userdata void * parameter) and calls that callback some time later from another RTOS thread (this is ESP32, so FreeRTOS). The thread that is doing the calling-back is allocated 8 kbytes of stack.

If I do something simple, like print("Hello.\n"), in the mJS callback then all works fine but when I do anything more adventurous: perform some processing on received data, access other mJS global, variables, I get random errors, e.g. mJS complains that the global variable is not defined or even that print is not defined.

My question is: is it safe to call back into mJS from another RTOS thread? If so then I guess I must just be suffering from stack overflow or a waggly pointer.


Yeah, mjs is expecting to be executed by the mongoose os thread.

You might try this:


Ah, unfortunately I don’t seem to have such a function, I’m using standalone mJS rather than Mongoose OS as a whole.


Ah I see.
Then, make your own!
The idea is: mjs VM should execute by only one task / thread. Thus, is another thread should execute the callback, do not execute it but instead signal the mjs task to do it. Use whatever IPC API is available in your environment.


Understood, will do.