[Photon] Fault in 'socket_connect' after 2 requests

Greetings,

I am having problems with sending http requests with the photon.
I received a batch of photons and just finished porting my software form the core to the photon. The software periodically collects data from sensors and submits it to the server. It is working properly on the core, however on the photon it only manages to do 2 requests after which a hard fault takes place during TCPClient::connect. Tracing the error lead me to the function ‘socket_connect’, during which the fault happens. I have turned off the interrupts and all other unimportant pieces of code in my software but the problem still persists. Firmware is operating in AUTOMATIC mode.
Any ideas whether I could have programmed something that causes this or might this be some kind of a bug?

1 Like

What version of system firmware are you using? If you could share a minimum version of your application code that has the problem, that will help the firmware team investigate the issue. Thanks :smile:

I am using the “latest” branch from https://github.com/spark/firmware/tree/latest which I believe is currently versioned at 0.4.3.

A simplified cut-out from the software:

 namespace WebApi {
   struct HTTPRequest {
     String method = "GET";
     String endpoint = "";
     String* body = nullptr;
     api_callback callback = defaultHandlerCallback;

     ~HTTPRequest() {
       clean();
     }

     void clean() {
       method = "GET";
       endpoint = "";
       if(body != nullptr) {
         delete body;
         body = nullptr;
       }
     }

     void init() {
       clean();
       body = new String("");
       endpoint = "";
     }

     String* getBody();
   };

   struct HTTPResponse {
     HTTPRequest* request;

     uint16_t code;
     uint32_t content_length;
     bool h_flag = false;
     bool header_received = false;

     String* header = nullptr;
     String* body = nullptr;

     ~HTTPResponse() {
       clean();
     }

     void clean() {
       h_flag = false;
       header_received = false;
       content_length = 0;
       code = 0;

       if(body != nullptr) {
         delete body;
         body = nullptr;
       }

       if(header != nullptr) {
         delete header;
         header = nullptr;
       }
     }

     void init() {
       clean();
       header = new String("");
       body = new String("");
     }

     String* getBody();
     String* getHeader();
   };

   TCPClient client;
   static HTTPResponse res;

   int32_t openConnection(HTTPRequest* req) {
     if(client.connected()) {
       Logger::println("[WebApi]openConnection call error: TCPClient already in use or buffer was not p
 #endifrocessed. Closing previous connection.");
       client.stop();
     }

     /**
      *  Here the hard fault occurs in client.connect during the third request.
      */
     if(client.connect(api_server, HTTP_PORT)) {
       responseTimeout = millis() + responseTimeoutPeriod + 2000;
       STATE = SEND_HEADER;
     } else {
       Logger::println("[WebApi]Connection failed!");
       Exception exc;
       exc.msg = "[WebApi::openConnection]Server connection failed";
       logException(exc);
       STATE = ERR;
       return -1;
     }
     return 0;
   }

   int32_t sendRequest(HTTPRequest req) {
     res.init();

     if(openConnection(req) < 0) goto ERR;
     if(sendHeader(req->endpoint, req->getBody()->length()) < 0) goto ERR;
     if(streamData(req->getBody()) < 0) goto ERR;
     if(endDataStream() < 0) goto ERR;
     readFullResponse();

     res.clear();
     return 0;
ERR:
    err_reset_cnt++;
    return 1;
 }

namespace MyApplication() {
   void dataProcess() {
     HTTPRequest req;
     /.../
       //Data compilation. At most one data chunk takes up to ~1KB of memory.
     /.../
     WebApi::sendRequest(req);
   }

   void process() {
     if(dataProcTimer.isTime()) {
       dataProcess();
       dataProcTimer.setTime();
     }
   }
}

 void loop() {
   MyApplication::process();
 }

Is this enough or should I create a fully working source code tarball which contains the essential modules?

Thank you for your fast response :smiley:

Does this require any libraries?

If you can help reduce the problem to the smallest amount of code that still shows the issue that would help very much.

Thanks :smile:
mat.

@mdma, this may be related to the issue I posted regarding name resolution (which I flagged as a possible DNS issue). It works sometimes and sometimes not. When it fails, it does into SOS and resets.

I see. I indeed had problems at first in getting the first and second requests working. However since now the first and second requests are constantly working I can only deduce that something in my code was the cause for the first failures. It is so awkward that it is always the third request which fails.

@Melx, I find that the call will sometimes work for a while and then fail several times in a row (causing resets) and then start working again. Sometimes the first call works, other times not.