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

Issue with _set_db_data when using psycopg3 #3061

Open
austin-wentz opened this issue May 9, 2024 · 3 comments
Open

Issue with _set_db_data when using psycopg3 #3061

austin-wentz opened this issue May 9, 2024 · 3 comments
Assignees
Labels
Type: Bug Something isn't working

Comments

@austin-wentz
Copy link

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
@szokeasaurusrex
Copy link
Member

@austin-wentz Thank you for reporting this problem, and thanks for the detailed reproduction! I can confirm that I have also observed the same error, so we will try to get a fix ready soon.

Has what you are trying to do ever worked in a previous SDK version, and if so, which version? Also, have you been able to work around the issue?

@szokeasaurusrex szokeasaurusrex self-assigned this May 10, 2024
@sentrivana sentrivana added the Type: Bug Something isn't working label May 14, 2024
@austin-wentz
Copy link
Author

@austin-wentz Thank you for reporting this problem, and thanks for the detailed reproduction! I can confirm that I have also observed the same error, so we will try to get a fix ready soon.

Has what you are trying to do ever worked in a previous SDK version, and if so, which version? Also, have you been able to work around the issue?

Sorry for the delay in response. We tried previous SDK versions, but the issue was still present. Yes, we were able to work around the issue by creating a new connection rather than trying to [re]connect the existing connection object.

@antonpirker
Copy link
Member

Thanks for the additional information. We will have a fix soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working
Projects
Status: No status
Development

No branches or pull requests

4 participants