Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP8266 crashes when used with low RAM/Heaps available. EXCEPTION (29) #56

Closed
efeuentertainment opened this issue Oct 20, 2017 · 7 comments

Comments

@efeuentertainment
Copy link

efeuentertainment commented Oct 20, 2017

Hello
I ran into a problem which i tried to investigate in the last week.
I'm using an ESP8266, wemos D1mini, and was able to reproduce the crash using the PhotoFromURL example.
This submitted issue might probably be a dublicate of #51 and #52 . Feel free to move and modify my post in any way you see fit.
Also note that this issue might be caused by ArduinoJSON or another library which Universal-Arduino-atelegram-Bot relies upon.

The crash occurs when sending messages. In my project the ESP crashes after sending 2-5 messages. Using the unmodified PhotoFromURL example it takes about 100-300 messages within ~3 minutes, which resulted in an Exception (29) crash.
I slightly modified the example and filled the RAM with a big global char array and wrote ESP.GetFreeHeap() to the serial output, which triggers the crash after sending ~10 messages.

The exception always occurs when the free heap size decreses beneath ~6500-5700.
The free heap size increases automatically when ~3min have passed. I assume the old buffers or something gets deleted when they are older than ~3min. The RAM gets filled the fastest when alternating between different messages, using my sketch: /get_test_photo and /status .
I enabled the _debug bool in UniversalTelegramBot.h

This is the modified sketch:
telegram_exceptionist.zip

Here 3 possible combinations and their outcome:

telegram_exeptionist.ino using WiFiClientSecure without filling RAM (no char array):
Sketch uses 296285 bytes (28%) of program storage space. Maximum is 1044464 bytes.
Global variables use 40484 bytes (49%) of dynamic memory, leaving 41436 bytes for local variables. Maximum is 81920 bytes.
Uploading 300432 bytes

free heaps:
at start 17300 B
crashes after 6160 B
(this was almost impossible since 100-300 messages have to be sent, but the RAM starts to free up again after ~3min.)

Serial output: [expand] GET Update Messages .... connected to server

{"ok":true,"result":[{"update_id":72978882,
"message":{"message_id":1980,"from":{"id":xxxxxxxx,"is_bot":false,"first_name":"Dejan","last_name":"Lauber","language_code":"en-US"},"chat":{"id":xxxxxxxx,"first_name":"Dejan","last_name":"Lauber","type":"private"},"date":1508349619,"text":"/status","entities":[{"offset":0,"length":7,"type":"bot_command"}]}}]}

incoming message length357
Creating DynamicJsonBuffer

got response
handleNewMessages
1
free heap: 6344 B

msg length: 298
sendMessage start: 8789721 ms
SEND Post Message

Exception (29):
epc1=0x4000e1b2 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000004 depc=0x00000000

ctx: cont
sp: 3fff0ee0 end: 3fff1500 offset: 01a0

stack>>>
3fff1080: 3fff03f4 000013d6 000013d6 40223beb
3fff1090: 00000208 3fff560c 3fffad2c 40223d84
3fff10a0: 00000104 00009ca0 00001394 3fff560c
3fff10b0: 00000040 00000082 3fffad14 40224036
3fff10c0: 00800000 00000080 00000000 00000041
3fff10d0: 00000040 0000003f 40205f50 00000041
3fff10e0: 3fffb6bc 3fff560c 3fff6844 3fff48b4
3fff10f0: 3fffbbd0 ffffffff 3fff560c 3fff560c
3fff1100: 00000040 3fff560c 3fffad14 3fff560c
3fff1110: 00000040 3fff560c 3fffad14 40224b92
3fff1120: 3fff579c 0000003f fdd3d074 00000001
3fff1130: 3fff560c 3fffa51c 3fffad14 00000001
3fff1140: 00000010 3fff560c 0000000f 40224ded
3fff1150: 3fff6844 3fff489c 0000000f 00000001
3fff1160: 00000001 3fffa6c4 3fff28c9 40223c50
3fff1170: 00000100 3fffa1a4 3fff282f 00000000
3fff1180: 00000100 3fffa1a4 3fff282f 40225e79
3fff1190: 3fff11c0 00000000 000000d0 00000030
3fff11a0: 73020732 79a2f204 3fff9904 00000004
3fff11b0: 00000004 3fff92e4 3fff2829 40221f84
3fff11c0: 57cc0203 f0c9c297 a217994f 3da31a57
3fff11d0: f8f06d78 322787ff 99383f74 a472b73f
3fff11e0: 039c900d df83dd48 41806564 07324e89
3fff11f0: 00000004 3fff2829 3fff92e4 40222385
3fff1200: 00000000 600011f0 3fff9904 3fffa8d4
3fff1210: 00000000 00000004 00000004 40203097
3fff1220: 3fff282d 3fff2829 3fff671c 00001200
3fff1230: 000012a0 3fff3a94 00000034 00000004
3fff1240: 3fff92e4 3fff2829 00000004 00000004
3fff1250: 00000004 3fff2829 3fff92e4 40221d7c
3fff1260: 00000000 3fff2829 3fff92e4 40222124
3fff1270: 00000010 3ffe915c 00000011 401004d8
3fff1280: 3fff12d0 0000000e 00000010 00000000
3fff1290: 3fff1e94 3ffe915c 3fff92e4 01000000
3fff12a0: 3fff12d0 3fff02bc 3fff92e4 40221ed4
3fff12b0: 3ffe915c 00000000 3fff1edc 00001387
3fff12c0: 00861f55 3fff02bc 3fff1edc 40203509
3fff12d0: 000001bb 3fff9904 3fff02bc 402029ee
3fff12e0: c5a79a95 3fff0418 3fff1338 3fff1390
3fff12f0: 000001bb 3fff02bc 3ffe915c 40203719
3fff1300: 3ffe9780 c5a79a95 3ffe9780 c5a79a95
3fff1310: 3fff139c 3fffad90 3fff0248 40204ab5
3fff1320: 3fff1350 00000001 3ffe91b9 3fff143c
3fff1330: 00000040 3fff1390 3fffa8bc 0000000f
3fff1340: 00000000 0000003c 3fff1390 40205a7f
3fff1350: 3fff03f4 000006d1 3fff1390 40205ab1
3fff1360: 00863e1e 3fffad90 3fff1390 40205b74
3fff1370: 3ffe93ec 3fffad90 3fff0248 3fff143c
3fff1380: 00863e1e 3fffad90 3fff0248 40204cf8
3fff1390: 3fff54bc 0000003f 0000003c 3fff4e0c
3fff13a0: 0000000f 00000000 3fff5504 0000003f
3fff13b0: 0000003c 3fffad90 00000000 40203cb0
3fff13c0: 00000010 3fff1430 3fff1430 3fff143c
3fff13d0: 3fff0248 3fff1430 3fffad90 40204dc9
3fff13e0: 3ffe9620 000005f5 3fffae94 00000400
3fff13f0: 3fff1448 00000000 3fff1430 40205b02
3fff1400: 3ffe937d 00000000 3fff0254 00000000
3fff1410: 3fff0418 00000000 3fff0254 40202164
3fff1420: 00000000 3fff25e0 3fff026c 402059d4
3fff1430: 3fff4df4 0000000f 00000000 3fff50a4
3fff1440: 0000012f 0000012a 3fff4ddc 0000000f
3fff1450: 00000009 3fff25d4 0000012f 0000012a
3fff1460: 00000000 00000000 00000000 00000000
3fff1470: 00000000 00000001 3ffe91b9 40206378
3fff1480: 00000000 3fff4984 0000000f 00000005
3fff1490: 3fff9ffc 0000000f 00000007 3fffa18c
3fff14a0: 0000000f 00000009 3fff0418 402056c5
3fff14b0: 00000001 00000001 3fff0418 402056f0
3fff14c0: 3fffdad0 3fff02d8 3fff0418 3fff04d4
3fff14d0: 3fff0244 3fff0248 00000001 40202389
3fff14e0: 3fffdad0 00000000 3fff04cc 40205f9c
3fff14f0: feefeffe feefeffe 3fff04e0 40100718
<<<stack<<<

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

ets Jan 8 2013,rst cause:4, boot mode:(1,6)

wdt reset

telegram_exeptionist.ino using WiFiClientSecure and filling RAM:
Sketch uses 296389 bytes (28%) of program storage space. Maximum is 1044464 bytes.
Global variables use 50580 bytes (61%) of dynamic memory, leaving 31340 bytes for local variables. Maximum is 81920 bytes.
Uploading 300544 bytes

free heaps:
at start 7400 B
crashes after 6000 B

Serial output: [expand] GET Update Messages .... connected to server

{"ok":true,"result":[{"update_id":72978902,
"message":{"message_id":2016,"from":{"id":xxxxxxxx,"is_bot":false,"first_name":"Dejan","last_name":"Lauber","language_code":"en-US"},"chat":{"id":xxxxxxxx,"first_name":"Dejan","last_name":"Lauber","type":"private"},"date":1508350789,"text":"/status","entities":[{"offset":0,"length":7,"type":"bot_command"}]}}]}

incoming message length357
Creating DynamicJsonBuffer

got response
handleNewMessages
1
free heap: 6000 B

msg length: 298
sendMessage start: 29404
SEND Post Message

Exception (29):
epc1=0x4000e1b2 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000004 depc=0x00000000

ctx: cont
sp: 3fff3650 end: 3fff3c70 offset: 01a0

stack>>>
3fff37f0: 3fff2b64 000009db 000009db 40223c0f
3fff3800: 00000208 3fffbf7c 3fff5e3c 40223da8
3fff3810: 00000104 00004cc8 00000999 3fffbf7c
3fff3820: 00000040 00000082 3fff5e24 4022405a
3fff3830: 00800000 00000080 00000000 00000041
3fff3840: 00000040 0000003f 40205f74 00000041
3fff3850: 3fff8e54 3fffbf7c 3fff9edc 3fff9c0c
3fff3860: 3fff9368 ffffffff 3fffbf7c 3fffbf7c
3fff3870: 00000040 3fffbf7c 3fff5e24 3fffbf7c
3fff3880: 00000040 3fffbf7c 3fff5e24 40224bb6
3fff3890: 3fff9eb4 0000003f 9ff4c9ed 00000001
3fff38a0: 3fffbf7c 3fff5e0c 3fff5e24 00000001
3fff38b0: 00000010 3fffbf7c 0000000f 40224e11
3fff38c0: 3fff9edc 3fff9bf4 0000000f 00000001
3fff38d0: 00000001 3fff6724 3fffa03e 40223c74
3fff38e0: 00000100 3fff9aa4 3fff9fb7 00000000
3fff38f0: 00000100 3fff9aa4 3fff9fb7 40225e9d
3fff3900: 3fff3930 00000000 000000d0 00000030
3fff3910: 02efe630 9225d911 6acfa58b 00000004
3fff3920: 00000004 3fff6144 3fff9fb1 40221fa8
3fff3930: c4ec0203 96fdef07 74983f52 13204e31
3fff3940: 14fb0e32 6663e8ed a078d46e 1493c32d
3fff3950: 44b68c7d de41381b 0b599e8c e63010c9
3fff3960: 00000004 3fff9fb1 3fff6144 402223a9
3fff3970: 74189ebf 4b3693ed bb2b5f8c 3fff611c
3fff3980: 00000000 00000004 00000004 402030bb
3fff3990: 3fff9fb5 3fff9fb1 3fff997c 00001200
3fff39a0: 000012a0 3fffb21c 00000034 00000004
3fff39b0: 3fff6144 3fff9fb1 00000004 00000004
3fff39c0: 00000004 3fff9fb1 3fff6144 40221da0
3fff39d0: 00000000 3fff9fb1 3fff6144 40222148
3fff39e0: 00000010 3ffe91c4 00000011 401004d8
3fff39f0: 3fff3a40 0000000e 00000010 00000000
3fff3a00: 3fff7c7c 3ffe91c4 3fff6144 01000000
3fff3a10: 3fff3a40 3fff031c 3fff6144 40221ef8
3fff3a20: 3ffe91c4 00000000 3fff4be4 00001387
3fff3a30: 0000734e 3fff031c 3fff4be4 4020352d
3fff3a40: 000001bb 3fff5eec 3fff031c 40202a12
3fff3a50: c6a79a95 3fff2b88 3fff3aa8 3fff3b00
3fff3a60: 000001bb 3fff031c 3ffe91c4 4020373d
3fff3a70: 3ffe97e8 c6a79a95 3ffe97e8 c6a79a95
3fff3a80: 3fff3b0c 3fff69c0 3fff02a8 40204ad9
3fff3a90: 3fff3ac0 00000001 3ffe9221 3fff3bac
3fff3aa0: 00000040 3fff3b00 3fff6104 0000000f
3fff3ab0: 00000000 0000003c 3fff3b00 40205aa3
3fff3ac0: 3fff2b64 00000111 3fff3b00 40205ad5
3fff3ad0: 00009220 3fff69c0 3fff3b00 40205b98
3fff3ae0: 3ffe9454 3fff69c0 3fff02a8 3fff3bac
3fff3af0: 00009220 3fff69c0 3fff02a8 40204d1c
3fff3b00: 3fff4e2c 0000003f 0000003c 3fff60ec
3fff3b10: 0000000f 00000000 3fff60a4 0000003f
3fff3b20: 0000003c 3fff69c0 00000000 40203cd4
3fff3b30: 00000010 3fff3ba0 3fff3ba0 3fff3bac
3fff3b40: 3fff02a8 3fff3ba0 3fff69c0 40204ded
3fff3b50: 3ffe9688 0000010b 3fff6ac4 00000400
3fff3b60: 3fff3bb8 00000000 3fff3ba0 40205b26
3fff3b70: 3ffe93e5 00000000 3fff02b4 00000000
3fff3b80: 3fff2b88 00000000 3fff02b4 40202164
3fff3b90: 00000000 3fff9838 3fff02cc 402059f8
3fff3ba0: 3fff4e14 0000000f 00000000 3fff687c
3fff3bb0: 0000012f 0000012a 3fff4dfc 0000000f
3fff3bc0: 00000009 3fff982c 0000012f 0000012a
3fff3bd0: 00000000 00000000 00000000 00000000
3fff3be0: 00000000 00000001 3ffe9221 4020639c
3fff3bf0: 00000000 3fff9c54 0000000f 00000005
3fff3c00: 3fff9c3c 0000000f 00000007 3fffbfdc
3fff3c10: 0000000f 00000009 3fff2b88 402056e9
3fff3c20: 00000001 00000001 3fff2b88 40205714
3fff3c30: 3fffdad0 3fff2a48 3fff2b88 3fff2c44
3fff3c40: 3fff02a4 3fff02a8 00000001 40202389
3fff3c50: 3fffdad0 00000000 3fff2c3c 40205fc0
3fff3c60: feefeffe feefeffe 3fff2c50 40100718
<<<stack<<<

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

ets Jan 8 2013,rst cause:4, boot mode:(1,6)

wdt reset

telegram_exeptionist.ino using HTTPSredirect and filling RAM:
I followed @witnessmenow 's suggestion in issue #52 to try HTTPSredirect instead of WiFiClientSecure. The same crash occurs.

Sketch uses 297021 bytes (28%) of program storage space. Maximum is 1044464 bytes.
Global variables use 51076 bytes (62%) of dynamic memory, leaving 30844 bytes for local variables. Maximum is 81920 bytes.
Uploading 301168 bytes

free heaps:
at start 6800 B
crashes after 5700 B

Serial output: [expand] GET Update Messages .... connected to server

{"ok":true,"result":[{"update_id":72978915,
"message":{"message_id":2047,"from":{"id":xxxxxxxx,"is_bot":false,"first_name":"Dejan","last_name":"Lauber","language_code":"en-US"},"chat":{"id":xxxxxxxx,"first_name":"Dejan","last_name":"Lauber","type":"private"},"date":1508501057,"text":"/status","entities":[{"offset":0,"length":7,"type":"bot_command"}]}}]}

incoming message length357
Creating DynamicJsonBuffer

got response
handleNewMessages
1
free heap: 5728 B

msg length: 298
sendMessage start: 72291
SEND Post Message

Exception (29):
epc1=0x4000e1b2 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000004 depc=0x00000000

ctx: cont
sp: 3fff3840 end: 3fff3e60 offset: 01a0

stack>>>
3fff39e0: 3fff2d4c 00000ea0 00000ea0 40223d13
3fff39f0: 00000208 3fff6b3c 3fff558c 40223eac
3fff3a00: 00000104 000072f0 00000e5e 3fff6b3c
3fff3a10: 00000040 00000082 3fff62ec 4022415e
3fff3a20: 00800000 00000080 00000000 00000041
3fff3a30: 00000040 0000003f 40206078 00000041
3fff3a40: 3fffb66c 3fff6b3c 3fff678c 3fff7204
3fff3a50: 3fffbb80 ffffffff 3fff6b3c 3fff6b3c
3fff3a60: 00000040 3fff6b3c 3fff62ec 3fff6b3c
3fff3a70: 00000040 3fff6b3c 3fff62ec 40224cba
3fff3a80: 3fff69bc 0000003f 6f7c523c 00000001
3fff3a90: 3fff6b3c 3fff62d4 3fff62ec 00000001
3fff3aa0: 00000010 3fff6b3c 0000000f 40224f15
3fff3ab0: 3fff678c 3fff69ec 0000000f 00000001
3fff3ac0: 00000001 3fff901c 3fff9159 40223d78
3fff3ad0: 00000100 3fff698c 3fff9157 00000000
3fff3ae0: 00000100 3fff698c 3fff9157 40225fa1
3fff3af0: 3fff3b20 00000000 000000d0 00000030
3fff3b00: ef97ef1c 9225d911 6acfa58b 00000004
3fff3b10: 00000004 3fff4fc4 3fff9151 402220ac
3fff3b20: 93e30203 bef4ae17 a98b23cf c47e77fe
3fff3b30: c7563393 db3e7a9b b440ae97 61e79a97
3fff3b40: 4b0c879a cf60bb60 4e4c5ee3 ef1ce98f
3fff3b50: 00000004 3fff9151 3fff4fc4 402224ad
3fff3b60: 74189ebf 4b3693ed bb2b5f8c 3fff50dc
3fff3b70: 00000000 00000004 00000004 402030c3
3fff3b80: 3fff9155 3fff9151 3fff63e4 00001200
3fff3b90: 000012a0 3fffa3bc 00000034 00000004
3fff3ba0: 3fff4fc4 3fff9151 00000004 00000004
3fff3bb0: 00000004 3fff9151 3fff4fc4 40221ea4
3fff3bc0: 00000000 3fff9151 3fff4fc4 4022224c
3fff3bd0: 00000010 3ffe936c 00000011 401004d8
3fff3be0: 3fff3c30 0000000e 00000010 00000000
3fff3bf0: 3fff68a4 3ffe936c 3fff4fc4 01000000
3fff3c00: 3fff3c30 3fff2bac 3fff4fc4 40221ffc
3fff3c10: 3ffe936c 00000000 3fff480c 00001387
3fff3c20: 00011b13 3fff2bac 3fff480c 40203535
3fff3c30: 000001bb 3fffbf04 3fff2bac 40202a1a
3fff3c40: c5a79a95 3fff2d70 3fff3c98 3fff3cf0
3fff3c50: 000001bb 3fff2bac 3ffe936c 40203745
3fff3c60: 3ffe9960 c5a79a95 3ffe9960 c5a79a95
3fff3c70: 3fff3cfc 3fff7238 3fff0428 40204bdd
3fff3c80: 3fff3cb0 00000001 3ffe922b 3fff3d9c
3fff3c90: 00000040 3fff3cf0 3fff6bc4 0000000f
3fff3ca0: 00000000 0000003c 3fff3cf0 40205ba7
3fff3cb0: 3fff2d4c 000003b4 3fff3cf0 40205bd9
3fff3cc0: 000139a7 3fff7238 3fff3cf0 40205c9c
3fff3cd0: 3ffe95d2 3fff7238 3fff0428 3fff3d9c
3fff3ce0: 000139a7 3fff7238 3fff0428 40204e20
3fff3cf0: 3fff6534 0000003f 0000003c 3fff657c
3fff3d00: 0000000f 00000000 3fff8fbc 0000003f
3fff3d10: 0000003c 3fff7238 00000000 40203dd8
3fff3d20: 00000010 3fff3d90 3fff3d90 3fff3d9c
3fff3d30: 3fff0428 3fff3d90 3fff7238 40204ef1
3fff3d40: 3ffe9800 00000480 3fff733c 00000400
3fff3d50: 3fff3da8 00000000 3fff3d90 40205c2a
3fff3d60: 3ffe9563 00000000 3fff0434 00000000
3fff3d70: 3fff2d70 00000000 3fff0434 40202164
3fff3d80: 00000000 3fff6a10 3fff044c 40205afc
3fff3d90: 3fff6bac 0000000f 00000000 3fff8e84
3fff3da0: 0000012f 0000012a 3fff6b94 0000000f
3fff3db0: 00000009 3fff6a04 0000012f 0000012a
3fff3dc0: 00000000 00000000 00000000 00000000
3fff3dd0: 00000000 00000001 3ffe922b 402064a0
3fff3de0: 00000000 3fff688c 0000000f 00000005
3fff3df0: 3fff4e64 0000000f 00000007 3fff67b4
3fff3e00: 0000000f 00000009 3fff2d70 402057ed
3fff3e10: 00000001 00000001 3fff2d70 40205818
3fff3e20: 3fffdad0 3fff2c30 3fff2d70 3fff2e2c
3fff3e30: 3fff0424 3fff0428 00000001 40202389
3fff3e40: 3fffdad0 00000000 3fff2e24 402060c4
3fff3e50: feefeffe feefeffe 3fff2e40 40100718
<<<stack<<<

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

ets Jan 8 2013,rst cause:4, boot mode:(1,6)

wdt reset

Notes

when the ESP crashes and dumps its stack, it waits 3s until the last line "wdt reset" happens.
i installed and tried the Arduino ESP8266/ESP32 Exception Stack Trace Decoder with an interesting first line. Apart from that i'm not sure what to look for..

The decoded output: [expand]

Exception 29: StoreProhibited: A store referenced a page mapped with an attribute that does not permit stores
Decoding 35 results
0x40223c0f: AES_cbc_decrypt at /Users/igrokhotkov/e/axtls/e1/crypto/aes.c line 337 (discriminator 2)
0x40223da8: trim at /Users/igrokhotkov/e/axtls/e1/crypto/bigint.c line 1203
0x4022405a: bi_terminate at /Users/igrokhotkov/e/axtls/e1/crypto/bigint.c line 119
0x40205f74: String::substring(unsigned int, unsigned int) const at /home/brian/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266/WString.cpp line 618
0x40224bb6: bi_set_mod at /Users/igrokhotkov/e/axtls/e1/crypto/bigint.c line 749
0x40224e11: precompute_slide_window at /Users/igrokhotkov/e/axtls/e1/crypto/bigint.c line 1327
: (inlined by) bi_mod_power at /Users/igrokhotkov/e/axtls/e1/crypto/bigint.c line 1372
0x40223c74: htonl at /Users/igrokhotkov/e/axtls/e1/ssl/os_port.h line 209 (discriminator 2)
: (inlined by) AES_cbc_decrypt at /Users/igrokhotkov/e/axtls/e1/crypto/aes.c line 347 (discriminator 2)
0x40225e9d: RSA_print at /Users/igrokhotkov/e/axtls/e1/crypto/rsa.c line 230
0x40221fa8: verify_digest at /Users/igrokhotkov/e/axtls/e1/ssl/tls1.c line 610
: (inlined by) basic_read at /Users/igrokhotkov/e/axtls/e1/ssl/tls1.c line 1336
0x402223a9: process_server_hello at /Users/igrokhotkov/e/axtls/e1/ssl/tls1_clnt.c line 293
: (inlined by) do_clnt_handshake at /Users/igrokhotkov/e/axtls/e1/ssl/tls1_clnt.c line 89
0x402030bb: ClientContext::_consume(unsigned int) at /home/brian/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266WiFi/src/WiFiClientSecure.cpp line 517
: (inlined by) ClientContext::read(char*, unsigned int) at /home/brian/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266WiFi/src/include/ClientContext.h line 175
0x40221da0: verify_digest at /Users/igrokhotkov/e/axtls/e1/ssl/tls1.c line 709
: (inlined by) basic_read at /Users/igrokhotkov/e/axtls/e1/ssl/tls1.c line 1336
0x40222148: send_client_hello at /Users/igrokhotkov/e/axtls/e1/ssl/tls1_clnt.c line 200
: (inlined by) do_client_connect at /Users/igrokhotkov/e/axtls/e1/ssl/tls1_clnt.c line 153
0x401004d8: malloc at /home/brian/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266/umm_malloc/umm_malloc.c line 1664
0x40221ef8: basic_read at /Users/igrokhotkov/e/axtls/e1/ssl/tls1.c line 1381
0x4020352d: WiFiClientSecure::_connectSSL(char const*) at /home/brian/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266WiFi/src/WiFiClientSecure.cpp line 517
0x40202a12: WiFiClient::connect(IPAddress, unsigned short) at /home/brian/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266WiFi/src/WiFiClient.cpp line 327
0x4020373d: WiFiClientSecure::connect(char const*, unsigned short) at /home/brian/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/libraries/ESP8266WiFi/src/WiFiClientSecure.cpp line 265
0x40204ad9: ArduinoJson::Internals::JsonSerializer > >::serialize(ArduinoJson::JsonVariant const&, ArduinoJson::Internals::JsonWriter >&) at /home/brian/share/arduino/libraries/UniversalTelegramBot/src/UniversalTelegramBot.cpp line 283
0x40205aa3: UpdaterClass::UpdaterClass() at /home/brian/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266/Updater.cpp line 373
0x40205ad5: _GLOBAL__sub_D__ZN12UpdaterClassC2Ev at /home/brian/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266/Updater.cpp line 375
0x40205b98: String::reserve(unsigned int) at /home/brian/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266/WString.cpp line 589
0x40204d1c: UniversalTelegramBot::sendPostToTelegram(String, ArduinoJson::JsonObject&) at /home/brian/share/arduino/libraries/UniversalTelegramBot/src/UniversalTelegramBot.cpp line 283
0x40203cd4: ArduinoJson::JsonObject& ArduinoJson::JsonObject::createNestedObject_impl (char const*) at /home/brian/share/arduino/libraries/UniversalTelegramBot/src/UniversalTelegramBot.cpp line 283
0x40204ded: StringSumHelper at /home/brian/share/arduino/libraries/UniversalTelegramBot/src/UniversalTelegramBot.cpp line 283
: (inlined by) UniversalTelegramBot::sendPostMessage(ArduinoJson::JsonObject&) at /home/brian/share/arduino/libraries/UniversalTelegramBot/src/UniversalTelegramBot.cpp line 462
0x40205b26: String::init() at /home/brian/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266/WString.cpp line 589
: (inlined by) String::invalidate() at /home/brian/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266/WString.cpp line 135
0x40202164: handleNewMessages(int) at /home/brian/share/arduino/telegram_exceptionist/telegram_exceptionist.ino line 65
0x402059f8: Print::println(unsigned int, int) at /home/brian/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266/Print.cpp line 76
0x4020639c: ArduinoJson::Internals::JsonBufferAllocated::operator new(unsigned int, ArduinoJson::JsonBuffer*) at /home/brian/share/arduino/libraries/ArduinoJson/src/ArduinoJson/Data/JsonBufferAllocated.hpp line 19
0x402056e9: HardwareSerial::begin(unsigned long, SerialConfig, SerialMode, unsigned char) at /home/brian/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266/HardwareSerial.cpp line 193
0x40205714: _GLOBAL__sub_I__ZN14HardwareSerialC2Ei at /home/brian/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266/HardwareSerial.cpp line 197
0x40202389: loop at /home/brian/share/arduino/telegram_exceptionist/telegram_exceptionist.ino line 111
0x40205fc0: operator delete(void*) at /home/brian/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266/abi.cpp line 57
0x40100718: cont_norm at /home/brian/.arduino15/packages/esp8266/hardware/esp8266/2.3.0/cores/esp8266/cont.S line 109

Build options:

Board: "Wemos D1 R2 & mini"
CPU Frequency: "80MHz"
Flash Size: "4M (3M SPIFFS)"
Upload Speed: "921600"

Other things i've tried:

Different USB Power adapter. no effect.
I got the same crash on another ESP8266, with different power adapter and cable.
470uF Capacitor is in place on the 5V rail.
Added a 2200uF Capacitor to the 3.3V rail. no effect.
changed to 1M flash size. no effect.
changed clock to 160mhz. no effect.

If anyone has a clue on how to resolve this issue, or an idea on what to try next, let me know.

Best regards,
dejan lauber

PS: The ESP8266 and this telegram library blew my mind! Awesome work!

@efeuentertainment
Copy link
Author

efeuentertainment commented Oct 20, 2017

i was thinking if that space is reserved for the wifi stack and causes the ecxeption.
i found this issue: How to increase heap size by reducing dynamic memory size?
where jjssoftware writes:

where stack grows/shrinks at one end of this free RAM and heap grows/shrinks at the other end. If things get so bad where stack and heap meet/overlap due to RAM depletion, you get an exception and a reboot.

i wonder if it's possible to check the wifi stack size...?

apart from that, i checked the addresses "3FFF3***" from the serial monitor and they seem to be well within range of the user RAM "dram1". can't be an out-of-range issue then.

3FFE8000h-3FFFC000h dram0 14000h RAM RW User data RAM. Available to applications.

source: Memory Map

@efeuentertainment
Copy link
Author

UPDATE:
Stack size is by default 4KB. its defined in <cont.h>.
this link includes an example on getting free stack

when i run that everything looks normal. stack size: 4KB and free stack around 1000B-1400B.

but if the free heap size gets lower than 4KB it would cause a crash. (i assume)
i printed the free heaps on various places within UniversalTelegramBot.cpp:

a successfull sendMessage free heap size goes:

free heap: 6240 B
SEND Post Message
free heap: 5008 B
free heap: 4888 B
sending header  
free heap: 4704 B  

which is quite close to 4KB..
and a crash goes:

free heap: 6056 B
SEND Post Message
free heap: 4824 B
free heap: 4704 B
sending header

and then it crashes in UniversalTelegramBot.cpp in function "UniversalTelegramBot::sendPostToTelegram" on exactly this line (est. 74):

	// Connect with api.telegram.org
	if (client->connect(HOST, SSL_PORT)) {

which means its quite certainly not a bug in UniversalTelegramBot, but rather somewhere else.

i did find another thread with a very similar issue in the esp8266 core
where OrakelKSL writes:

Every time client receives a request, the heap goes down and does NOT recover!
...ir recovers, but it takes MINUTES!
... and if you keep f5 pressed exception 28 or 29 occours.

and devyte's response:

The fact heap would recover hints at sockets being kept in TIME_WAIT state, probably for a the typical 2 minutes. That is per RFC (I forget which), although for tiny embedded systems implementations usually don't hold to it.
Still, with latest git I don't currently see the issue when refreshing a webpage.

if it's the same issue i see little i can do about it...
i can either try to find where those sockets get put in TIME_WAIT, and change it to delete unused sockets,
or i do a dirty fix where i only update the UniversalTelegramBot if there is more than ... eg. 8KB free Heaps..

someone another idea?

@gon0
Copy link

gon0 commented Nov 3, 2017

If it helps, I also got Exception(29) with the telegram bot, shown here: Aypac/Arduino-TR-064-SOAP-Library#2 (comment)

@witnessmenow
Copy link
Owner

@gon0 making a https connection uses 18k of heap memory, I presume you are out of memory by the time out get down to the telegram line

@gon0
Copy link

gon0 commented Nov 3, 2017

@witnessmenow Yeah, seems like that.
However, you did very good work with the library! In my code, the Telegram-Bot is the main function, so I will leave out the https://github.com/Aypac/Arduino-TR-064-SOAP-Library library until there is a solution to use both.

@efeuentertainment
Copy link
Author

UPDATE
i found a workaround to clean up sockets kept in TIME_WAIT.
issue: memory leak 184 bytes at a time #1923
where psy0rz commented:

since that pull request isnt merged yet and it will take some time to reach upstream, here's a workaround that actually cleanups the TIME_WAITS and frees memory:

// do this before including other stuff like wificlient.h
#include "lwip/tcp_impl.h"

void tcpCleanup()
{
  while(tcp_tw_pcbs!=NULL)
  {
    tcp_abort(tcp_tw_pcbs);
  }
}

update: This goes in your main file. Call tcpCleanup regulary, for example from your loop() function.

it sounds as if there's a pull request with a better solution which will fix this issue in the future.
anyway, my sketch works for now!

in my case the ESP crashed when free heap went under ~4.5KB. but that's not always the same it seems.
as OrakelKSL wrote:

Heap goes rapidly down to a few bytes [...]

which implies his heap went down to near zero...

@morrowwm
Copy link

morrowwm commented Feb 6, 2018

FWIW, I updated my Arduino IDE to use the latest ESP8266 library following instructions here: https://github.com/esp8266/Arduino#using-git-version

Now my sloppy mashup of the excellent Universal-Arduino-Telegram-Bot and a webserver based on this other excellent code: https://tttapa.github.io/ESP8266/Chap16%20-%20Data%20Logging.html
is running fine now.

Previously, it crashed repeatedly on client->connect() calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants