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

ProgrammingError: 390114 (08001): Authentication token has expired. The user must authenticate again. #218

Closed
mvbrn opened this issue Oct 17, 2019 · 3 comments
Labels

Comments

@mvbrn
Copy link

mvbrn commented Oct 17, 2019

  1. What version of Python are you using (python --version)?

Python 3.7.4

  1. What operating system and processor architecture are you using (python -c 'import platform; print(platform.platform())')?

Linux-4.14.123-111.109.amzn2.x86_64-x86_64-with-debian-10.1

  1. What are the component versions in the environment (pip list)?
appdirs                       1.4.3
argon2-cffi                   19.1.0
asn1crypto                    0.24.0
atomicwrites                  1.3.0
attrs                         19.3.0
azure-common                  1.1.23
azure-storage-blob            2.1.0
azure-storage-common          2.1.0
bandit                        1.6.2
black                         19.3b0
boto3                         1.9.250
botocore                      1.12.250
CacheControl                  0.12.5
cachy                         0.2.0
certifi                       2019.9.11
cffi                          1.13.0
chardet                       3.0.4
cleo                          0.6.8
Click                         7.0
coreapi                       2.3.3
coreschema                    0.0.4
coverage                      4.5.4
cryptography                  2.7
dj-database-url               0.5.0
Django                        2.2.6
django-cors-headers           3.1.1
djangorestframework           3.10.3
djangorestframework-simplejwt 4.3.0
docutils                      0.15.2
drf-yasg                      1.17.0
factory-boy                   2.12.0
Faker                         2.0.3
Flask                         1.1.1
future                        0.18.0
gevent                        1.4.0
gitdb2                        2.0.6
GitPython                     3.0.3
greenlet                      0.4.15
gunicorn                      19.9.0
html5lib                      1.0.1
idna                          2.8
ijson                         2.5.1
importlib-metadata            0.23
inflection                    0.3.1
isort                         4.3.21
itsdangerous                  1.1.0
itypes                        1.1.0
Jinja2                        2.10.3
jmespath                      0.9.4
jsonschema                    3.1.1
lockfile                      0.12.2
locustio                      0.11.0
MarkupSafe                    1.1.1
more-itertools                7.2.0
msgpack                       0.6.2
numpy                         1.17.2
packaging                     19.2
pastel                        0.1.1
pbr                           5.4.3
pip                           19.3
pkginfo                       1.5.0.1
pluggy                        0.13.0
poetry                        0.12.17
psycopg2                      2.8.3
py                            1.8.0
pyarrow                       0.15.0
pycparser                     2.19
pycryptodomex                 3.9.0
PyJWT                         1.7.1
pylev                         1.3.0
pyOpenSSL                     19.0.0
pyparsing                     2.4.2
PyPika                        0.35.8
pyrsistent                    0.14.11
pytz                          2019.3
PyYAML                        5.1.2
pyzmq                         18.1.0
requests                      2.22.0
requests-toolbelt             0.8.0
ruamel.yaml                   0.16.5
ruamel.yaml.clib              0.2.0
s3transfer                    0.2.1
setuptools                    41.4.0
shellingham                   1.3.1
six                           1.12.0
smmap2                        2.0.5
snowflake-connector-python    2.0.1
sqlparse                      0.3.0
stevedore                     1.31.0
text-unidecode                1.3
toml                          0.10.0
tomlkit                       0.5.8
uritemplate                   3.0.0
urllib3                       1.25.6
wcwidth                       0.1.7
webencodings                  0.5.1
Werkzeug                      0.16.0
wheel                         0.33.6
zipp                          0.6.0
  1. What did you do?

Used a persistent connection to the snowflake in Django application. Suddenly it started raising exception.

  1. What did you expect to see?

No exception raised

  1. What did you see instead?
Traceback:

File "/usr/local/lib/python3.7/site-packages/snowflake/connector/network.py" in _renew_session
  327.             return self._token_request(REQUEST_TYPE_RENEW)

File "/usr/local/lib/python3.7/site-packages/snowflake/connector/network.py" in _token_request
  403.                         sqlstate=SQLSTATE_CONNECTION_WAS_NOT_ESTABLISHED))

During handling of the above exception (390114 (08001): Authentication token has expired.  The user must authenticate again.), another exception occurred:

File "/usr/local/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/usr/local/lib/python3.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  54.         return view_func(*args, **kwargs)

File "/usr/local/lib/python3.7/site-packages/django/views/generic/base.py" in view
  71.             return self.dispatch(request, *args, **kwargs)

File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py" in dispatch
  505.             response = self.handle_exception(exc)

File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py" in handle_exception
  465.             self.raise_uncaught_exception(exc)

File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py" in raise_uncaught_exception
  476.         raise exc

File "/usr/local/lib/python3.7/site-packages/rest_framework/views.py" in dispatch
  502.             response = handler(request, *args, **kwargs)

...

File "/app/src/core/snowflake.py" in _execute_query
  27.         ret = list(self._conn.cursor(DictCursor).execute(sql))

File "/usr/local/lib/python3.7/site-packages/snowflake/connector/cursor.py" in execute
  510.             _is_put_get=_is_put_get)

File "/usr/local/lib/python3.7/site-packages/snowflake/connector/cursor.py" in _execute_helper
  384.                 _no_results=_no_results)

File "/usr/local/lib/python3.7/site-packages/snowflake/connector/connection.py" in cmd_query
  854.             _include_retry_params=True)

File "/usr/local/lib/python3.7/site-packages/snowflake/connector/network.py" in request
  304.                 timeout=timeout, _include_retry_params=_include_retry_params)

File "/usr/local/lib/python3.7/site-packages/snowflake/connector/network.py" in _post_request
  540.                 ret = self._renew_session()

File "/usr/local/lib/python3.7/site-packages/snowflake/connector/network.py" in _renew_session
  330.                 raise ex.cause

(part of the traceback is removed)

After the process restart, it started working.

@smtakeda
Copy link
Contributor

This error occurs if the connection is idle, i.e., no query is submitted, for over four hours.
One solution is set client_session_keep_alive to True such that the background thread pings hourly to maintain the tokens refreshed.
Other way is run some queries periodically, e.g., select 1 every hour or so.

@bubthegreat
Copy link

We've been struggling to catch this exception to perform re-authentication for a service account, but can't ever seem to catch the exception, even with a bare try/except - is there something special about this exception that we're missing when we try and catch the exception?

@mvbrn mvbrn closed this as completed Jul 23, 2020
@eyalyoli
Copy link

eyalyoli commented Jul 27, 2022

this is how you do it with an engine:

    from snowflake.sqlalchemy import URL

    engine = create_engine(
        URL(
            account=DB_ACCOUNT,
            user=DB_USER,
            password=DB_PASSWORD
        ),
        pool_size=50, 
        max_overflow=100,
        connect_args={
            'client_session_keep_alive': True,
        })
    Session = sessionmaker(engine)

or a connection

    ctx = snowflake.connector.connect(
   user='<user_name>',
   password='<password>',
   account='myorganization-myaccount',
   client_session_keep_alive=True
)

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

4 participants