Skip to content

Issue with _set_db_data when using psycopg3 #3061

Closed
@austin-wentz

Description

@austin-wentz

How do you use Sentry?

Sentry Saas (sentry.io)

Version

2.1.1

Steps to Reproduce

I created a sample repo that reproduces the bug.

  1. Checkout the repo
git clone https://github.com/austin-wentz/sentry-psycopg-bug.git
  1. Install requirements
pip install -r requirements.txt
  1. Startup postgres
docker-compose up
  1. Run test without Sentry (test will succeed)
./manage.py test
  1. Run test with Sentry (test will fail)
export USE_SENTRY=True && ./manage.py test

From what I can tell, the issue is that the call to get_parameters() tries to access pgconn.info, but that will throw an operational error if the connection is already closed.

A suggested fix would be something like

if is_psycopg3:
    connection_params = {}
    if not cursor_or_db.connection.closed
        connection_params = cursor_or_db.connection.info.get_parameters()

Expected Result

The expected result is for the sentry-sdk to not try and access a bad connection in _set_db_data

Actual Result

The actual result is that the call to get_parameters() results in a psycopg.OperationalError:

Traceback (most recent call last):
  File "/Users/austin/workplace/sentry-psycopg-bug/spbug/tests.py", line 11, in test_bug
    connection.connect()
  File "/Users/austin/workplace/sentry-psycopg-bug/venv_3_10/lib/python3.10/site-packages/sentry_sdk/utils.py", line 1711, in runner
    return sentry_patched_function(*args, **kwargs)
  File "/Users/austin/workplace/sentry-psycopg-bug/venv_3_10/lib/python3.10/site-packages/sentry_sdk/integrations/django/__init__.py", line 658, in connect
    _set_db_data(span, self)
  File "/Users/austin/workplace/sentry-psycopg-bug/venv_3_10/lib/python3.10/site-packages/sentry_sdk/integrations/django/__init__.py", line 693, in _set_db_data
    connection_params = cursor_or_db.connection.info.get_parameters()
  File "/Users/austin/workplace/sentry-psycopg-bug/venv_3_10/lib/python3.10/site-packages/psycopg/_connection_info.py", line 91, in get_parameters
    for i in self.pgconn.info
  File "psycopg_binary/pq/pgconn.pyx", line 90, in psycopg_binary.pq.PGconn.info.__get__
  File "psycopg_binary/pq/pgconn.pyx", line 635, in psycopg_binary.pq._ensure_pgconn
psycopg.OperationalError: the connection is closed

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions