# Buggy code
def __init__(self, host="127.0.0.1", port=9200):
    """Create a Elasticsearch client."""
    super().__init__()
    self._error_container = {}

    self.user = current_app.config.get("ELASTIC_USER", "user")
    self.password = current_app.config.get("ELASTIC_PASSWORD", "pass")
    self.ssl = current_app.config.get("ELASTIC_SSL", False)
    self.verify = current_app.config.get("ELASTIC_VERIFY_CERTS", True)

    if self.ssl:
        self.client = Elasticsearch(
            [{"host": host, "port": port}],
            http_auth=(self.user, self.password),
            use_ssl=self.ssl,
            verify_certs=self.verify,
        )
    else:
        self.client = Elasticsearch([{"host": host, "port": port}])

    self.import_counter = Counter()
    self.import_events = []


# Traceback (urllib3.exceptions.ReadTimeoutError)
[2021-01-21 09:57:16,813] celery.worker.strategy/INFO Received task: timesketch.lib.tasks.run_csv_jsonl[34b8f46651b94a8d8ad9e29bb06a147c]
[2021-01-21 09:57:16,819] timesketch.tasks/INFO Index timeline [vol timeline] to index [34b8f46651b94a8d8ad9e29bb06a147c] (source: jsonl)
[2021-01-21 09:58:14,763] timesketch.elasticsearch/ERROR Unable to add events
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 421, in _make_request
six.raise_from(e, None)
File "<string>", line 3, in raise_from
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 416, in _make_request
httplib_response = conn.getresponse()
File "/usr/lib/python3.8/http/client.py", line 1347, in getresponse
response.begin()
File "/usr/lib/python3.8/http/client.py", line 307, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.8/http/client.py", line 268, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/usr/lib/python3.8/socket.py", line 669, in readinto
return self._sock.recv_into(b)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/elasticsearch/connection/http_urllib3.py", line 241, in perform_request
response = self.pool.urlopen(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 719, in urlopen
retries = retries.increment(
File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 376, in increment
raise six.reraise(type(error), error, _stacktrace)
File "/usr/local/lib/python3.8/dist-packages/six.py", line 693, in reraise
raise value
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 665, in urlopen
httplib_response = self._make_request(
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 423, in _make_request
self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 330, in _raise_timeout
raise ReadTimeoutError(
urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='elasticsearch', port=9200): Read timed out. (read timeout=10)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/timesketch/lib/datastores/elastic.py", line 830, in flush_queued_events
results = self.client.bulk(body=self.import_events)
File "/usr/local/lib/python3.8/dist-packages/elasticsearch/client/utils.py", line 84, in _wrapped
return func(*args, params=params, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/elasticsearch/client/__init__.py", line 448, in bulk
return self.transport.perform_request(
File "/usr/local/lib/python3.8/dist-packages/elasticsearch/transport.py", line 351, in perform_request
status, headers_response, data = connection.perform_request(
File "/usr/local/lib/python3.8/dist-packages/elasticsearch/connection/http_urllib3.py", line 253, in perform_request
raise ConnectionTimeout("TIMEOUT", str(e), e)
elasticsearch.exceptions.ConnectionTimeout: ConnectionTimeout caused by - ReadTimeoutError(HTTPConnectionPool(host='elasticsearch', port=9200): Read timed out. (read timeout=10))
[2021-01-21 09:58:15,185] celery.app.trace/ERROR Task timesketch.lib.tasks.run_csv_jsonl[34b8f46651b94a8d8ad9e29bb06a147c] raised unexpected: UnboundLocalError("local variable 'results' referenced before assignment")
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/celery/app/trace.py", line 385, in trace_task
R = retval = fun(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/timesketch/app.py", line 198, in __call__
return TaskBase.__call__(self, *args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/celery/app/trace.py", line 650, in __protected_call__
return self.run(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/timesketch/lib/tasks.py", line 563, in run_csv_jsonl
results = es.flush_queued_events()
File "/usr/local/lib/python3.8/dist-packages/timesketch/lib/datastores/elastic.py", line 835, in flush_queued_events
errors_in_upload = results.get('errors', False)
UnboundLocalError: local variable 'results' referenced before assignment

# Fixed code
def __init__(self, host="127.0.0.1", port=9200):
    """Create a Elasticsearch client."""
    super().__init__()
    self._error_container = {}

    self.user = current_app.config.get("ELASTIC_USER", "user")
    self.password = current_app.config.get("ELASTIC_PASSWORD", "pass")
    self.ssl = current_app.config.get("ELASTIC_SSL", False)
    self.verify = current_app.config.get("ELASTIC_VERIFY_CERTS", True)

    if self.ssl:
        self.client = Elasticsearch(
            [{"host": host, "port": port}],
            http_auth=(self.user, self.password),
            use_ssl=self.ssl,
            verify_certs=self.verify,
        )
    else:
        self.client = Elasticsearch([{"host": host, "port": port}])

    self.import_counter = Counter()
    self.import_events = []
    self._request_timeout = current_app.config.get(
        "TIMEOUT_FOR_EVENT_IMPORT", self.DEFAULT_EVENT_IMPORT_TIMEOUT
    )