webis-de / mturk-manager

An alternative front end for Amazon Mechanical Turk
GNU General Public License v3.0
12 stars 4 forks source link

Timezone issue with the backend database #71

Open zeuner opened 3 years ago

zeuner commented 3 years ago

When I enter an URL and a Token at the frontend /add_credentials page, mturk-manager connects to /config at the backend, throwing an HTTP 500 error there.

With debugging enabled, I could get the following trace:

AssertionError at /config
database connection isn't set to UTC

Request Method: GET
Request URL: http://127.0.0.1:8004/config
Django Version: 3.0.8
Python Executable: /venv/bin/uwsgi
Python Version: 3.7.11
Python Path: ['/code', '.', '', '/usr/local/lib/python37.zip', '/usr/local/lib/python3.7', '/usr/local/lib/python3.7/lib-dynload', '/venv/lib/python3.7/site-packages']
Server time: Sat, 31 Jul 2021 10:00:53 +0000
Installed Applications:
['api.apps.ApiConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'rest_framework',
 'rest_framework.authtoken',
 'corsheaders',
 'django_celery_results',
 'django_celery_beat',
 'graphene_django']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'corsheaders.middleware.CorsMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback (most recent call last):
  File "/venv/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/venv/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/venv/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/venv/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "/venv/lib/python3.7/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
  File "/venv/lib/python3.7/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/venv/lib/python3.7/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/venv/lib/python3.7/site-packages/rest_framework/views.py", line 497, in dispatch
    self.initial(request, *args, **kwargs)
  File "/venv/lib/python3.7/site-packages/rest_framework/views.py", line 414, in initial
    self.perform_authentication(request)
  File "/venv/lib/python3.7/site-packages/rest_framework/views.py", line 324, in perform_authentication
    request.user
  File "/venv/lib/python3.7/site-packages/rest_framework/request.py", line 227, in user
    self._authenticate()
  File "/venv/lib/python3.7/site-packages/rest_framework/request.py", line 380, in _authenticate
    user_auth_tuple = authenticator.authenticate(self)
  File "/venv/lib/python3.7/site-packages/rest_framework/authentication.py", line 196, in authenticate
    return self.authenticate_credentials(token)
  File "/venv/lib/python3.7/site-packages/rest_framework/authentication.py", line 201, in authenticate_credentials
    token = model.objects.select_related('user').get(key=key)
  File "/venv/lib/python3.7/site-packages/django/db/models/query.py", line 411, in get
    num = len(clone)
  File "/venv/lib/python3.7/site-packages/django/db/models/query.py", line 258, in __len__
    self._fetch_all()
  File "/venv/lib/python3.7/site-packages/django/db/models/query.py", line 1261, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/venv/lib/python3.7/site-packages/django/db/models/query.py", line 57, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/venv/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1185, in execute_sql
    return list(result)
  File "/venv/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1584, in cursor_iter
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "/venv/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1584, in <lambda>
    for rows in iter((lambda: cursor.fetchmany(itersize)), sentinel):
  File "/venv/lib/python3.7/site-packages/django/db/utils.py", line 97, in inner
    return func(*args, **kwargs)
  File "/venv/lib/python3.7/site-packages/django/db/backends/postgresql/utils.py", line 6, in utc_tzinfo_factory
    raise AssertionError("database connection isn't set to UTC")

Any clues for how to debug this?

zeuner commented 3 years ago

I can enter credentials when I disable USE_TZ in the Django configuration: https://github.com/zeuner/mturk-manager/blob/postgres-timezone/mturk_db/mturk_db/settings.py#L143

I don't know whether mturk-manager is expected to work correctly with USE_TZ disabled, though.

Kritten commented 3 years ago

Could you please check the version of your installed psycopg2 package? This is something like the python driver for postgres, I think. If it's installed in version 2.9, please downgrade to 2.8 and try again.

This answer here states that they changed the utc behavior in version 2.9: https://stackoverflow.com/a/68025007/4071305

If it works in version 2.8 I have to update the requirements.txt and pin the version to 2.8 (should've done this from the beginning).

And yes, turning off the utc setting in django might help with getting rid of the error but is not a good idea.

zeuner commented 3 years ago

Thanks for the pointer. I can confirm that the docker container had psycopg2 version 2.9.1 installed.

With psycopg2 pinned to version 2.8.6 (https://github.com/webis-de/mturk-manager/pull/72), the error disappears.