Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#32039 closed Bug (needsinfo)

psycop2.extras.register_default_jsonb() crashes with TypeError in Django 3.1.1.

Reported by: elliottyates Owned by: nobody
Component: Database layer (models, ORM) Version: 3.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

In a project that I recently upgraded from Django 3.1.0 to 3.1.1 I'm seeing the following traceback:

Traceback (most recent call last):
  File "manage.py", line 19, in <module>
    execute_from_command_line(sys.argv)
  File "/opt/libs/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/opt/libs/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/libs/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/libs/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/opt/libs/lib/python3.8/site-packages/django/core/management/base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "/opt/libs/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 92, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/opt/libs/lib/python3.8/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/opt/libs/lib/python3.8/site-packages/django/db/migrations/loader.py", line 53, in __init__
    self.build_graph()
  File "/opt/libs/lib/python3.8/site-packages/django/db/migrations/loader.py", line 216, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/opt/libs/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 77, in applied_migrations
    if self.has_table():
  File "/opt/libs/lib/python3.8/site-packages/django/db/migrations/recorder.py", line 55, in has_table
    with self.connection.cursor() as cursor:
  File "/opt/libs/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/opt/libs/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
    return self._cursor()
  File "/opt/libs/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor
    self.ensure_connection()
  File "/opt/libs/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/opt/libs/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/opt/libs/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/opt/libs/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/opt/libs/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/opt/libs/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 206, in get_new_connection
    psycopg2.extras.register_default_jsonb(conn_or_curs=connection, loads=lambda x: x)
  File "/opt/libs/lib/python3.8/site-packages/psycopg2/_json.py", line 155, in register_default_jsonb
    return register_json(conn_or_curs=conn_or_curs, globally=globally,
  File "/opt/libs/lib/python3.8/site-packages/psycopg2/_json.py", line 125, in register_json
    register_type(JSON, not globally and conn_or_curs or None)
TypeError: argument 2 must be a connection, cursor or None

Calling views results in a similar traceback on the first ORM call:

  File "/opt/libs/lib/python3.8/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/opt/libs/lib/python3.8/site-packages/django/db/models/query.py", line 425, in get
    num = len(clone)
  File "/opt/libs/lib/python3.8/site-packages/django/db/models/query.py", line 269, in __len__
    self._fetch_all()
  File "/opt/libs/lib/python3.8/site-packages/django/db/models/query.py", line 1303, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/opt/libs/lib/python3.8/site-packages/django/db/models/query.py", line 53, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/opt/libs/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1154, in execute_sql
    cursor = self.connection.cursor()
  File "/opt/libs/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/opt/libs/lib/python3.8/site-packages/django/db/backends/base/base.py", line 259, in cursor
    return self._cursor()
  File "/opt/libs/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor
    self.ensure_connection()
  File "/opt/libs/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/opt/libs/lib/python3.8/site-packages/django/db/backends/base/base.py", line 219, in ensure_connection
    self.connect()
  File "/opt/libs/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/opt/libs/lib/python3.8/site-packages/django/db/backends/base/base.py", line 200, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/opt/libs/lib/python3.8/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/opt/libs/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", line 206, in get_new_connection
    psycopg2.extras.register_default_jsonb(conn_or_curs=connection, loads=lambda x: x)
  File "/opt/libs/lib/python3.8/site-packages/psycopg2/_json.py", line 155, in register_default_jsonb
    return register_json(conn_or_curs=conn_or_curs, globally=globally,
  File "/opt/libs/lib/python3.8/site-packages/psycopg2/_json.py", line 125, in register_json
    register_type(JSON, not globally and conn_or_curs or None)
TypeError: argument 2 must be a connection, cursor or None

This happens with psycop2 2.8.5 and 2.8.6.

Reverting to Django 3.1.0 seems to fix it.

Perhaps related to: https://github.com/django/django/pull/13358/files#diff-aef216ccdc37941bfed98ea3c80f7642L203 from https://code.djangoproject.com/ticket/31956.

Thanks! Happy to provide more info with some guidance.

Change History (5)

comment:1 by Mariusz Felisiak, 4 years ago

Resolution: needsinfo
Status: newclosed
Summary: Django 3.1.1 psycop2.extras.register_default_jsonb in db.backends.postgresl.base failing with TypeErrorpsycop2.extras.register_default_jsonb() crashes with TypeError in Django 3.1.1.
Type: UncategorizedBug

Thanks for this ticket, however I cannot reproduce this issue. Can you provide a small sample project?

comment:2 by elliottyates, 4 years ago

Thank you @felixxm. I'm not sure I'd be able to reproduce on a trivial project either. If I see what's happening I'll include more here. Hopefully it is either something else in my project or others will be able to provide corroborating details.

(I'm not sure why, but this is being flagged as spam by the Trac platform -- I'm going to try submitting again...)

comment:3 by Mariusz Felisiak, 4 years ago

Thanks. It's not flagged as spam, you don't need to submit it again.

comment:4 by ziga, 4 years ago

We're having exactly same issue in 3.1.3 while 3.1.0 works fine.

I'll try to come up with minimal sample project.

comment:5 by ziga, 4 years ago

After long long debugging session – async debugging is hard – I've figured the main source of this issue was use of install_all_patches from opentracing_instrumentation.

Note: See TracTickets for help on using tickets.
Back to Top