Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
socket inside a thread -> CPU halt
#1
Hi, 

i have a problem with the usage of a socket inside a thread. 

i want to create a simple http server and therefore use a socket inside a thread, something like this (simplified):

import _thread
import usocket

def _runner():
    http_socket = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
    http_socket.settimeout(None)
    http_socket.bind(usocket.getaddrinfo("0.0.0.0", 80)[0][4])
    http_socket.listen(1)
    while True:
        conn, addr = http_socket.accept()
        conn.send("fancy html")
        conn.close()
_thread.start_new_thread("http_runner",_runner, ())

i experiment a bit, with define the socket inside our outside of the thread but without effect. the micropython code runs until conn.send() 
Then a CPU halt is triggered:

Code:
Guru Meditation Error: Core  1 panic'ed (LoadProhibited). Exception was unhandle
d.
Core 1 register dump:
PC      : 0x400fd52b  PS      : 0x00060e30  A0      : 0x800f4c4d  A1      : 0x3f
fec620
0x400fd52b: mp_execute_bytecode at D:/esp32_win32/msys32/home/mm/esp/MicroPython
_LoBo/MicroPython_BUILD/components/micropython/py/vm.c:245 (discriminator 3)

A2      : 0x00000001  A3      : 0x3f814881  A4      : 0x3f81417c  A5      : 0x00
000000
A6      : 0xffffff30  A7      : 0x3f814882  A8      : 0x000000ff  A9      : 0x3f
814160
A10     : 0x3f83b4b0  A11     : 0x00000347  A12     : 0x3f430378  A13     : 0x3f
814178
A14     : 0x3ffec648  A15     : 0x3ffec6b0  SAR     : 0x0000001c  EXCCAUSE: 0x00
00001c
EXCVADDR: 0x0000010b  LBEG    : 0x4009c111  LEND    : 0x4009c121  LCOUNT  : 0xff
ffffff
0x4009c111: strlen at /home/jeroen/esp8266/esp32/newlib_xtensa-2.2.0-bin/newlib_
xtensa-2.2.0/xtensa-esp32-elf/newlib/libc/machine/xtensa/../../../../.././newlib
/libc/machine/xtensa/strlen.S:84

0x4009c121: strlen at /home/jeroen/esp8266/esp32/newlib_xtensa-2.2.0-bin/newlib_
xtensa-2.2.0/xtensa-esp32-elf/newlib/libc/machine/xtensa/../../../../.././newlib
/libc/machine/xtensa/strlen.S:96


Backtrace: 0x400fd52b:0x3ffec620 0x400f4c4a:0x3ffec6c0 0x400efe89:0x3ffec6f0 0x4
00fc9e5:0x3ffec710 0x400dc665:0x3ffec7b0
0x400fd52b: mp_execute_bytecode at D:/esp32_win32/msys32/home/mm/esp/MicroPython
_LoBo/MicroPython_BUILD/components/micropython/py/vm.c:245 (discriminator 3)

0x400f4c4a: fun_bc_call at D:/esp32_win32/msys32/home/mm/esp/MicroPython_LoBo/Mi
croPython_BUILD/components/micropython/py/objfun.c:283

0x400efe89: mp_call_function_n_kw at D:/esp32_win32/msys32/home/mm/esp/MicroPyth
on_LoBo/MicroPython_BUILD/components/micropython/py/runtime.c:601

0x400fc9e5: thread_entry at D:/esp32_win32/msys32/home/mm/esp/MicroPython_LoBo/M
icroPython_BUILD/components/micropython/py/modthread.c:121

0x400dc665: freertos_entry at D:/esp32_win32/msys32/home/mm/esp/MicroPython_LoBo
/MicroPython_BUILD/components/micropython/esp32/mpthreadport.c:612


CPU halted.

Board is LoLin D32 Pro with a ESP32-WROVER module and 4MB PSRAM
Firmware is built from https://github.com/loboris/MicroPython_ESP32_psRAM_LoBo with some little modifications via menuconfig

Did someone expierence a similar problem? Could someone point me to a direction how to avoid the cpu halt?  Huh
Reply
#2
You may check uftpserver.py which uses sockets and runs in thread without issues.

I think the issue is with conn, addr = http_socket.accept(), you should use conn, addr = http_socket.accepted() instead.
I'm not shure if it is documented somewhere, I'll check it.
Also, you may need to increase the default thread stack size (4K) with _thread.stack_size()

BTW, microWebSrv is included as frozen module, and exaple of usage is available in exaples directory.
Reply
#3
(12-14-2018, 10:08 AM)lobo Wrote: I think the issue is with conn, addr = http_socket.accept(), you should use conn, addr = http_socket.accepted() instead.
Thanks for your answer. I tried as you suggested, but unfortunatly it fails also. 
(12-14-2018, 10:08 AM)lobo Wrote: BTW, microWebSrv is included as frozen module, and exaple of usage is available in exaples directory.
Nice little WebServer! thanks for pointing that out. Unfortunatly this crashes also. 

a little more information:
I want to do http get requests from main thread and a webserver in a seperate thread. it seems that if i switch of the http get requests from main thread the microWebSrv runs fine in thread-mode. 

can it be that the module Requests and the webserver-thread/sockets get in each other's way?

here is the backtrace:
Code:
assertion "ATB_GET_KIND(block) == AT_HEAD" failed: file "D:/esp32_win32/msys32/h
ome/mm/esp/MicroPython_LoBo/MicroPython_BUILD/components/micropython/py/gc.c", l
ine 699, function: gc_realloc
abort() was called at PC 0x4011802f on core 1
0x4011802f: __assert_func at /home/jeroen/esp8266/esp32/newlib_xtensa-2.2.0-bin/
newlib_xtensa-2.2.0/xtensa-esp32-elf/newlib/libc/stdlib/../../../.././newlib/lib
c/stdlib/assert.c:63 (discriminator 8)


Backtrace: 0x40095b74:0x3ffed870 0x40095d3b:0x3ffed890 0x4011802f:0x3ffed8b0 0x4
00e7901:0x3ffed8e0 0x400e6ee5:0x3ffed900 0x400e8079:0x3ffed920 0x400e81bd:0x3ffe
d940 0x40117868:0x3ffed960 0x400f4baf:0x3ffed9a0 0x400efe89:0x3ffed9d0 0x400efef
9:0x3ffed9f0 0x400fe20a:0x3ffeda10 0x400f4c4a:0x3ffedab0 0x400efe89:0x3ffedb30 0
x400efef9:0x3ffedb50 0x400fe20a:0x3ffedb70 0x400f4c4a:0x3ffedc10 0x400efe89:0x3f
fedc40 0x400efef9:0x3ffedc60 0x400fe20a:0x3ffedc80 0x400f4c4a:0x3ffedd20 0x400ef
e89:0x3ffedd90 0x400efef9:0x3ffeddb0 0x400f9f0f:0x3ffeddd0 0x400f948f:0x3ffede20
0x400efe89:0x3ffede40 0x400efef9:0x3ffede60 0x400fe20a:0x3ffede80 0x400f4c4a:0x
3ffedf20 0x400efe89:0x3ffedf50 0x400f2f1c:0x3ffedf70 0x400f2f39:0x3ffedfc0 0x400
efe89:0x3ffedfe0 0x400fc9e5:0x3ffee000 0x400dc665:0x3ffee0a0
0x40095b74: invoke_abort at D:/esp32_win32/msys32/home/mm/esp/MicroPython_LoBo/T
ools/esp-idf/components/esp32/panic.c:649

0x40095d3b: abort at D:/esp32_win32/msys32/home/mm/esp/MicroPython_LoBo/Tools/es
p-idf/components/esp32/panic.c:649

0x4011802f: __assert_func at /home/jeroen/esp8266/esp32/newlib_xtensa-2.2.0-bin/
newlib_xtensa-2.2.0/xtensa-esp32-elf/newlib/libc/stdlib/../../../.././newlib/lib
c/stdlib/assert.c:63 (discriminator 8)

0x400e7901: gc_realloc at D:/esp32_win32/msys32/home/mm/esp/MicroPython_LoBo/Mic
roPython_BUILD/components/micropython/py/gc.c:699 (discriminator 1)

0x400e6ee5: m_realloc at D:/esp32_win32/msys32/home/mm/esp/MicroPython_LoBo/Micr
oPython_BUILD/components/micropython/py/malloc.c:132

0x400e8079: vstr_ensure_extra at D:/esp32_win32/msys32/home/mm/esp/MicroPython_L
oBo/MicroPython_BUILD/components/micropython/py/vstr.c:121

0x400e81bd: vstr_add_len at D:/esp32_win32/msys32/home/mm/esp/MicroPython_LoBo/M
icroPython_BUILD/components/micropython/py/vstr.c:125

0x40117868: stream_unbuffered_readline at D:/esp32_win32/msys32/home/mm/esp/Micr
oPython_LoBo/MicroPython_BUILD/components/micropython/py/stream.c:275

0x400f4baf: fun_builtin_var_call at D:/esp32_win32/msys32/home/mm/esp/MicroPytho
n_LoBo/MicroPython_BUILD/components/micropython/py/objfun.c:127

0x400efe89: mp_call_function_n_kw at D:/esp32_win32/msys32/home/mm/esp/MicroPyth
on_LoBo/MicroPython_BUILD/components/micropython/py/runtime.c:601

0x400efef9: mp_call_method_n_kw at D:/esp32_win32/msys32/home/mm/esp/MicroPython
_LoBo/MicroPython_BUILD/components/micropython/py/runtime.c:631

0x400fe20a: mp_execute_bytecode at D:/esp32_win32/msys32/home/mm/esp/MicroPython
_LoBo/MicroPython_BUILD/components/micropython/py/vm.c:1021

0x400f4c4a: fun_bc_call at D:/esp32_win32/msys32/home/mm/esp/MicroPython_LoBo/Mi
croPython_BUILD/components/micropython/py/objfun.c:283

0x400efe89: mp_call_function_n_kw at D:/esp32_win32/msys32/home/mm/esp/MicroPyth
on_LoBo/MicroPython_BUILD/components/micropython/py/runtime.c:601

0x400efef9: mp_call_method_n_kw at D:/esp32_win32/msys32/home/mm/esp/MicroPython
_LoBo/MicroPython_BUILD/components/micropython/py/runtime.c:631

0x400fe20a: mp_execute_bytecode at D:/esp32_win32/msys32/home/mm/esp/MicroPython
_LoBo/MicroPython_BUILD/components/micropython/py/vm.c:1021

0x400f4c4a: fun_bc_call at D:/esp32_win32/msys32/home/mm/esp/MicroPython_LoBo/Mi
croPython_BUILD/components/micropython/py/objfun.c:283

0x400efe89: mp_call_function_n_kw at D:/esp32_win32/msys32/home/mm/esp/MicroPyth
on_LoBo/MicroPython_BUILD/components/micropython/py/runtime.c:601

0x400efef9: mp_call_method_n_kw at D:/esp32_win32/msys32/home/mm/esp/MicroPython
_LoBo/MicroPython_BUILD/components/micropython/py/runtime.c:631

0x400fe20a: mp_execute_bytecode at D:/esp32_win32/msys32/home/mm/esp/MicroPython
_LoBo/MicroPython_BUILD/components/micropython/py/vm.c:1021

0x400f4c4a: fun_bc_call at D:/esp32_win32/msys32/home/mm/esp/MicroPython_LoBo/Mi
croPython_BUILD/components/micropython/py/objfun.c:283

0x400efe89: mp_call_function_n_kw at D:/esp32_win32/msys32/home/mm/esp/MicroPyth
on_LoBo/MicroPython_BUILD/components/micropython/py/runtime.c:601

0x400efef9: mp_call_method_n_kw at D:/esp32_win32/msys32/home/mm/esp/MicroPython
_LoBo/MicroPython_BUILD/components/micropython/py/runtime.c:631

0x400f9f0f: mp_obj_instance_make_new at D:/esp32_win32/msys32/home/mm/esp/MicroP
ython_LoBo/MicroPython_BUILD/components/micropython/py/objtype.c:341

0x400f948f: type_call at D:/esp32_win32/msys32/home/mm/esp/MicroPython_LoBo/Micr
oPython_BUILD/components/micropython/py/objtype.c:944

0x400efe89: mp_call_function_n_kw at D:/esp32_win32/msys32/home/mm/esp/MicroPyth
on_LoBo/MicroPython_BUILD/components/micropython/py/runtime.c:601

0x400efef9: mp_call_method_n_kw at D:/esp32_win32/msys32/home/mm/esp/MicroPython
_LoBo/MicroPython_BUILD/components/micropython/py/runtime.c:631

0x400fe20a: mp_execute_bytecode at D:/esp32_win32/msys32/home/mm/esp/MicroPython
_LoBo/MicroPython_BUILD/components/micropython/py/vm.c:1021

0x400f4c4a: fun_bc_call at D:/esp32_win32/msys32/home/mm/esp/MicroPython_LoBo/Mi
croPython_BUILD/components/micropython/py/objfun.c:283

0x400efe89: mp_call_function_n_kw at D:/esp32_win32/msys32/home/mm/esp/MicroPyth
on_LoBo/MicroPython_BUILD/components/micropython/py/runtime.c:601

0x400f2f1c: mp_call_method_self_n_kw at D:/esp32_win32/msys32/home/mm/esp/MicroP
ython_LoBo/MicroPython_BUILD/components/micropython/py/objboundmeth.c:70

0x400f2f39: bound_meth_call at D:/esp32_win32/msys32/home/mm/esp/MicroPython_LoB
o/MicroPython_BUILD/components/micropython/py/objboundmeth.c:83

0x400efe89: mp_call_function_n_kw at D:/esp32_win32/msys32/home/mm/esp/MicroPyth
on_LoBo/MicroPython_BUILD/components/micropython/py/runtime.c:601

0x400fc9e5: thread_entry at D:/esp32_win32/msys32/home/mm/esp/MicroPython_LoBo/M
icroPython_BUILD/components/micropython/py/modthread.c:121

0x400dc665: freertos_entry at D:/esp32_win32/msys32/home/mm/esp/MicroPython_LoBo
/MicroPython_BUILD/components/micropython/esp32/mpthreadport.c:612


CPU halted.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)