Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir errors using `inet_res` library

Tags:

erlang

elixir

I have some trouble with inet_res from Erlang. I tried to use it from my Elixir shell and also from my Erlang shell but I have unexpected behaviour. Sometimes I'm able to lookup a domain, sometimes I don't have the return value:

iex(1)> :inet_res.lookup('disneur.me', :in, :mx)
[]
iex(2)> :inet_res.lookup('disneur.me', :in, :mx)
[]
iex(3)> :inet_res.lookup('disneur.me', :in, :mx)
[{1, 'aspmx.l.google.com'}, {10, 'alt3.aspmx.l.google.com'},
 {10, 'alt4.aspmx.l.google.com'}, {5, 'alt1.aspmx.l.google.com'},
 {5, 'alt2.aspmx.l.google.com'}]

As you can see the first two times I called it, it returned an empty array and the thrid time, it returned the good value.

I tried to add in 4th parameters retry: 5 and also timeout: 10 but it doesn't help:

:inet_res.lookup('disneur.me', :in, :mx, timeout: 10, retry: 5)
[]
iex(2)> :inet_res.lookup('disneur.me', :in, :mx, timeout: 10, retry: 5)
[{1, 'aspmx.l.google.com'}, {10, 'alt3.aspmx.l.google.com'},
 {10, 'alt4.aspmx.l.google.com'}, {5, 'alt1.aspmx.l.google.com'},
 {5, 'alt2.aspmx.l.google.com'}]

I also tried directly from erlang shell and I have exactly the same behaviour:

1> inet_res:lookup("disneur.me", in, mx, [{timeout, 10}, {retry, 5}]).
[]
2> inet_res:lookup("disneur.me", in, mx, [{timeout, 10}, {retry, 5}]).
[]
3> inet_res:lookup("disneur.me", in, mx, [{timeout, 10}, {retry, 5}]).
[]
4> inet_res:lookup("disneur.me", in, mx, [{timeout, 10}, {retry, 5}]).
[{1,"aspmx.l.google.com"},
 {10,"alt3.aspmx.l.google.com"},
 {10,"alt4.aspmx.l.google.com"},
 {5,"alt1.aspmx.l.google.com"},
 {5,"alt2.aspmx.l.google.com"}]

Do you know why I have such unpredictable behaviour? Do I use this library the wrong way?

EDIT: For information, I tried with other domains (i.e: gmail.com) and I have the same issue. Other people also tried this code on their own computer and in different location. I also have the same failure on Circle-CI.

EDIT2: A copy-paste from a discussion I had on Slack (Elixir team)

troush [8:45 PM] It is working fine for my domain. Maybe this issue domain specific?

kdisneur [8:47 PM] did you try several times? I mean killing you session and retrying because sometimes it works, sometimes it doesn't.

troush [8:54 PM]

iex(35)> :inet_res.lookup('google.com', :in, :mx)
[{30, 'alt2.aspmx.l.google.com'}, {40, 'alt3.aspmx.l.google.com'},
 {50, 'alt4.aspmx.l.google.com'}, {10, 'aspmx.l.google.com'},
 {20, 'alt1.aspmx.l.google.com'}]
iex(36)> :inet_res.lookup('google.com', :in, :mx)
[{40, 'alt3.aspmx.l.google.com'}, {50, 'alt4.aspmx.l.google.com'},
 {10, 'aspmx.l.google.com'}, {20, 'alt1.aspmx.l.google.com'},
 {30, 'alt2.aspmx.l.google.com'}]
iex(37)> :inet_res.lookup('google.com', :in, :mx)
[{50, 'alt4.aspmx.l.google.com'}, {10, 'aspmx.l.google.com'},
 {20, 'alt1.aspmx.l.google.com'}, {30, 'alt2.aspmx.l.google.com'},
 {40, 'alt3.aspmx.l.google.com'}]

troush [8:55 PM] Maybe a connection isssue.

kdisneur [8:56 PM] yes when you are in a session and it's working once, I have the same result than you. That's why I was asking about restarting a new Elixir shell

troush [8:59 PM] Oh, okay, i understand. Yes, i got this issue on broad new iex session. Empty responses, but only on your domain. On gmail/google.com it works perfctly

troush ​[9:01] And i got same issue on my custom domain. So there some issue with this :simple_smile: sorry for disonanse

So I don't think it's coming from my network or for a specific domain. I think it's really about the way I use this library.

Thanks for your help

like image 339
Kevin Disneur Avatar asked Oct 31 '22 15:10

Kevin Disneur


1 Answers

It's a race condition. You're requesting data from something that doesn't exist yet. At VM initialization inet_db needs to grab a list of resolvers. You're making that query before that process completes.

To 'fast-track':

iex(1)>  :inet_db.add_ns({4,2,2,1}) ; :inet_res.lookup('disneur.me', :in, :mx)
[{10, 'alt3.aspmx.l.google.com'}, {10, 'alt4.aspmx.l.google.com'},
{5, 'alt1.aspmx.l.google.com'}, {5, 'alt2.aspmx.l.google.com'},
{1, 'aspmx.l.google.com'}]
like image 183
Redvers Davies Avatar answered Nov 29 '22 13:11

Redvers Davies