Electron: blinking red when UDP socket opens in a thread

electron
Tags: #<Tag:0x00007fe21fe79768>

#1

Hi all,
as the title, I have the main thread that connects the Electron to 3rd-party sim cell but I get red blinking led when I try to open a UDP socket in another thread…
A resume of the flow is pasted here: https://pastebin.com/9spVtYzi

The log:

0000049387 [app] INFO: Connected in 1 seconds
0000049387 [app] INFO: Connection...
0000050266 [app] INFO: PM: pm_count<=0
0000050266 [app] INFO: NODE ID: 44592326
0000050268 [app] INFO: >> CONNECTED!
0000050268 [app] INFO: >> Sending
0000050268 [app] INFO: OPENING UDP 60004
    50.267 AT send       4 "AT\r\n"
    50.267 AT read  +   31 "\r\n+CREG: 5,\"
Serial connection closed.

Basically it hangs at
mainUDP.begin(PORT);

Am I missing something? I’m already checking about Cellular.ready before opening the UDP socket but I get blinking red :confused:


#2

You can actually embed the code in a preformatted (code) block image

And you may also want to state what kind of red blink you see.
I’d guess it’s an SOS+13 (stack overflow)

Also, in some previous device OS version calling Log.xxxx() inside an independet thread caused problems too - not sure whether this is already fixed in 1.4.0.


#3

You can actually embed the code in a preformatted (code) block image

You mean instead of pastebin or you want the complete code? (It’s quite long :frowning: )
I checked the SOS sequence. Yes It’s stack overflow.

I tried to comment all Logs.xxx() and also SerialLogHandler, but it doesn’t solve the problem :frowning:
I checked with only SerialLogHandler too, but it hangs always after the UDP begin (basically around 50sec, at those AT commands).


#4

Yup, instead of pastebin :wink:

If it’s not Log it may well be UDP.
However, since we now know it’s a stack overflow you can donate more stack to the thread as a fourth parameter in the Thread() constructor.
The default value is 3K.


#5

I went into this problem deeply.
The problem was due to a char array allocation BEFORE opening the socket.

SpiffsParticleFile f = fs.openFile(filePath, SPIFFS_O_RDWR|SPIFFS_O_CREAT|SPIFFS_O_APPEND);
if (f.isValid()) {
     int len = f.length();
     char buf[len];

with len = 4096 and it’s probably too large :sweat_smile: (It happens even in the main Thread)
It’s not clear why the electron blinks red only when I open the UDP socket and not when I declare the array though.

Using the heap apparently fixed the problem.

char* buf = (char*) malloc(len);


#6

When you almost exhaust the stack with your local variably any function that may acquire more than what’s left the stack overflow will happen. As such opening the UDP socket is probably somewhat heavy on the stack but could’t execute properly after the array had claimed too much of that stack.