Repository: avocado-framework/avocado

Issue:
assets fetch: handshake timeout it is not being caught
During investigation of #4824 I found a problem with timeout during handshake.


```
Process Process-1:
Traceback (most recent call last):
  File "/usr/lib64/python3.7/urllib/request.py", line 1350, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/usr/lib64/python3.7/http/client.py", line 1277, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib64/python3.7/http/client.py", line 1323, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/lib64/python3.7/http/client.py", line 1272, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/lib64/python3.7/http/client.py", line 1032, in _send_output
    self.send(msg)
  File "/usr/lib64/python3.7/http/client.py", line 972, in send
    self.connect()
  File "/usr/lib64/python3.7/http/client.py", line 1447, in connect
    server_hostname=server_hostname)
  File "/usr/lib64/python3.7/ssl.py", line 423, in wrap_socket
    session=session
  File "/usr/lib64/python3.7/ssl.py", line 870, in _create
    self.do_handshake()
  File "/usr/lib64/python3.7/ssl.py", line 1139, in do_handshake
    self._sslobj.do_handshake()
socket.timeout: _ssl.c:1074: The handshake operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib64/python3.7/multiprocessing/process.py", line 297, in _bootstrap
    self.run()
  File "/usr/lib64/python3.7/multiprocessing/process.py", line 99, in run
    self._target(*self._args, **self._kwargs)
  File "/home/local/src/beraldo/avocado/avocado/utils/download.py", line 69, in download
    src_file = url_open(url, data=data)
  File "/home/local/src/beraldo/avocado/avocado/utils/download.py", line 46, in url_open
    result = urlopen(url, data=data, timeout=timeout)
  File "/usr/lib64/python3.7/urllib/request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib64/python3.7/urllib/request.py", line 525, in open
    response = self._open(req, data)
  File "/usr/lib64/python3.7/urllib/request.py", line 543, in _open
    '_open', req)
  File "/usr/lib64/python3.7/urllib/request.py", line 503, in _call_chain
    result = func(*args)
  File "/usr/lib64/python3.7/urllib/request.py", line 1393, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/usr/lib64/python3.7/urllib/request.py", line 1352, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error _ssl.c:1074: The handshake operation timed out>
Failed to fetch download ([Errno 2] No such file or directory: '/home/local/avocado/data/cache/by_location/4135b9170bbc2afc3ec7f79c169bf722b4d2afe7/download.cd7fe814-e6c0-4b10-82cd-e390d92fa099').
(avocado-py3.7) [local@laptop avocado]$ 
```

Probably this is related to 9b75f9d62faf53248fc687bd6e4dde85bb705b56, but I'm still investigating.


Discussion:

Resolving patch:
diff --git a/avocado/utils/download.py b/avocado/utils/download.py
--- a/avocado/utils/download.py
+++ b/avocado/utils/download.py
@@ -20,12 +20,14 @@
 import logging
 import os
 import shutil
+import socket
+import sys
 from multiprocessing import Process
 from urllib.request import urlopen
 
 from . import aurl, crypto, output
 
-log = logging.getLogger('avocado.test')
+log = logging.getLogger('avocado.utils.download')
 
 
 def url_open(url, data=None, timeout=5):
@@ -41,7 +43,13 @@ def url_open(url, data=None, timeout=5):
     :return: file-like object.
     :raises: `URLError`.
     """
-    result = urlopen(url, data=data, timeout=timeout)
+    try:
+        result = urlopen(url, data=data, timeout=timeout)
+    except socket.timeout as ex:
+        msg = "Timeout was reach: {}".format(str(ex))
+        log.error(msg)
+        return None
+
     msg = ('Retrieved URL "%s": content-length %s, date: "%s", '
            'last-modified: "%s"')
     log.debug(msg, url,
@@ -62,9 +70,13 @@ def url_download(url, filename, data=None, timeout=300):
     :return: `None`.
     """
     def download():
-        log.info('Fetching %s -> %s', url, filename)
-
         src_file = url_open(url, data=data)
+        if not src_file:
+            msg = ("Failed to get file. Probably timeout was reach when "
+                   "connecting to the server.\n")
+            sys.stderr.write(msg)
+            sys.exit(1)
+
         try:
             with open(filename, 'wb') as dest_file:
                 shutil.copyfileobj(src_file, dest_file)
@@ -72,6 +84,7 @@ def download():
             src_file.close()
 
     process = Process(target=download)
+    log.info('Fetching %s -> %s', url, filename)
     process.start()
     process.join(timeout)
     if process.is_alive():


Test patch: