Bug: JSON.parse(undefined) reboots the ESP8266

Posted on
  • I had similar code in onInit():

    JSON.parse(undefined)
    

    Instantly after save() my ESP8266 went into a reset loop.
    Is it a specific ESP8266 bug, or is it a general bug?

  • (argh, forum ate a longer response, so just a short answer now:)

    Works on my machine :)

    My machine now includes ESP8266, ESP32, nRF52. JSON.parse(undefined) doesn't cause a reboot.
    Most likely some other code either in the init or in the "regular" part of your program that causes a reboot. Some other error, or stuck in an infinite loop waiting for something that never happens because init failed (and rebooted by watchdog).

    Even tried this on an ESP8266 (with save to flash)

    function onInit(){
      print('@onInit');
      //throw new Error('b00');
      JSON.parse(undefined);
    }
    

    Output:

     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v01 (c) 2018 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    Flash map 4MB:1024/1024, manuf 0xef chip 0x4016
    >
    Running onInit()...
    @onInit
    Uncaught SyntaxError: Expecting a valid value, got undefined
     at line 1 col 10
    undefined
             ^
    in function called from system
    

    And works just fine.

  • @maze1980, it is very difficult to respond to conversations with such titles and with no details / context of the code and even less of the hardware...

    Main issue with most platforms - and especially non-Espruino boards - is that it matters what the setup is and happened before you issue save(), for example: is there level 0 code that creates things other than code objects... It is always good practice to have nothing set - such as pin modes - or activated - such as timeouts, intervals, connects, etc - except in onInit().

    A tanking - rebooting -ESP866 has usually two root causes:

    • power stability
    • cycle hogs in the application code
  • I flashed the device with "espruino_2v04_esp8266_4mb_combined_4096­.bin".
    The first command I typed in after connecting is "JSON.parse(undefined)" (no Wifi/no other devices connected).
    The ESP8266 is USB-powered, and this works fine with Wifi and a few Neopixels normally (when not using this code).

    >    JSON.parse(undefined)
     ets Jan  8 2013,rst cause:2, boot mode:(3,7)
    load 0x40100000, len 2408, room 16
    tail 8
    chksum 0xe5
    load 0x3ffe8000, len 776, room 0
    tail 8
    chksum 0x84
    load 0x3ffe8310, len 632, room 0
    tail 8
    chksum 0xd8
    csum 0xd8
    2nd boot version : 1.6
      SPI Speed      : 80MHz
      SPI Mode       : DIO
      SPI Flash Size & Map: 32Mbit(1024KB+1024KB)
    jump to run user1 @ 1000
    ãänì{ûo|ällld`c<sd'à'ãd`ãrÛl$þ
     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v04 (c) 2019 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    Flash map 4MB:1024/1024, manuf 0xc8 chip 0x4016
    >process.memory();
    ={ free: 1575, usage: 25, total: 1600, history: 0,
      gc: 0, gctime: 1.901 }
    >process.env;
    ={
      VERSION: "2v04",
      GIT_COMMIT: "3956264e",
      BOARD: "ESP8266_4MB",
      FLASH: 0, RAM: 81920,
      SERIAL: "a020a61c-4bc6",
      CONSOLE: "Serial1",
      MODULES: "Flash,Storage,hea" ... "r,crypto,neopixel",
      EXPTR: 1073643636 }
    >console.log(process.env.MODULES);
    Flash,Storage,heatshrink,net,dgram,http,­NetworkJS,Wifi,ESP8266,TelnetServer,cryp­to,neopixel
    =undefined
    >require("ESP8266").getState();
    ={
      sdkVersion: "2.2.1(6ab97e9)",
      cpuFrequency: 160, freeHeap: 15464, maxCon: 10,
      flashMap: "4MB:1024/1024",
      flashKB: 4096,
      flashChip: "0xc8 0x4016"
     }
    >require("Flash").getFree();
    =[
      { addr: 2097152, length: 1048576 },
      { addr: 3145728, length: 262144 },
      { addr: 3407872, length: 262144 },
      { addr: 3670016, length: 262144 },
      { addr: 3932160, length: 241664 }
     ]
    >require("Storage").getFree();
    =16
    > 
    
  • Ok, looks like the 2v04 "_4mb" build crashes.
    Same ESP flashed with different builds:

      VERSION: "2v01",
      GIT_COMMIT: "748a4d3",
      BOARD: "ESP8266_4MB",
    

    -> works

      VERSION: "2v03",
      GIT_COMMIT: "e77d74f6",
      BOARD: "ESP8266_4MB"
    

    -> works

    VERSION: "2v04",
      GIT_COMMIT: "3956264e",
      BOARD: "ESP8266_4MB"
    

    -> crashes

      VERSION: "2v04",
      GIT_COMMIT: "3956264e",
      BOARD: "ESP8266_BOARD"
    

    -> works

  • Interesting... and even the board matters... (assuming the commit is for the built code..., which is the same for the latter two).

  • I know that this conversation is in the 8266 realm, but - for curiosity - I just tested on a PICO, and that is what I get:

     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v04 (c) 2019 G.Williams
    Found PICO_R1_3, 2v04
    >
    Connected to /dev/cu.usbmodem14111
    >JSON.parse()
    Uncaught SyntaxError: Expecting a valid value, got undefined
     at line 1 col 10
    undefined
             ^
    > JSON.parse(undefined)
    Uncaught SyntaxError: Expecting a valid value, got undefined
     at line 1 col 7
    undefined
          ^
    >JSON.parse("")
    Uncaught SyntaxError: Expecting a valid value, got EOF
     at line 1 col 1
    ^
    >JSON.parse("{}");
    ={  }
    > 
    

    ...comparable what the (Chrome) browser JS Engine throws at me: see attached screen shot (...but is for sure not a crash of the system / HW).

    Workaround is asking if undefined before parsing... (try catch does not help)...


    1 Attachment

    • JSON_parse_sth.png
  • I've now included a test for undefined in my code,it is fixed for me.
    Since the source code is the same for all systems I wonder what's going on. Might be a job for a long winter nights to dig into it.

    Another bug is this one (directly after erase and flash):

    >require("Storage").getFree();
    =16
    >require("Storage").eraseAll();
    =undefined
    >require("Storage").getFree();
    =196608
    

    Do you have the same on the ESP8266 4MB build? It doesn't affect the JSON.parse.

  • @MaBe could this be related to the compile flags used in the 4MB ESP8266 build?

  • Just checked - Yes - reopened #1679 to fix it.

  • Thanks! I've just applied that fix so cutting edge builds should now work ok

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Bug: JSON.parse(undefined) reboots the ESP8266

Posted by Avatar for maze1980 @maze1980

Actions