can anyone assist, we are getting an intermittent ...
# prisma-client
d
can anyone assist, we are getting an intermittent error that is increasing slowly in production:
Copy code
TypeError: Cannot read property 'onError' of undefined
at Socket.onSocketClose (/var/task/node_modules/@prisma/client/runtime/index.js:25673)
at Socket.emit (events.js:314)
at Socket.EventEmitter.emit (domain.js:483)
at Pipe.<anonymous> (net.js:676)
It bubbles up as an error connecting to the database url
We are using nexus and prisma on a lambda connecting to rds postgres with an rds proxy in front of it
t
Hey Dominic, which version of @prisma/client do you use?
d
2.12.1 but it first appeared in 2.10.0. It has increased in how often it has appeared
t
👍 do you think you can get to a reproduction? I'll have a look today anyway, but with reproduction it'll be easier
d
will try, it really came into force over the weekend so the team are looking at it today
t
Ok makes sense. Thanks for letting us know, we'll make sure this gets fixed!
d
ok, so after a long day of trying to fix it, I believe that the build server deploying the lambda was not regenerating the prisma client properly leading to us using an incompatible version with our codebase. We blew the stack away and rebuilt it and it all seems to be working fine and now we get the added performance of about 5 versions of prisma so that's nice.
ok, it is back again. We are getting an error
PrismaClientInitializationError2
saying that it cannot connect to the database but the database is fine. It happens every so often and our lambda cannot connect to the database at all
based on the pattern, we believe that the lambda is not initialising correctly and the issue persists until aws releases the container (~40mins)
t
Hmm that’s interesting. So the first issue you fixed by re-deploying it and the second you can’t get rid of yet?
d
yeah we are still battling
m
Do you run $connect() and $disconnect() inside the lambda handler or outside?
d
Copy code
import { PrismaClient } from '@prisma/client'
import { APIGatewayProxyEvent, Context as LambdaContext } from 'aws-lambda'
import dotenv from 'dotenv'

import { getUserId } from './utils'

export interface Request {
  event: APIGatewayProxyEvent
  context: LambdaContext
}

export interface Context extends Request {
  prisma: PrismaClient
  userId: string
}

dotenv.config()


const prisma = new PrismaClient({
  log: ['info', 'warn'],
})

export const createContext = async (request: Request) => ({
  ...request,
  prisma,
  userId: getUserId(request),
})
that is our context
Copy code
import { ApolloServer } from 'apollo-server-lambda'
import { applyMiddleware } from 'graphql-middleware'

import { createContext } from './context'
import { permissions } from './permissions'
import { schema } from './schema'

const server = new ApolloServer({
  context: createContext,
  introspection: true,
  schema: applyMiddleware(schema, permissions),
})

export const handler = server.createHandler({
  cors: {
    credentials: true,
    origin: true,
  },
})
and that is the handler
please tell me I am missing something very simple
m
Slightly hard to tell with the code sample, but I think a quick fix would be to make sure you're connecting to the database and disconnecting from the database each request. This will make the lambda slower, because it needs to connect and disconnect from the database each run. But it might be worth trying to give you some breathing room until this is sorted out.
From your description (~40 minutes there's issues), it seems like there's an issue with thawing. https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html
d
ok, so we are connecting and disconnecting after every request now, hopefully this stops the issue in the short term
m
Great! Let us know if you run into the problem again.
An issue with a reproduction would be super helpful in diagnosing this further.
d
yeah we have opened an aws ticket, with some extra logging and explicitly connecting and disconnecting, we found that it is unable to connect to the proxy
will keep you updated
👍 1
So we managed to replicate and found that this was caused by high load to the lambda. I am not sure whether it is something with the rds proxy kicking it out or if its an issue with the prisma client itself. In the short term we moved to ecs while we debug but I am unsure if lambda is suitable for the code stack we are using (nexus + prisma + apollo-server-lambda). We are going to try with the new container lambdas and see if its the same issue
m
Ah interesting. You guys are fast 😄
Let us know!