Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I fix this error: “Temporary failure in name resolution” #436

Closed
vgoklani opened this issue Jul 13, 2018 · 6 comments
Closed
Labels

Comments

@vgoklani
Copy link

I received this error while connecting to a websocket (in python). The error handler automatically resets the connection, and the stream restarts. But the connections eventually break again every few hours.

What does the error mean? Why does the connection work, and then suddenly break after a few hours?

How do I fix this error? Is it some type of lookup issue? Wouldn't those result be cached?

Googling around, I saw this: "you probably should run a caching DNS forwarder locally, that'll shave off some milliseconds off your requests." Could you someone please explain what that means and just point me in the right direction.

Thanks!

My connection code is below. Is there anyway to cache the DNS here? Why would there need to be a lookup if it's a websocket connection?

	async def connect(self):
		while True:
			log.info("connecting")
			async with websockets.connect(self.url, max_queue=2**10,) as websocket:
				log.info("connected")
				await websocket.send(json.dumps(self.subscribe))
				log.info("subscribed")
				while(True):
					try:
						message_str = await asyncio.wait_for(websocket.recv(), timeout=self._timeout)
						if message_str[9:18] != "heartbeat":
							# await self.queue.put(message_str)
							self.queue.put_nowait(message_str)
					except Exception as e:
						raise Exception("no data in {} seconds, killing connection".format(self._timeout))
@aaugustin
Copy link
Member

It's hard to tell without the full stack trace.

Most likely your system occasionally fails to resolve the hostname in self.url into an IP address to connect to. However that exception would cause the code you provided to exit, not to loop like you describe.

@aaugustin
Copy link
Member

Anyway it's clearly not a bug in websockets, as websockets relies on asyncio / Python / the operating system to establish connections. The answer is inevitably going to be "look at a lower level".

@antonioromero
Copy link

antonioromero commented Nov 21, 2018

I have a similar problem. Here is my trace.

2018-11-20 02:28:22,294 WARNING websockets.protocol Error in data transfer
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/websockets/protocol.py", line 496, in transfer_data
    msg = yield from self.read_message()
  File "/usr/local/lib/python3.6/site-packages/websockets/protocol.py", line 526, in read_message
    frame = yield from self.read_data_frame(max_size=self.max_size)
  File "/usr/local/lib/python3.6/site-packages/websockets/protocol.py", line 591, in read_data_frame
    frame = yield from self.read_frame(max_size)
  File "/usr/local/lib/python3.6/site-packages/websockets/protocol.py", line 632, in read_frame
    extensions=self.extensions,
  File "/usr/local/lib/python3.6/site-packages/websockets/framing.py", line 100, in read
    data = yield from reader(2)
  File "/usr/local/lib/python3.6/asyncio/streams.py", line 674, in readexactly
    yield from self._wait_for_data('readexactly')
  File "/usr/local/lib/python3.6/asyncio/streams.py", line 464, in _wait_for_data
    yield from self._waiter
  File "/usr/local/lib/python3.6/asyncio/selector_events.py", line 723, in _read_ready
    data = self._sock.recv(self.max_size)
ConnectionResetError: [Errno 104] Connection reset by peer

2018-11-20 06:40:48,207 WARNING turnstile.sync Connection closed

2018-11-20 10:48:06,424 WARNING turnstile.sync Connection closed

2018-11-20 11:35:49,602 ERROR asyncio Fatal read error on socket transport
protocol: <asyncio.sslproto.SSLProtocol object at 0xb5c51e10>
transport: <_SelectorSocketTransport fd=10 read=polling write=<idle, bufsize=0>>
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/asyncio/selector_events.py", line 723, in _read_ready
    data = self._sock.recv(self.max_size)
TimeoutError: [Errno 110] Operation timed out

2018-11-20 11:35:49,607 WARNING websockets.protocol Error in data transfer
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/websockets/protocol.py", line 496, in transfer_data
    msg = yield from self.read_message()
  File "/usr/local/lib/python3.6/site-packages/websockets/protocol.py", line 526, in read_message
    frame = yield from self.read_data_frame(max_size=self.max_size)
  File "/usr/local/lib/python3.6/site-packages/websockets/protocol.py", line 591, in read_data_frame
    frame = yield from self.read_frame(max_size)
  File "/usr/local/lib/python3.6/site-packages/websockets/protocol.py", line 632, in read_frame
    extensions=self.extensions,
  File "/usr/local/lib/python3.6/site-packages/websockets/framing.py", line 100, in read
    data = yield from reader(2)
  File "/usr/local/lib/python3.6/asyncio/streams.py", line 674, in readexactly
    yield from self._wait_for_data('readexactly')
  File "/usr/local/lib/python3.6/asyncio/streams.py", line 464, in _wait_for_data
    yield from self._waiter
  File "/usr/local/lib/python3.6/asyncio/selector_events.py", line 723, in _read_ready
    data = self._sock.recv(self.max_size)
TimeoutError: [Errno 110] Operation timed out

2018-11-20 11:37:18,221 WARNING turnstile.sync Connection closed

2018-11-20 11:37:28,242 ERROR turnstile.sync [Errno -3] Try again

2018-11-20 11:37:28,253 ERROR turnstile.sync Traceback (most recent call last):
  File "/app/turnstile/sync.py", line 83, in sync
    loop=self.loop)
  File "/usr/local/lib/python3.6/site-packages/websockets/client.py", line 393, in __await__
    transport, protocol = yield from self._creating_connection
  File "/usr/local/lib/python3.6/asyncio/base_events.py", line 734, in create_connection
    infos = f1.result()
  File "/usr/local/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/local/lib/python3.6/socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -3] Try again

After that I get always the last error (socket.gaierror).

I use websockets==4.0.1

@aaugustin
Copy link
Member

I believe the root cause is visible in this stack trace:

ConnectionResetError: [Errno 104] Connection reset by peer

@antonioromero
Copy link

Indeed. I think it is because the client (who uses a LTE-Connection) lost internet connection. After that trace we get always socket.gaierror. The client cannot resolve DNS.
My problem with the Exception ConnectionResetError: [Errno 104] Connection reset by peer is that it begins at

File "/usr/local/lib/python3.6/site-packages/websockets/protocol.py", line 496, in transfer_data
    msg = yield from self.read_message()

There is no reference to my script. How could I handle that?

@aaugustin
Copy link
Member

If your script calls yield from recv(), it gets a ConnectionClosed exception, which is how you know that the connection dropped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants