runtime icon indicating copy to clipboard operation
runtime copied to clipboard

"Device not configured" from Dns.GetHostEntryAsync on macOS

Open stephentoub opened this issue 10 years ago • 34 comments

https://ci.dot.net/job/dotnet_corefx/job/master/job/osx_debug_prtest/2571/consoleText

     System.Net.Tests.WebProxyTest.BypassOnLocal_MatchesExpected [FAIL]
        System.Net.Internals.SocketExceptionFactory+ExtendedSocketException : Device not configured
        Stack Trace:
              at System.Net.Dns.HostResolutionEndHelper(IAsyncResult asyncResult)
              at System.Net.Dns.EndGetHostEntry(IAsyncResult asyncResult)
              at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
           --- End of stack trace from previous location where exception was thrown ---
              at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
              at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
              at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
           /Users/dotnet-bot/j/workspace/dotnet_corefx/master/osx_debug_prtest/src/System.Net.WebProxy/tests/WebProxyTest.cs(146,0): at System.Net.Tests.WebProxyTest.<BypassOnLocal_MemberData>d__10.MoveNext()
              at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()

stephentoub avatar Nov 03 '16 13:11 stephentoub

Have just reproduced this and its causing us a nightmare.

Works fine in netcoreapp1.0 but netcoreapp1.1 explodes with above exception.

We have production systems wanting to move to 1.1 ASAP but can't

jchannon avatar Jan 09 '17 10:01 jchannon

cc: @geoffkizer, is this something you could investigate?

stephentoub avatar Jan 09 '17 14:01 stephentoub

@Priya91 can you please help here?

karelz avatar Jan 18 '17 20:01 karelz

@jchannon What is the host value for which you get this error? .Net error string Device not configured is thrown for host not found error. This function ultimately calls gethostbyname() native OS API, it will be valuable to get this datapoint.

Looking into how this error string gets it's value from.

Priya91 avatar Jan 19 '17 02:01 Priya91

An ip address

On Thu, 19 Jan 2017 at 02:50, Lakshmi Priya Sekar [email protected] wrote:

@jchannon https://github.com/jchannon What is the host value for which you get this error?

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/dotnet/corefx/issues/13309#issuecomment-273667855, or mute the thread https://github.com/notifications/unsubscribe-auth/AAGapoOXYUikeP6wKmN8tZwpcC-dzHvvks5rTs9_gaJpZM4KoYFg .

jchannon avatar Jan 19 '17 07:01 jchannon

here is my investigation from other issue on Mac SO

@stephentoub @Priya91 I heard from @karelz that you are contacts for the non Windows networking issues. on Sierra, there are a couple of net tests are failing for the same reason. what is happening is we are calling gethostname() which will return the current local host name (something like Tarek-MacBook-Pro.local and then we try to get the host entry info by calling gethostbyname() and passing the local host name we got from gethostname(). gethostbyname() is failing with the error HOST_NOT_FOUND.

I have tried to run getent hosts and it shows the only registered host is localhost. in other Linux machines the command show the expected full local host name.

also on Mac OS there is other failure caused by same reason:

      <test name="System.Net.NameResolution.PalTests.NameResolutionPalTests.GetHostByName_HostName" type="System.Net.NameResolution.PalTests.NameResolutionPalTests" method="GetHostByName_HostName" time="5.025076" result="Fail">
        <failure exception-type="System.Net.Internals.SocketExceptionFactory+ExtendedSocketException">
          <message><![CDATA[System.Net.Internals.SocketExceptionFactory+ExtendedSocketException : Device not configured]]></message>
          <stack-trace><![CDATA[   at System.Net.NameResolutionPal.GetHostByName(String hostName) in /private/tmp/corefx/src/System.Net.NameResolution/src/System/Net/NameResolutionPal.Unix.cs:line 151
   at System.Net.NameResolution.PalTests.NameResolutionPalTests.GetHostByName_HostName() in /private/tmp/corefx/src/System.Net.NameResolution/tests/PalTests/NameResolutionPalTests.cs:line 40]]></stack-trace>
        </failure>
      </test>

tarekgh avatar Jan 30 '17 22:01 tarekgh

@jchannon Can you provide a repro of the failure, the test bug is probably because GetHostEntryAsync failed on mac OS due to incorrect machine dns configuration. Trying host would have produced error host not found. We haven't had this test fail since on any of our CI machines, so I need more info to resolve your issue.

I used the following code with .NET Core 1.1 on OS X 10.10.5,

using System;
using System.Net;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
			Console.WriteLine(Dns.GetHostName());
			Console.WriteLine(Dns.GetHostEntryAsync(Dns.GetHostName()).GetAwaiter().GetResult().AddressList[0]);            
        }
    }
}

and got this output,

lasekar-osx:gethostname$ dotnet run
Project gethostname (.NETCoreApp,Version=v1.1) was previously compiled. Skipping compilation.
lasekar-osx.local
10.106.148.121

Please provide more info for further investigation.

Priya91 avatar Feb 14 '17 00:02 Priya91

I ran that same code with .NET Core 1.1 on macOS 10.12.3 (16D32) and got an exception.

[05:22pm yaakov@Expression:/tmp/dotnet-dns] dotnet run
Project dotnet-dns (.NETCoreApp,Version=v1.1) will be compiled because expected outputs are missing
Compiling dotnet-dns for .NETCoreApp,Version=v1.1

Compilation succeeded.
    0 Warning(s)
    0 Error(s)

Time elapsed 00:00:01.1827378
 

Expression.local

Unhandled Exception: System.Net.Internals.SocketExceptionFactory+ExtendedSocketException: Device not configured
   at System.Net.Dns.HostResolutionEndHelper(IAsyncResult asyncResult)
   at System.Net.Dns.EndGetHostEntry(IAsyncResult asyncResult)
   at System.Net.Dns.<>c.<GetHostEntryAsync>b__16_1(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at ConsoleApplication.Program.Main(String[] args) in /private/tmp/dotnet-dns/Program.cs:line 11
[05:22pm yaakov@Expression:/tmp/dotnet-dns] uname -a
Darwin Expression.local 16.4.0 Darwin Kernel Version 16.4.0: Thu Dec 22 22:53:21 PST 2016; root:xnu-3789.41.3~3/RELEASE_X86_64 x86_64

I wonder if something changed in the networking stack of newer versions of macOS.

yaakov-h avatar Feb 14 '17 06:02 yaakov-h

@yaakov-h I dont think it's the stack that's changed, as i just updated my mac today, and ran the program, it worked as expected. What the api does is call into gethostbyname or getaddrinfo to perform dns lookup. The error is what the exception states, that the dns servers may not be configured for your mac. You can follow the documentation here to verify that.

Closing the issue due to lack of information to proceed further. Please reopen with new info.

Priya91 avatar Feb 15 '17 00:02 Priya91

@Priya91 there are definitely DNS servers configured... and I can resolve other addresses as is evidenced by being able to resolve GitHub.com to post in this issue.

Similarly, if it was just a case of the Mac's own name not being known in reverse lookup, I'd expect System.Net.Sockets.SocketException (0x80004005): No such host is known instead...

What further info can I get, aside from dropping a laptop in the post (not gonna happen)?

yaakov-h avatar Feb 22 '17 05:02 yaakov-h

@yaakov-h given that we cannot reproduce the issue locally, the best thing is for you to debug the issue on your machine down to Mac APIs calls. Will they fail if you call them with the same values from native app? Another angle: Try if it fails on more than your machine ...

karelz avatar Feb 22 '17 19:02 karelz

@yaakov-h here's a sample native code with gethostbyname you can use to run on your laptop and what's the output?

#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
    if (argc < 2)
    {
      printf("Usage: %s hostname\n", argv[0]);
      exit(-1);
    }

    printf("GetHostByName results:\n");

    // gethostbyname
    struct hostent *hp = gethostbyname(argv[1]);

    if (hp == NULL) 
    {
      printf("gethostbyname() failed\n");
      printf("%s: %d\n", hstrerror(h_errno), h_errno);
      char buffer[500];
      strerror_r(h_errno, buffer, 500);
      printf("strerror_r: %s", buffer);
      printf("\n");
    } 
    else
    {
      printf("%s = ", hp->h_name);
      unsigned int i=0;
      while (hp->h_addr_list[i] != NULL)
      {
        printf("%s ", inet_ntoa(*(struct in_addr*)(hp->h_addr_list[i])));
        i++;
      }

      printf("\n");
    }

    printf("GetAddrInfo results:\n");

    // getaddrinfo
    struct addrinfo *result, *rp, hints;

    memset(&hints, 0, sizeof(struct addrinfo));
    hints.ai_family = AF_UNSPEC;    
    hints.ai_flags = 0x02;
    hints.ai_protocol = 0;          

    s = getaddrinfo(argv[1], NULL, &hints, &result);
    if (s != 0)
    {
      printf("getaddrinfo() failed\n");
      printf("%s %d\n", gai_strerror(s), s);
      char buffer[500];
      strerror_r(h_errno, buffer, 500);
      printf("strerror_r: %s", buffer);
      printf("\n");
    }
    else
    {
      printf("%s = ", result->ai_canonname);
      char str[INET6_ADDRSTRLEN];
      for (rp = result; rp != NULL; rp = rp->ai_next)
      {
        inet_ntop(AF_INET6, rp->ai_addr->sa_data, str, INET6_ADDRSTRLEN);
        printf("%s ", str);
      }
    }

    printf("\n");
    freeaddrinfo(result);
    return 0;
}

This is the output:

lasekar-osx:gethostbyname lakshmipriya$ ./a.out lasekar-osx.local
GetHostByName results:
lasekar-osx.local = 10.83.47.9 
GetAddrInfo results:
lasekar-osx.local = ::ee80:0:0:0:9cc ::fe70:0:0:0:6cc ::2002:5898:e0:2019:ce ::2041:3898:a0:3028:de 0:a54:2f07::5c61 0:a43:2e09:: 

Priya91 avatar Feb 22 '17 22:02 Priya91

[05:06pm yaakov@Expression:gethostbyname] ./a.out Expression
GetHostByName results:
gethostbyname() failed
Unknown host: 1
strerror_r: Operation not permitted
GetAddrInfo results:
getaddrinfo() failed
nodename nor servname provided, or not known 8
strerror_r: Operation not permitted

[05:06pm yaakov@Expression:gethostbyname] ./a.out Expression.local
GetHostByName results:
gethostbyname() failed
Unknown host: 1
strerror_r: Operation not permitted
GetAddrInfo results:
getaddrinfo() failed
nodename nor servname provided, or not known 8
strerror_r: Operation not permitted

I did some experimenting and comparing against another laptop, and I can now reproduce the case 100%.

Seemingly, macOS cannot resolve it's own hostname (including .local) if File Sharing - or potentially all sharing - is off. As soon as I turn it on, the name starts resolving, as soon as I turn it off (and clear the DNS cache) the lookup fails again.

yaakov-h avatar Feb 24 '17 06:02 yaakov-h

I ran into the "Device not configured" error with the above C# code by just being disconnected from Wifi. I was also able to duplicate this by having DNS servers that didn't recognize my machine in the DNS settings for my connection. Note that I'm pretty sure my macbook does have file sharing turned off. It is controlled by our IT department and I have no control over that setting.

macOS: 10.12.4 sdks: 1.0.0-preview2-003131, 1.0.0-preview2-1-003177

Tested with both 1.0 and 1.1 applications.

I ended up fixing this (at least for local machine connections) in our code by just checking if the target host was the "localhost" and switching to the Loopback IP Address.

if(Dns.GetHostName() == Destination.Host)
{
    return Client.ConnectAsync(IPAddress.Loopback, Destination.Port);
}

return Client.ConnectAsync(Destination.Host, Destination.Port);

joemcbride avatar Apr 27 '17 22:04 joemcbride

It's not clear if this is fixed (just says 'closed'), but I believe a workaround to be adding the machine name to the /etc/hosts file (at least on MacOS), with 127.0.0.1 as the address. I'm guessing there is a fix and it will be in the 2.0 release.

CleverCoder avatar Aug 08 '17 19:08 CleverCoder

Is there any progress on this issue? I'm getting it here as well...

galvesribeiro avatar Oct 31 '17 15:10 galvesribeiro

@galvesribeiro it was closed as "not reproducible" based on the comments above. If you have repro, which repros everywhere or if you have a fix, please let us know.

karelz avatar Oct 31 '17 20:10 karelz

@karelz I noticed that it happens on my office network but not on my home one.

It looks like the OSX DNS can't resolve by itself its own hostname so it rely on the DNS configured to it.

image

If the hostname DNS can't be resolved by the DNS server (the wifi router in this case) then we have a failure. My router at home (Amplifi) register every device it assign an IP on DHCP on its own DNS. So when OSX makes a DNS request to resolve it, it get the proper IP and don't fail.

I just think the exception from corefx should not be "Device not ready" but a "Can't resolve hostname" or something.

galvesribeiro avatar Oct 31 '17 20:10 galvesribeiro

"Device not configured" is what strerror gives for ENXIO, which is what we are supplying it for the HostNotFound case:

            { SocketError.HostNotFound, Interop.Error.ENXIO }, // not perfect, but closest match available

I see in some configurations ENXIO produces "No such device or address" which probably why it was used for HostNotFound. Perhaps that changed in recent versions of macOS? It could use EHOSTUNREACH (something like "No route to host") but of course that's not the same - the host isn't resolved at all.

I'm not sure where to look for the updated list of error strings on macOS - I looked at https://opensource.apple.com/source/gcc/gcc-5664/libiberty/strerror.c.auto.html and it says "No such device or address". @galvesribeiro what macOS version is this?

Another option is to use our own string in this particular case. It won't be localized, though.

danmoseley avatar Oct 31 '17 21:10 danmoseley

I'll reactivate this for that suggestion.

danmoseley avatar Oct 31 '17 22:10 danmoseley