Method GET Fails After Several Requests

Posted on
  • Hello Everyone,

    I was in the process of adapting some code from

    http://forum.espruino.com/conversations/­301910/

    to test out writing using fs.pipe() on Espruino/8266 to the new file system enhancement. And I came across a strange problem -- at least for me. I may not be coding it right, and or it maybe a known problem I'm not aware of, though its happened on two boards now. So I thought I'd share it with you to get your feedback.

    Basically with this code:

    var http = require("http");
    var wifi=require("Wifi");
    var esp8266=require("ESP8266"); // for esp8266.printLog()
    
    var page = "<html><head><title>test pipe</title><script src=\"https://ajax.googleapis.com/ajax/l­ibs/jquery/2.1.4/jquery.min.js\"></scrip­t></head><body><button id=\"getPage\">Reload Page</button><textarea rows=\"5\" cols=\"100\" id=\"Htmlfile\"></textarea></body><scrip­t>$(\"#getPage\").click(function(){$.get­(\"/getPage\",function(data){$(\"#Htmlfi­le\").val(data);})});</script></html>";
    
    function handleGet(req,res) {
      console.log("handleGet() -> req");
      console.log(req);
      
      if (req.url=="/getPage" || req.url== "/") {
        
        res.writeHead(200,{"Content-Type":"text/­html"});
        res.end(page);
        
      } else {
        res.writeHead(404);
        res.end("404 Not Found");
       }
    }
    
    function onPageRequest(req,res) {
        console.log("onPageRequest() -> req");
        console.log(req);
     
        if(req.method == 'GET') 
           handleGet(req,res);
        //else req.connection.destroy();
      
    }
    
    E.on("init", function () {
          wifi.connect(wifi.getDetails().ssid, {password: wifi.getDetails().password}, function(err) {
              if (err) {
                console.log("Connection error: "+err);
                return;
              }
              console.log("Connected!");
              console.log(wifi.getIP());
              server = http.createServer(onPageRequest);
     server.listen(80);
            });
    });
    
    

    After 7 to 9 sequential requests spaced by a few seconds (ie: pressing the button in the example), I would get a failure status in Chrome's developer tools and of course no data would be returned, as shown in the included screenshot. I was doing this manually (pressing the button), and made sure to wait until the requests had completed in the browser. If I pressed RELOAD in the browser (not the button in the html page), the 8266 would also fail to send anything, though interestingly it was still seeing the requests, as they would be "consoled" properly. Just nothing was returned.

    If I waited 5 minutes or so, I could do it again another 7-9 times then it would fail. I suppose it could be a fun game if I had been drinking, but this was not the case. :) And I suppose the more practically minded of you would tell me just not to press it so often :P

    By including this line:

    memoryInterval=setInterval(function() {console.log(process.memory());},1000);
    

    I could see easily that free memory almost always stayed between around 1400-1513.

    { "free": 1513, "usage": 187, "total": 1700, "history": 105 }
    

    After each failure esp8266.printLog shows something like this:

    >esp8266.printLog()
    70, heap: 2592
    793824> espconn_tcp_delete 1, 80
    794217> > jshReset
    794218> < jshReset
    807169> E:M 416
    816078> E:M 416
    817512> E:M 416
    818945> E:M 416
    820081> E:M 416
    840002> Thu Jan 01 00:14:00 1970, heap: 1376
    900002> Thu Jan 01 00:15:00 1970, heap: 1376
    =undefined
    

    and another time:

    >esp8266.printLog()
    2> E:M 656
     43382> E:M 656
     43382> E:M 656
     43382> E:M 656
     43382> E:M 656
     43382> E:M 656
     43383> E:M 656
     45902> E:M 656
     45902> E:M 656
     45902> E:M 656
     45903> E:M 656
     45903> E:M 656
     45903> E:M 656
     45903> E:M 656
     45903> E:M 656
     45903> E:M 656
    =undefined
    

    So my first question is have I done anything visible wrong in my code? And then if not, can anyone replicate this? Or have any other ideas?

    For what its worth, I have a few ESP8266s talking to each other (so 2-4) in another project, so we could imagine that this scenario would occur if some of the units made some requests quickly in sequence. So its something that could be seen "live" somewhere outside of this example, at least for me.

    Thanks :)
    -hfc

    BTW: I'm using 1v91.741.


    1 Attachment

    • Screenshot 2017-04-10 at 21.08.30.png
  • I think you are running out of heap - E:M 656 I seem to recall is a failed memory allocation.

    I ran your code on the ESP32. I clicked the button on the browser as quick as could - no fails... and it responded within 124-197 ms for the request.

  • Some of the builds reduce the jsvars from 1700 to 1600 to give more heap - perhaps you should try that.

  • Ah, OK thanks -- I will do that :)

  • Hi @hungryforcodes,

    please check this https://github.com/espruino/Espruino/iss­ues/809#issuecomment-249232460

    You are running out of heap memory.

  • @MaBe Hello! This was a great link and very informative, thank you :) I'll see what I can do on my side to mitigate the effect. :)

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

Method GET Fails After Several Requests

Posted by Avatar for hungryforcodes @hungryforcodes

Actions