RobertCraigie / prisma-client-py

Prisma Client Python is an auto-generated and fully type-safe database client designed for ease of use
https://prisma-client-py.readthedocs.io
Apache License 2.0
1.85k stars 81 forks source link

httpx.ConnectError: [Errno 111] Connection refused #937

Open cgtopher opened 6 months ago

cgtopher commented 6 months ago

Bug description

Getting "Connection Refused" error on connect to db. I've tested the networking with psql to verify my connection isn't getting blocked by firewalls, using same connection details.

How to reproduce

Not sure how reproducible this can be, but this is the minimal code I've been using to test it (normally running inside a flask application running behind gunicorn).

(venv) <user>@ubuntu-s-2vcpu-4gb-amd-sfo3-01:/<home-dir># export PRISMA_PY_DEBUG=1 && python
Python 3.11.6 (main, Oct  8 2023, 05:06:43) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from prisma import Prisma
>>> import logging
>>> p = Prisma()
>>> logging.getLogger('prisma').setLevel(logging.DEBUG)
>>> p.connect()

I'll put resulting logs in comments.

Expected behavior

Connects to database

Prisma information

generator client {
  provider             = "prisma-client-py"
  interface            = "sync"
  recursive_type_depth = 5
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

Environment & setup

prisma                  : 5.11.0
prisma client python    : 0.13.0
platform                : debian-openssl-3.0.x
expected engine version : efd2449663b3d73d637ea1fd226bafbcf45b3102
installed extras        : []
install path            : /<home-dir>/venv/lib/python3.11/site-packages/prisma
binary cache dir        : /<user>/.cache/prisma-python/binaries/5.11.0/efd2449663b3d73d637ea1fd226bafbcf45b3102
cgtopher commented 6 months ago

Logs from running .connect()

{"timestamp":"2024-04-03T23:49:14.423644Z","level":"INFO","fields":{"message":"Starting a postgresql pool with 5 connections."},"target":"quaint::pooled"}
{"timestamp":"2024-04-03T23:49:14.494218Z","level":"INFO","fields":{"message":"Started query engine http server on http://127.0.0.1:37103","ip":"127.0.0.1","port":"37103"},"target":"query_engine::server"}
Traceback (most recent call last):
  File "/<home-dir>/venv/lib/python3.11/site-packages/httpcore/_exceptions.py", line 10, in map_exceptions
    yield
  File "/<home-dir>/venv/lib/python3.11/site-packages/httpcore/_backends/sync.py", line 206, in connect_tcp
    sock = socket.create_connection(
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/socket.py", line 851, in create_connection
    raise exceptions[0]
  File "/usr/lib/python3.11/socket.py", line 836, in create_connection
    sock.connect(sa)
ConnectionRefusedError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/<home-dir>/venv/lib/python3.11/site-packages/httpx/_transports/default.py", line 67, in map_httpcore_exceptions
    yield
  File "/<home-dir>/venv/lib/python3.11/site-packages/httpx/_transports/default.py", line 231, in handle_request
    resp = self._pool.handle_request(req)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/<home-dir>/venv/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py", line 268, in handle_request
    raise exc
  File "/<home-dir>/venv/lib/python3.11/site-packages/httpcore/_sync/connection_pool.py", line 251, in handle_request
    response = connection.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/<home-dir>/venv/lib/python3.11/site-packages/httpcore/_sync/connection.py", line 99, in handle_request
    raise exc
  File "/<home-dir>/venv/lib/python3.11/site-packages/httpcore/_sync/connection.py", line 76, in handle_request
    stream = self._connect(request)
             ^^^^^^^^^^^^^^^^^^^^^^
  File "/<home-dir>/venv/lib/python3.11/site-packages/httpcore/_sync/connection.py", line 124, in _connect
    stream = self._network_backend.connect_tcp(**kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/<home-dir>/venv/lib/python3.11/site-packages/httpcore/_backends/sync.py", line 205, in connect_tcp
    with map_exceptions(exc_map):
  File "/usr/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/<home-dir>/venv/lib/python3.11/site-packages/httpcore/_exceptions.py", line 14, in map_exceptions
    raise to_exc(exc) from exc
httpcore.ConnectError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/<home-dir>/venv/lib/python3.11/site-packages/prisma/engine/_query.py", line 208, in spawn
    data = self.request('GET', '/status')
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/<home-dir>/venv/lib/python3.11/site-packages/prisma/engine/_http.py", line 150, in request
    response = self.session.request(method, url, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/<home-dir>/venv/lib/python3.11/site-packages/prisma/_sync_http.py", line 25, in request
    return Response(self.session.request(method, url, **kwargs))
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/<home-dir>/venv/lib/python3.11/site-packages/httpx/_client.py", line 828, in request
    return self.send(request, auth=auth, follow_redirects=follow_redirects)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/<home-dir>/venv/lib/python3.11/site-packages/httpx/_client.py", line 915, in send
    response = self._send_handling_auth(
               ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/<home-dir>/venv/lib/python3.11/site-packages/httpx/_client.py", line 943, in _send_handling_auth
    response = self._send_handling_redirects(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/<home-dir>/venv/lib/python3.11/site-packages/httpx/_client.py", line 980, in _send_handling_redirects
    response = self._send_single_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/<home-dir>/venv/lib/python3.11/site-packages/httpx/_client.py", line 1016, in _send_single_request
    response = transport.handle_request(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/<home-dir>/venv/lib/python3.11/site-packages/httpx/_transports/default.py", line 230, in handle_request
    with map_httpcore_exceptions():
  File "/usr/lib/python3.11/contextlib.py", line 155, in __exit__
    self.gen.throw(typ, value, traceback)
  File "/<home-dir>/venv/lib/python3.11/site-packages/httpx/_transports/default.py", line 84, in map_httpcore_exceptions
    raise mapped_exc(message) from exc
httpx.ConnectError: [Errno 111] Connection refused

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/<home-dir>/venv/lib/python3.11/site-packages/prisma/_base_client.py", line 305, in connect
    self._internal_engine.connect(
  File "/<home-dir>/venv/lib/python3.11/site-packages/prisma/engine/_query.py", line 190, in connect
    self.spawn(file, timeout=timeout, datasources=datasources)
  File "/<home-dir>/venv/lib/python3.11/site-packages/prisma/engine/_query.py", line 229, in spawn
    raise errors.EngineConnectionError('Could not connect to the query engine') from last_exc
prisma.engine.errors.EngineConnectionError: Could not connect to the query engine
vikyw89 commented 5 months ago

same here

egcash commented 4 months ago

Exactly same issue

Bug description

Getting "Connection Refused" error on connect to db. I've tested the networking with psql to verify my connection isn't getting blocked by firewalls, using same connection details.

How to reproduce

Not sure how reproducible this can be, but this is the minimal code I've been using to test it (normally running inside a flask application running behind gunicorn).

(venv) <user>@ubuntu-s-2vcpu-4gb-amd-sfo3-01:/<home-dir># export PRISMA_PY_DEBUG=1 && python
Python 3.11.6 (main, Oct  8 2023, 05:06:43) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from prisma import Prisma
>>> import logging
>>> p = Prisma()
>>> logging.getLogger('prisma').setLevel(logging.DEBUG)
>>> p.connect()

I'll put resulting logs in comments.

Expected behavior

Connects to database

Prisma information

generator client {
  provider             = "prisma-client-py"
  interface            = "sync"
  recursive_type_depth = 5
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

Environment & setup

  • OS: Ubuntu 23.10
  • Database: PostgreSQL
  • Python version: 3.11.6
  • Prisma version:
prisma                  : 5.11.0
prisma client python    : 0.13.0
platform                : debian-openssl-3.0.x
expected engine version : efd2449663b3d73d637ea1fd226bafbcf45b3102
installed extras        : []
install path            : /<home-dir>/venv/lib/python3.11/site-packages/prisma
binary cache dir        : /<user>/.cache/prisma-python/binaries/5.11.0/efd2449663b3d73d637ea1fd226bafbcf45b3102

Exactly same issue

RobertCraigie commented 2 months ago

Hey, sorry for the delayed response. Could you try connecting to your database using the JS client? It'd help narrow down this issue.

These commands should get the JS client set up for you:

npm init -y
npm install typescript ts-node @types/node --save-dev
npx tsc --init
npm install prisma --save-dev
vikyw89 commented 1 month ago

this happens on supabase, i did quick resboot on supabase and it worked. most probable culprit was supabase only allow 15 max connections, and somehow with fastapi (even with lifespan event to cleanup connection, the connections are somehow still open ?)

robbalian commented 3 days ago

Anyone end up addressing this with a workaround?

We use Prisma on both node and python, and python throws this error or pool overflow error on ~.5% of requests. I did a test with SQLAlchemy with no issues. I would much rather use Prisma Python though!