Strange behaviour around fs.statSync

Posted on
  • running this prints "..." on screen, so variable startAt is corrupted

    function rtc2Date(dt){
      var t,d;
      d = dt.split(" ")[0]; t = dt.split(" ")[1];
      d = d.split("/"); t = t.split(":");
      return new Date(d[2] + 2000,d[1],d[0],t[0],t[1],t[2]);
    }
    var fs = require("fs");
    var data = JSON.parse(fs.readFile("Juergen1_ctrl.js­on"));
    var startAt = rtc2Date(data.options.startAt);
    //console.log(startAt);
    var entrys = fs.statSync("Juergen1_data.bin").size / 20;
    console.log(startAt);
    

    uncommented first console.log and it works fine:

    function rtc2Date(dt){
      var t,d;
      d = dt.split(" ")[0]; t = dt.split(" ")[1];
      d = d.split("/"); t = t.split(":");
      return new Date(d[2] + 2000,d[1],d[0],t[0],t[1],t[2]);
    }
    var fs = require("fs");
    var data = JSON.parse(fs.readFile("Juergen1_ctrl.js­on"));
    var startAt = rtc2Date(data.options.startAt);
    console.log(startAt);
    var entrys = fs.statSync("Juergen1_data.bin").size / 20;
    console.log(startAt);
    

    Also had problems sometimes by using fs.statSync from terminal window. Connection to com-port was lost, and I had to reset and restart everything.
    IMHO, this could be a problem around memory management for fs.statSync.
    Using 1v80 on Espruino Board
    Data returned from readFile is this:

    {
      "tags": {
        "MQ2": "Methane, Butane, LPG, smoke",
        "MQ4": "Methane, CNG Gas",
        "MQ7": "Carbone Monoxide",
        "temp": "Temp",
        "rh": "Humidity"
       },
      "options": { "interval": 60000, "groupLength": 10,
        "startAt": "16/06/15 22:34:23"
       }
     }
    
  • Thanks - that looks interesting. I'll have a look and see if I can reproduce it.

    I can't see anything immediately wrong in fs.statSync - it's possible it's actually an issue with Date.

  • Just tried to reproduce syncStat problem in a different way. Typed commands in terminal window:

    • reset()
    • require("fs").readdir()
    • require("fs").syncStat("Juergen1_data.bi­n")
      There is no response from Espruino, so I
    • closed connection
    • clicked connect button and got an empty list, no COM port available anymore
    • wrote these lines and about a minute later, a Com port is available
    • tried to connect and got Connection failed
    • unplugged Espruino Board from USB
    • plugged Espruino Board
    • now connect worked fine
    • syncStat worked fine
      Hmmm, whats that ? So tried again:
    • reset()
    • require("fs").readdir()
    • require("fs").statSync("Juergen1_data.bi­n")
    • Espruino hangs again
  • What's in "Juergen1_data.bin"? I don't think you ever said.

    I just tried this on an Espruino board with 1v79 from the website and it seems to work, so maybe my file Juergen1_data.bin is different somehow.

    Having said that, I found that if readFile failed and sent undefined to JSON.parse it could cause you pretty major problems.

  • Nope, now it's breaking!

    As you say, doing a readdir just before is enough to kill it. But readdir followed by a readFile is fine.

    >require("fs").readFile("Juergen1_data.b­in")
    ="Hello World"
    >require("fs").statSync("Juergen1_data.b­in")
    ={ "size": 11, "dir": false,
      "mtime": { "ms": 1328054400000 }
     }
    >require("fs").readdir()
    =[
      "Juergen.json",
      "Juergen1_ctrl.json",
      "Juerget1_data.bin",
      "Juergen1_data.bin",
      "Juerget1_data.bin~"
     ]
    >require("fs").statSync("Juergen1_data.b­in") 
    

    Looks like this is something else dodgy in the SPI/Filesystem interface code :(

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

Strange behaviour around fs.statSync

Posted by Avatar for JumJum @JumJum

Actions