Opened 2 months ago

Closed 2 months ago

Last modified 2 months ago

#32403 closed Bug (fixed)

When run test with off postgres database got `RuntimeError: generator didn't yield` instead of connection error

Reported by: Kazantcev Andrey Owned by: Mariusz Felisiak
Component: Database layer (models, ORM) Version: 3.1
Severity: Release blocker Keywords: postgres db
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have only default database configuration and if I run tests without started postgresql I got

  File "C:\Users\Asus\.virtualenvs\curds-cxXQlVO3\lib\site-packages\django\test\runner.py", line 695, in run_tests
    old_config = self.setup_databases(aliases=databases)
  File "C:\Users\Asus\.virtualenvs\curds-cxXQlVO3\lib\site-packages\django\test\runner.py", line 614, in setup_databases
    return _setup_databases(
  File "C:\Users\Asus\.virtualenvs\curds-cxXQlVO3\lib\site-packages\django\test\utils.py", line 170, in setup_databases
    connection.creation.create_test_db(
  File "C:\Users\Asus\.virtualenvs\curds-cxXQlVO3\lib\site-packages\django\db\backends\base\creation.py", line 55, in create_test_db
    self._create_test_db(verbosity, autoclobber, keepdb)
  File "C:\Users\Asus\.virtualenvs\curds-cxXQlVO3\lib\site-packages\django\db\backends\base\creation.py", line 186, in _create_test_db
    with self._nodb_cursor() as cursor:
  File "c:\python39\lib\contextlib.py", line 119, in __enter__
    raise RuntimeError("generator didn't yield") from None
RuntimeError: generator didn't yield

Seams _nodb_cursor method didn't do yield when handling error.

Change History (11)

comment:1 Changed 2 months ago by Mariusz Felisiak

Resolution: needsinfo
Status: newclosed

Thanks for the report, however the builtin django.db.backends.postgresql backend (on Python 3.9.0+ and 3.8.5) raises Connection refused for me (as expected):

django.db.utils.OperationalError: could not connect to server: Connection refused
        Is the server running on host "localhost" (127.0.0.1) and accepting
        TCP/IP connections on port 5432?

Can you share more details or a database configuration?

comment:2 in reply to:  1 Changed 2 months ago by Kazantcev Andrey

Replying to Mariusz Felisiak:

Can you share more details or a database configuration?

Configuration

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.getenv('POSTGRESQL_NAME', 'postgres'),
        'USER': os.getenv('POSTGRESQL_USER', 'postgres'),
        'PASSWORD': os.getenv('POSTGRESQL_PASSWORD', 'postgres'),
        'HOST': os.getenv('POSTGRESQL_HOST', 'localhost'),
        'PORT': os.getenv('POSTGRESQL_PORT', '5432'),
        'OPTIONS': json.loads(os.getenv('POSTGRESQL_ENGINE_OPTIONS', '{}'))
    }
}

No specific database env vars sets. Used only default values.

I found a problem. The problem is in [these lines](https://github.com/django/django/blob/f9cfd346f0222d86bfbac26ef5455806965bae6b/django/db/backends/postgresql/base.py#L324-L325). If I have one connection and test expression in if return false then function just return without reraise exception. And contextmanager raises error RuntimeError: generator didn't yield

Also, if I have more than two connection configurations with different NAME, this code will try to process only one next configuration, and if it is incorrect, it will throw an exception without trying to check the next connection.

Last edited 2 months ago by Kazantcev Andrey (previous) (diff)

comment:3 Changed 2 months ago by Tim Graham

Component: contrib.postgresDatabase layer (models, ORM)
Resolution: needsinfo
Status: closednew

I've also seen this with django_cockroachdb (which inherits the PostgreSQL backend).

comment:4 Changed 2 months ago by Simon Charette

Triage Stage: UnreviewedAccepted
Version: 3.23.1

Seems like a regression in f48f671223a20b161ca819cf7d6298e43b8ba5fe which is also part of 3.1.

comment:5 Changed 2 months ago by Mariusz Felisiak

Severity: NormalRelease blocker

Thanks for details, I can confirm that it's a regression in f48f671223a20b161ca819cf7d6298e43b8ba5fe.

comment:6 Changed 2 months ago by Mariusz Felisiak

Owner: set to Mariusz Felisiak
Status: newassigned

comment:7 Changed 2 months ago by Mariusz Felisiak

Has patch: set

comment:8 Changed 2 months ago by Carlton Gibson

Triage Stage: AcceptedReady for checkin

comment:9 Changed 2 months ago by GitHub <noreply@…>

Resolution: fixed
Status: assignedclosed

In f131841c:

Fixed #32403 -- Fixed re-raising DatabaseErrors when using only 'postgres' database.

Thanks Kazantcev Andrey for the report.

Regression in f48f671223a20b161ca819cf7d6298e43b8ba5fe.

comment:10 Changed 2 months ago by Mariusz Felisiak <felisiak.mariusz@…>

In 7d658893:

[3.2.x] Fixed #32403 -- Fixed re-raising DatabaseErrors when using only 'postgres' database.

Thanks Kazantcev Andrey for the report.

Regression in f48f671223a20b161ca819cf7d6298e43b8ba5fe.
Backport of f131841c601b9d4884adcdb284b4213c2ad89231 from master

comment:11 Changed 2 months ago by Mariusz Felisiak <felisiak.mariusz@…>

In 9efe832e:

[3.1.x] Fixed #32403 -- Fixed re-raising DatabaseErrors when using only 'postgres' database.

Thanks Kazantcev Andrey for the report.

Regression in f48f671223a20b161ca819cf7d6298e43b8ba5fe.
Backport of f131841c601b9d4884adcdb284b4213c2ad89231 from master

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