fphttpclient returns 0 HTTP response or fails after long period. lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- jdp · 2020-12-14 08:39 Hi all. I wrote a program that will send telemetry data to a server every 20 seconds. It all works well. Then I wrote some code to buffer the data on the client-side should the server be slow or the internet is off. This works most of the time. I set a timeout value of 15 seconds. If the server does not respond and I have a timeout then I write the data to the buffer. The problem I have is this. Sometimes the client will get into a mode where it does not block and wait for the timeout. It will fail on the call and the response code will be 0 not 500 like on a timeout. I can not recover from this state even if the server is back online. I have to kill the program and start it again. I am also not sure what exception to catch for a timeout on its own. The platform is Raspberry Pi / Linux on ARM. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i        var       HTTPSender       :TFPHttpClient;       begin             try            HTTPSender := TFPHttpClient.Create(nil);              HTTPSender.ConnectTimeout := 15000;              HTTPSender.IOTimeout    := 15000;              HTTPSender.Post(UrlString + params, Response);                           HTTPGetResult := True;          except                      on E:Exception do begin                 HTTPGetResult := False;              end;             end;            if(HTTPSender.ResponseStatusCode = 200) then begin               //All OK             end             else             begin             //Write to buffer             end;                    end; end;   I can reproduce the error by blocking the ip on my firewall to simulate the server being offline. After a while, the client will get into this state. Not sure why or how to get around it. ---------------------------------------------------------------------- Thaddy · 2020-12-14 17:02 Reverse the logic of the last statement and log the status code would be more helpful. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i if(HTTPSender.ResponseStatusCode <> 200) then   //All wrong  writeln(HTTPSender.ResponseStatusCode);else  begin   // All OK. start processing.....  end; This will obtain the exact status code and we can help you better. Programmers should think with the above in mind. ---------------------------------------------------------------------- jdp · 2020-12-15 08:10 @thaddy the status code is 0 as stated above. There is no status code of 0 in HTTP. The code above is just a sample of the logic. Is this a bug then? ---------------------------------------------------------------------- rvk · 2020-12-15 08:23 The HTTPSender.ResponseStatusCode didn't get a value when there is an exception. So you should move the check inside the try except or add HTTPGetResult to the check. Also... Where is the HTTPSender.free? Besides that, 500 is an Internal Server Error. When the ip is unreachable I don't think you'll get a 500 error. Your HTTPGetResult will just be false. HTTPSender.ResponseStatusCode will only get a code when there is a response from the server. Which is inpossible if ip is unreachable. ---------------------------------------------------------------------- jdp · 2020-12-15 10:52 Hi Rik. The free is there in the actual code, don't worry, you taught me well lol. This was just to explain the logic and what I am seeing. It makes sense that the return code will not be 500 if the link is down. But why does it get into a state that it will no longer wait or block for the 15 seconds and return immediately when the http call is made and then never again connect to the server even if the internet comes back? I create a new HTTP object on every call. Also, how do I catch the timeout error with this library? ---------------------------------------------------------------------- rvk · 2020-12-15 11:20 Does the first timeout indeed happen after 15 seconds? If so... how many times do you call the thread? What version of FPC are you using? QuoteSometimes the client will get into a mode where it does not block and wait for the timeout.If that happens, then there must be some reason for that. What is HTTPGetResult at that point? If it is false then you need to get the exception. Now you only do HTTPGetResult := False; So you are 'eating' the exception. You need to log the exception somehow to know what it is. ---------------------------------------------------------------------- jdp · 2020-12-15 19:06 Yes the timeout is 15 seconds all the time until it is not and does not block at all anymore and just returns all the time. I have 2 threads. 1 is the active one posting the data and the other one sends the data from the buffer. The buffer one is using Synape to send HTTP data and there I am getting the response code 500 if I block the server on the firewall but I get 0 when using the fphttpclient object. What is the best way to capture the exception? Do I do a Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  E:exeption begin error_msg := e.message end; and log the error_msg ? ---------------------------------------------------------------------- rvk · 2020-12-15 19:10 Quote from: jdp on December 15, 2020, 08:06:09 pmWhat is the best way to capture the exception? Do I do a Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  E:exeption begin error_msg := e.message end;and log the error_msg ? Yes ( on E: Exception do ) And append it to a file or something (including timestamp). ---------------------------------------------------------------------- jdp · 2020-12-15 19:53 Mmmm I am getting an Access Violation back from the Post / Get Methods of the TFPHttpClient when I post valid data and it returns with response code 200. Why would it throw a access vialation exception and return with valid return code. ---------------------------------------------------------------------- rvk · 2020-12-15 20:16 Maybe an exception due to openssl? https://forum.lazarus.freepascal.org/index.php?topic=45962.0 ---------------------------------------------------------------------- jdp · 2020-12-15 20:24 I am using SSL but it should be fixed in my version of Lazarus according to that post. Version 2.1.0 Date 2019-08-15 FPC 3.3.1 SVN Revision: 61697M Should I maybe try and upgrade my Lazarus again? I am so scared things stop working if I do that lol. ---------------------------------------------------------------------- jdp · 2020-12-15 20:25 Or maybe I should use the Synapse library instead and see if I have the same issues. ---------------------------------------------------------------------- rvk · 2020-12-15 20:31 Quote from: jdp on December 15, 2020, 09:25:52 pmOr maybe I should use the Synapse library instead and see if I have the same issues. Yes, you could try that. You could also try to create a small testproject to see if you can reproduce the error. If you can maybe it can be fixed (if it is a bug).