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

Prisma client never recovers from P1001 #10240

Closed
haggalin opened this issue Nov 12, 2021 · 11 comments
Closed

Prisma client never recovers from P1001 #10240

haggalin opened this issue Nov 12, 2021 · 11 comments
Labels
bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: connect topic: connection pool
Milestone

Comments

@haggalin
Copy link

haggalin commented Nov 12, 2021

Bug description

Prisma client never recovers from PrismaClientInitializationError error code P1001, in a scenario where the database goes offline and then online again after some time. Haven't tested other error codes.

We use a serverless aurora v1 from AWS which has a feature called "AutoPause" i.e. the database pauses when there's no activity and then resumes when activity begins again.

How to reproduce

  1. Verify that postgres is not running at localhost:5432
  2. Run node test.js.
    Contents of test.js:
    /* eslint-disable */
    const { PrismaClient } = require('@prisma/client');
    const readline = require('readline')
    const url = 'postgresql://user:pass@localhost:5432/postgres';
    
    function createClient() {
        return new PrismaClient({
            datasources: {
                db: {
                    url
                }
            }
        });
    }
    
    let prisma = createClient();
    
    function askQuestion(query) {
        const rl = readline.createInterface({
            input: process.stdin,
            output: process.stdout,
        });
    
        return new Promise(resolve => rl.question(query, ans => {
            rl.close();
            resolve(ans);
        }))
    }
    
    
    async function main() {
        try {
            await prisma.$connect();
            //await prisma.$queryRaw`SELECT 1`
        } catch (error) {
            if (error?.errorCode !== 'P1001') {
                console.error(error);
                throw error;
            }
            await askQuestion(`Start up the postgres database at ${url}. Press ENTER when database is online.`);
            try {
                // Note: Workaround is to recreate the client:
                //prisma = createClient();
                await prisma.$connect();
                //await prisma.$queryRaw`SELECT 1`
            } catch (e) {
                console.error(e);
                console.log('\n' +
                    'Test failed');
                return;
            }
            console.log('Test passed!');
            return;
        }
        throw new Error('Test precondition failed: Database should be offline in the first query');
    
    }
    
    main()
        .catch((e) => {
            console.error(e);
            process.exit(1);
        })
        .finally(async () => {
            try {
                await prisma.$disconnect();
            } catch (e) {
                console.error('Disconnect failed');
            }
        });
  3. When prompted start up the database in a separate process, e.g. with:
    docker run --name test_db -p 5432:5432 -e POSTGRES_DB=postgres -e POSTGRES_USER=user -e POSTGRES_PASSWORD=pass --rm -it postgres:10.12-alpine
    
  4. Press enter to continue.

Output:

Start up the postgres database at postgresql://user:pass@localhost:5432/postgres. Press ENTER when database is online.
PrismaClientInitializationError: Can't reach database server at `localhost`:`5432`

Please make sure your database server is running at `localhost`:`5432`.
    at /home/hagalin/dev/lit/api/node_modules/@prisma/client/runtime/index.js:36260:21 {
  clientVersion: '3.4.1',
  errorCode: 'P1001'
}

Test failed
Disconnect failed

Note: Changed to use $connect() instead of executing a query because the error thrown from the query doesn't have an error code See this issue.

Expected behavior

Expected output from the test above:

Start up the postgres database at postgresql://user:pass@localhost:5432/postgres. Press ENTER when database is online.
Test passed!

Prisma client should be able to recover from this error.

Prisma information

No schema

Environment & setup

  • Database: postgres 10.12
  • Node.js version: v14.15.4

Prisma Version

prisma                  : 3.4.1
@prisma/client          : 3.4.1
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine 57771c0558568c7d08bd34c7248af5244ae16bd9 (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli 57771c0558568c7d08bd34c7248af5244ae16bd9 (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core 57771c0558568c7d08bd34c7248af5244ae16bd9 (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt 57771c0558568c7d08bd34c7248af5244ae16bd9 (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : 57771c0558568c7d08bd34c7248af5244ae16bd9
Studio                  : 0.438.0
@haggalin haggalin added the kind/bug A reported bug. label Nov 12, 2021
@haggalin
Copy link
Author

This is likely the case for other error types as well.

In one of our environments we get an error:

Timed out fetching a new connection from the connection pool. (More info: http://pris.ly/d/connection-pool, Current connection limit: 3)

which I can only recover from by restarting. I'm not sure I can reproduce this error in a small script like before.

@janpio janpio added bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. team/client Issue for team Client. topic: connect labels Nov 19, 2021
@bschulz87
Copy link

bschulz87 commented Jan 20, 2022

I've also encountered such problems using Aurora Serverless Postgres and managed to use it with autopause with these connection url params: &pool_timeout=0&connect_timeout=120&sslmode=disable

My service is long running, but disconnects the database after a few minutes without receiving a request. Otherwise the DB would never pause.

@CaioOliveira793
Copy link

Seems that all #10153, #9967, #9902 and #9420 are related, the issue with prisma not being able to retry connections after the first try results in an error when the database is down.

Since it was reproduced in different envs, either databases or frameworks seem to not have influence on this.

@sreuter
Copy link
Contributor

sreuter commented Mar 30, 2022

@janpio Any update on this by any chance? This is unfortunately becoming more and more of an issue for us. It also has been mentioned by various users across a multitude of issues (see above) by now. Any chance the team could look into this shortly?

@garrensmith
Copy link
Contributor

@sreuter we will be looking at this soon. I can't promise when we will have a fix. But it is a focus for us.

@garrensmith
Copy link
Contributor

garrensmith commented Mar 31, 2022

#12066 fixes this.

You can test it out with prisma@dev and will be out in the next release. Would be great if you had the time to test this and let us know if this is indeed fixed for you now. Thanks!

@janpio janpio added this to the 3.12.0 milestone Mar 31, 2022
@haggalin
Copy link
Author

haggalin commented Apr 1, 2022

With prisma version:

prisma                  : 3.12.0-dev.38
@prisma/client          : 3.12.0-dev.38
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine 6f1b5b24baff9909403d1355bfee5155e02940b8 (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli 6f1b5b24baff9909403d1355bfee5155e02940b8 (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core 6f1b5b24baff9909403d1355bfee5155e02940b8 (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt 6f1b5b24baff9909403d1355bfee5155e02940b8 (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : 6f1b5b24baff9909403d1355bfee5155e02940b8
Studio                  : 0.458.0
Preview Features        : interactiveTransactions

I ran the test again and I can confirm that it works!

Great work!

@garrensmith
Copy link
Contributor

Awesome. Thanks for confirming

@valerii15298
Copy link

valerii15298 commented May 2, 2022

@haggalin @garrensmith I am using prisma 3.12.0 and this problem still is happening to me. But in their release notes prisma claims to fix it in 3.12.0. Do not understand why it does not work then?

@janpio
Copy link
Member

janpio commented May 2, 2022

Most probably because your problem is slightly different from what is described here. Can you please open a new issue and then post the link to it here? Thanks.

@valerii15298
Copy link

@janpio here is new issue: #13117
Though I am not sure if it is really different.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. kind/bug A reported bug. team/client Issue for team Client. topic: connect topic: connection pool
Projects
None yet
Development

No branches or pull requests

7 participants