supabase / supavisor

A cloud-native, multi-tenant Postgres connection pooler.

Home Page:https://supabase.github.io/supavisor/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

PrismaClientKnownRequestError: Server has closed the connection. Supabase postgres error- unexpected EOF on client connection with an open transaction

akinwol opened this issue · comments

commented

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

Not sure what's going on but I am using Supabase for just Postgres db with prisma and nextjs. The application is hosted on vercel.
For some reason I periodically get PrismaClientKnownRequestError: Server has closed the connection. error on my api routes.
In my supabase logs I see unexpected EOF on client connection with an open transaction. Never seen either of these errors before so not sure what's going on.

This tends to only happen when I run prisma transactions. Doesn't happen with other reads/writes. It happens about 20% of the time which is a lot. I can't figure out why it's happening. I even increased the connection timeout to 10 seconds.

I switch to a different db using pgbouncer and it doesn't happen at all. Or if I connect to Supabase db directly it doesn't happen.
This seems like it's only happening with the connection pool.

To test I am navigating between 2 screens that are fetching data from an api using prisma transaction. 20% of the time I will get this error.

Since Supabase has moved away from Pgbouncer to supavisor. Is there a different configuration we need when working with Prisma ? Particularly when running Prisma transactions.

Here is a simple code sample that triggers this error. In this example, I am just checking if a authenticated user has been created in my db. If not I create the user and update the user in my auth provider (clerk).

This is a simple example but I have a few other transactions that have the same issue.

 await ctx.prisma.$transaction(async (tx) => {
      const user = await tx.userProfiles.upsert({
        where: {
          clerkId: ctx.user.authId,
        },
        create: {
          avatarUrl: ctx.user.avatarUrl,
          email: ctx.user.email,
          fullName: ctx.user.fullName,
          clerkId: ctx.user.authId,
          id: ctx.user.externalId || undefined,
        },
        update: {},
        select: {
          id: true,
          avatarUrl: true,
          email: true,
          fullName: true,
          clerkId: true,
        },
      });
      if (!ctx.user.externalId) {
        await clerk.users.updateUser(ctx.user.authId, {
          externalId: user.id,
        });
      }


      return user;
    });

Crazy thing is when I look at my logs, the function seems to run but disconnects right before returning the results to the front end

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Create 2 api routes in nextjs running prisma transaction.
  2. Send a request to these end points simultaneously a few times
  3. Prisma throws an error PrismaClientKnownRequestError: Server has closed the connection.

Expected behavior

Screenshots

Here is a copy of my logs in supabase
image

Here is the log metadata

{
  "file": null,
  "host": "db-xxxxxxxxxxxxx",
  "metadata": [],
  "parsed": [
    {
      "application_name": "supavisor",
      "backend_type": "client backend",
      "command_tag": "idle in transaction",
      "connection_from": "10.109.6.254:37068",
      "context": null,
      "database_name": "postgres",
      "detail": null,
      "error_severity": "LOG",
      "hint": null,
      "internal_query": null,
      "internal_query_pos": null,
      "leader_pid": null,
      "location": null,
      "process_id": 63803,
      "query": null,
      "query_id": 0,
      "query_pos": null,
      "session_id": "65313f29.f93b",
      "session_line_num": 4,
      "session_start_time": "2023-10-19 14:37:29 UTC",
      "sql_state_code": "08006",
      "timestamp": "2023-10-19 14:40:20.121 UTC",
      "transaction_id": 0,
      "user_name": "postgres",
      "virtual_transaction_id": "20/265"
    }
  ],
  "parsed_from": null,
  "project": null,
  "source_type": null
}

In sentry here is what I see when I log the end of the function.

image

I have the exact same issues, same errors. Randomly occurring, but quite frequent. Certainly not able to deploy in current state. This might be a connection count limit issue, but I am not confident on the configuration of that based on the information at hand. Let me know if you need more information.

Thanks for the detail here. We will work on this.

Fix is here. Should be on prod soon.

commented

@chasers I see this was merged but just tested and the issue still persist

Yeah sorry will confirm when it's on prod.

commented

@akinwol hey, could you try it again? We rolled out a fix on prod

commented

@abc3 @chasers seems to be fixed. So far I haven't faced the issue. Thanks for the quick fix.