New Connection Error

I have a live trading program that uses the Alpaca Python SDK. It spins up a thread and does all the trading on the thread. If the main thread detects the trading thread has died, it restarts the thread again, up to MAX_RETRY attempts (in my case 15) with 15 seconds between restart attempts. In the below logs for today, I’m getting these exceptions. Does anybody have an idea of what might be happening and a potential resolution? Thanks in advance!

2020-10-19 10:08:28 do_live_trading_no_ws.py: Successfully started new thread <Thread(Thread-15, started 25572)>. 1 remaining
Exception in thread Thread-15:
Traceback (most recent call last):
  File "USER_PATH\venv\lib\site-packages\urllib3\connection.py", line 160, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "USER_PATH\venv\lib\site-packages\urllib3\util\connection.py", line 61, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "USER_PATH\AppData\Local\Programs\Python\Python36\lib\socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "USER_PATH\venv\lib\site-packages\urllib3\connectionpool.py", line 677, in urlopen
    chunked=chunked,
  File "USER_PATH\venv\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request
    self._validate_conn(conn)
  File "USER_PATH\venv\lib\site-packages\urllib3\connectionpool.py", line 978, in _validate_conn
    conn.connect()
  File "USER_PATH\venv\lib\site-packages\urllib3\connection.py", line 309, in connect
    conn = self._new_conn()
  File "USER_PATH\venv\lib\site-packages\urllib3\connection.py", line 172, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x00000259A4A5A828>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "USER_PATH\venv\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "USER_PATH\venv\lib\site-packages\urllib3\connectionpool.py", line 727, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "USER_PATH\venv\lib\site-packages\urllib3\util\retry.py", line 439, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='paper-api.alpaca.markets', port=443): Max retries exceeded with url: /v2/positions/[SYMBOL_PLACEHOLDER] (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000259A4A5A828>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "USER_PATH\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "USER_PATH\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "USER_PATH/do_live_trading_no_ws.py", line 197, in make_trade_decision
    qty = int(api.get_position(ticker).qty)
  File "USER_PATH\venv\lib\site-packages\alpaca_trade_api\rest.py", line 383, in get_position
    resp = self.get('/positions/{}'.format(symbol))
  File "USER_PATH\venv\lib\site-packages\alpaca_trade_api\rest.py", line 156, in get
    return self._request('GET', path, data)
  File "USER_PATH\venv\lib\site-packages\alpaca_trade_api\rest.py", line 119, in _request
    return self._one_request(method, url, opts, retry)
  File "USER_PATH\venv\lib\site-packages\alpaca_trade_api\rest.py", line 138, in _one_request
    resp = self._session.request(method, url, **opts)
  File "USER_PATH\venv\lib\site-packages\requests\sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "USER_PATH\venv\lib\site-packages\requests\sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "USER_PATH\venv\lib\site-packages\requests\adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='paper-api.alpaca.markets', port=443): Max retries exceeded with url: /v2/positions/[SYMBOL_PLACEHOLDER] (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000259A4A5A828>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))

2020-10-19 10:08:43 do_live_trading_no_ws.py: Thread <Thread(Thread-15, stopped 25572)> died unexpectedly. Restarting new thread.
2020-10-19 10:08:43 do_live_trading_no_ws.py: Successfully started new thread <Thread(Thread-16, started 28772)>. 0 remaining
Exception in thread Thread-16:
Traceback (most recent call last):
  File "USER_PATH\venv\lib\site-packages\urllib3\connection.py", line 160, in _new_conn
    (self._dns_host, self.port), self.timeout, **extra_kw
  File "USER_PATH\venv\lib\site-packages\urllib3\util\connection.py", line 61, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "USER_PATH\AppData\Local\Programs\Python\Python36\lib\socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "USER_PATH\venv\lib\site-packages\urllib3\connectionpool.py", line 677, in urlopen
    chunked=chunked,
  File "USER_PATH\venv\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request
    self._validate_conn(conn)
  File "USER_PATH\venv\lib\site-packages\urllib3\connectionpool.py", line 978, in _validate_conn
    conn.connect()
  File "USER_PATH\venv\lib\site-packages\urllib3\connection.py", line 309, in connect
    conn = self._new_conn()
  File "USER_PATH\venv\lib\site-packages\urllib3\connection.py", line 172, in _new_conn
    self, "Failed to establish a new connection: %s" % e
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x00000259A4A5A278>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "USER_PATH\venv\lib\site-packages\requests\adapters.py", line 449, in send
    timeout=timeout
  File "USER_PATH\venv\lib\site-packages\urllib3\connectionpool.py", line 727, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "USER_PATH\venv\lib\site-packages\urllib3\util\retry.py", line 439, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='paper-api.alpaca.markets', port=443): Max retries exceeded with url: /v2/positions/[SYMBOL_PLACEHOLDER] (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000259A4A5A278>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "USER_PATH\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "USER_PATH\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "USER_PATH/do_live_trading_no_ws.py", line 197, in make_trade_decision
    qty = int(api.get_position(ticker).qty)
  File "USER_PATH\venv\lib\site-packages\alpaca_trade_api\rest.py", line 383, in get_position
    resp = self.get('/positions/{}'.format(symbol))
  File "USER_PATH\venv\lib\site-packages\alpaca_trade_api\rest.py", line 156, in get
    return self._request('GET', path, data)
  File "USER_PATH\venv\lib\site-packages\alpaca_trade_api\rest.py", line 119, in _request
    return self._one_request(method, url, opts, retry)
  File "USER_PATH\venv\lib\site-packages\alpaca_trade_api\rest.py", line 138, in _one_request
    resp = self._session.request(method, url, **opts)
  File "USER_PATH\venv\lib\site-packages\requests\sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "USER_PATH\venv\lib\site-packages\requests\sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "USER_PATH\venv\lib\site-packages\requests\adapters.py", line 516, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='paper-api.alpaca.markets', port=443): Max retries exceeded with url: /v2/positions/[SYMBOL_PLACEHOLDER] (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x00000259A4A5A278>: Failed to establish a new connection: [Errno 11001] getaddrinfo failed',))

Did you replace “[SYMBOL_PLACEHOLDER]” in “/v2/positions/[SYMBOL_PLACEHOLDER]”?
It looks like the URL is failing without an actual symbol.

Yeah, I replaced that in the logs not in the actual program. I should have clarified that. The program works normally except when it encounters rare semi-failure states like this one. Similar to USER_PATH