NETDB ResolveName unstable on Debian? Affects Indy - causes intermittent failure lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- jl · 2011-06-05 17:51 Hi, I've been trying to use Indy indy10.2.0.3 on Debian and after sometime I noticed intermittent EIdResolveError, which doesn't happen on the Windows host - I'm running Debian as Guest on Virtualbox.  This problem affects all Indy components that need to resolve dns. After debugging Indy, I realized that the problem is with NETDB's ResolveName.  Indy calls ResolveName inside IdStackUnix.pas - see below.  LRetVal returns -1 or 0 (can't remember which) intermittently even though network is up all the time. The intermittent failure aside, another problem is if network is down before the program calls ResolveName, every subsequent calls of ResolveName will fail - NetAddr will always return 0.0.0.0.  Is there something which I need to do to "free" ResolveName? I've also created a test program, details below.  Any thoughts will be appreciated.  Thanks. function TIdStackUnix.HostByName(const AHostName: string;  const AIPVersion: TIdIPVersion = ID_DEFAULT_IP_VERSION): string; var  LI4 : array of THostAddr;  LH4 : THostEntry;  LI6 : array of THostAddr6;  LRetVal : Integer; begin  case AIPVersion of    ID_IPv4 :    begin      if GetHostByName(AHostName,LH4) then      begin        Result := HostAddrToStr( LH4.Addr );        exit;      end      else      begin        SetLength(LI4, 10);        LRetVal := ResolveName(AHostName, LI4);        if LRetVal < 1 then begin          raise EIdResolveError.CreateFmt(RSResolveError, [AHostName, 'Error', LRetVal]); {do not localize}        end;        Result := NetAddrToStr(LI4[0]);      end;    end;    ID_IPv6 :    begin      SetLength(LI6, 10);      LRetVal :=  ResolveName6(AHostName, LI6);      if LRetVal < 1 then begin        raise EIdResolveError.CreateFmt(RSResolveError, [AHostName, LRetVal]);      end;      Result := NetAddrToStr6(LI6[0]);    end;  end; end; Indy Trace: ----2011-06-01 22:47:01 stacktrace: class: EIdResolveError message: Error resolving host ismsgateway.com: Error (-1)  $0814DFB2  TIDSTACKUNIX__HOSTBYNAME,  line 410 of /home/joshua/Desktop/Data/send_20110421/indy/IdStackUnix.pas  $0814C3F6  TIDSTACK__RESOLVEHOST,  line 518 of /home/joshua/Desktop/Data/send_20110421/indy/IdStack.pas  $08165DF1  TIDIOHANDLERSTACK__CONNECTCLIENT,  line 315 of /home/joshua/Desktop/Data/send_20110421/indy/IdIOHandlerStack.pas  $08165514  TIDIOHANDLERSOCKET__OPEN,  line 330 of /home/joshua/Desktop/Data/send_20110421/indy/IdIOHandlerSocket.pas  $0815C5E4  TIDTCPCLIENTCUSTOM__CONNECT,  line 305 of /home/joshua/Desktop/Data/send_20110421/indy/IdTCPClient.pas  $081BDAB4  TIDTELNET__CONNECT,  line 376 of /home/joshua/Desktop/Data/send_20110421/indy/IdTelnet.pas  $08074EA6  TINDYTELNETTHREAD__ONTIMER,  line 382 of indytelnetthread.pp To confirm that this is not an Indy issue, I created a test console to verify that ResolveName is the one that is giving problems.  Download full app source (inclusive of indy) from:- http://www.2shared.com/file/S1U0A5zw/test_app.html  procedure TMyApplication.DoRun;  var    i: integer;      LI4 : array of THostAddr;       LRetVal : Integer;  begin    try      i := 1;      while i < 1000 do      begin        try              SetLength(LI4, 10);              LRetVal := ResolveName('google.com', LI4);               if LRetVal < 1 then writeln('Failed to retrieve address!');              writeln('NetAddr:'+NetAddrToStr(LI4[0]));        except          on E: Exception do  begin            DumpException(E);          end;        end;      //  sleep(100);        Inc(i);      end;    finally    end;    // stop program loop    Terminate;  end;    Here's the output from the console.  You can also find a screen capture here showing that PING works while the DNS resolve failed - http://www.imageupload.org/?d=C894B4BF1 root@debian:/home/joshua/Desktop/Data/testapps/testpingclientconsole# ./project1 NetAddr:209.191.122.70 NetAddr:67.195.160.76 NetAddr:98.137.149.56 ...............repeat........ NetAddr:67.195.160.76 NetAddr:209.191.122.70 NetAddr:98.137.149.56 NetAddr:69.147.125.65 Failed to retrieve address! NetAddr:69.147.125.65 NetAddr:72.30.2.43 NetAddr:98.137.149.56 NetAddr:98.137.149.56 ...............repeat........ NetAddr:98.137.149.56 NetAddr:69.147.125.65 NetAddr:98.137.149.56 Heap dump by heaptrc unit 753 memory blocks allocated : 23777/28248 753 memory blocks freed     : 23777/28248 0 unfreed memory blocks : 0 True heap size : 131072 True free heap : 131072 root@debian:/home/joshua/Desktop/Data/testapps/testpingclientconsole# Output from the console if network is down before console is launched, and then network is reinstated, but ResolveName does not recover:- joshua@debian:~/Desktop/Data/testapps/testresolvenameconsole$ ./project1 Failed to retrieve address! NetAddr:0.0.0.0 Failed to retrieve address! NetAddr:0.0.0.0 Failed to retrieve address! NetAddr:0.0.0.0 Failed to retrieve address! NetAddr:0.0.0.0 Failed to retrieve address! NetAddr:0.0.0.0 Failed to retrieve address! NetAddr:0.0.0.0 Failed to retrieve address! NetAddr:0.0.0.0 Failed to retrieve address! ...............repeat........ Output from Console started with network up, disabled, and then enabled again:- joshua@debian:~/Desktop/Data/testapps/testresolvenameconsole$ ./project1 NetAddr:69.147.125.65 NetAddr:98.137.149.56 NetAddr:72.30.2.43 NetAddr:67.195.160.76 NetAddr:72.30.2.43 NetAddr:98.137.149.56 NetAddr:67.195.160.76 NetAddr:67.195.160.76 NetAddr:69.147.125.65 NetAddr:72.30.2.43 NetAddr:72.30.2.43 NetAddr:69.147.125.65 NetAddr:98.137.149.56 NetAddr:72.30.2.43 NetAddr:72.30.2.43 NetAddr:209.191.122.70 Failed to retrieve address! NetAddr:209.191.122.70 Failed to retrieve address! NetAddr:209.191.122.70 Failed to retrieve address! NetAddr:209.191.122.70 Failed to retrieve address! NetAddr:209.191.122.70 Failed to retrieve address! NetAddr:209.191.122.70 Failed to retrieve address! NetAddr:209.191.122.70 Failed to retrieve address! ..........repeat...... NetAddr:209.191.122.70 Failed to retrieve address! NetAddr:209.191.122.70 Failed to retrieve address! NetAddr:209.191.122.70 Failed to retrieve address! NetAddr:209.191.122.70 Failed to retrieve address! NetAddr:209.191.122.70 NetAddr:98.137.149.56 NetAddr:72.30.2.43 NetAddr:67.195.160.76 NetAddr:67.195.160.76 NetAddr:209.191.122.70 NetAddr:98.137.149.56 NetAddr:67.195.160.76 NetAddr:209.191.122.70 NetAddr:67.195.160.76 NetAddr:72.30.2.43 NetAddr:209.191.122.70 NetAddr:98.137.149.56 NetAddr:72.30.2.43 NetAddr:69.147.125.65 NetAddr:98.137.149.56 NetAddr:69.147.125.65 NetAddr:72.30.2.43 NetAddr:72.30.2.43 Failed to retrieve address! NetAddr:72.30.2.43 NetAddr:98.137.149.56 NetAddr:69.147.125.65 NetAddr:209.191.122.70 NetAddr:209.191.122.70 NetAddr:98.137.149.56 NetAddr:98.137.149.56 NetAddr:69.147.125.65 NetAddr:72.30.2.43 NetAddr:209.191.122.70 NetAddr:69.147.125.65 Failed to retrieve address!  <----down even though network connected. NetAddr:69.147.125.65 NetAddr:69.147.125.65 NetAddr:98.137.149.56 ..........repeat...... NetAddr:209.191.122.70 NetAddr:72.30.2.43 NetAddr:72.30.2.43 NetAddr:69.147.125.65 NetAddr:72.30.2.43 Heap dump by heaptrc unit 2153 memory blocks allocated : 63462/76648 2153 memory blocks freed     : 63462/76648 0 unfreed memory blocks : 0 True heap size : 131072 True free heap : 131072 joshua@debian:~/Desktop/Data/testapps/testresolvenameconsole$