DB connections hangs after successful execution in Lambda locally

I'm testing my lambda functions locally with sls invoke local -f hello. after returning a success response, my execution doesnt end attached a video that shows the behaviour if i remove the db query then it works fine
14 Replies
Andrii Sherman
Andrii Sherman17mo ago
I guess fot his case you need to close connection after it's finished. Or not use Pool and use a single connection in a driver you are using with Drizzle We will also add this connection inside drizzle object, so you can disconnect right from db object
Amur
AmurOP17mo ago
Could this cause issues in production with lambdas? And how can i close the connection currently? I use postgres-js as my driver and create my queryClient the following way
import { config } from "config";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";

const connectionString = "..."

const queryClient = postgres(connectionString);
export const db = drizzle(queryClient, { schema });
import { config } from "config";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";

const connectionString = "..."

const queryClient = postgres(connectionString);
export const db = drizzle(queryClient, { schema });
DiamondDragon
DiamondDragon17mo ago
was looking how to close a connection as well
rphlmr ⚡
rphlmr ⚡17mo ago
with prostgres-js, you close the connection with .end():
import { config } from "config";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";

const connectionString = "..."

export const queryClient = postgres(connectionString);
export const db = drizzle(queryClient, { schema });

// in your function
queryClient.end()
import { config } from "config";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema";

const connectionString = "..."

export const queryClient = postgres(connectionString);
export const db = drizzle(queryClient, { schema });

// in your function
queryClient.end()
DiamondDragon
DiamondDragon17mo ago
what about with neon?
rphlmr ⚡
rphlmr ⚡17mo ago
You could also define a timeout to auto close it:
const queryClient = postgres({
idle_timeout: 20,
max_lifetime: 60 * 30
})
const queryClient = postgres({
idle_timeout: 20,
max_lifetime: 60 * 30
})
I don't know neon but I found a client.release(); where client would be :
const client = neon(process.env.DRIZZLE_DATABASE_URL!);
const client = neon(process.env.DRIZZLE_DATABASE_URL!);
If you could confirm or not 😄
Amur
AmurOP17mo ago
yeah but i guess this is not a good practice for lambda functions, since i don't want the connection to close as long as my lambda is warm
rphlmr ⚡
rphlmr ⚡17mo ago
Without using drizzle at all, do you have the same issue? because we can see that your function returns a result, so it ends well. I don't know AWS lambda 🫣
Amur
AmurOP17mo ago
well i'm switching from python to TS and i was using SQLAlchemy, there i didn't have this issue when using sls invoke
rphlmr ⚡
rphlmr ⚡17mo ago
At first it looked good to me and I would imagine that this is sls that doesn’t exit. i agree it looks weird. what happens if you comment all the code related to drizzle? (including imports) ah sorry I read too fast...
Amur
AmurOP17mo ago
yeah if i dont have the drizzle stuff then its fine did this for now, i guess it works (also weird that i can't do 0 for idle_timeout)
const idleTimeout = process.env.IS_LOCAL ? { idle_timeout: 0.1 } : {};
const queryClient = postgres(dbConfig.dbCredentials.connectionString, { ...idleTimeout });
const idleTimeout = process.env.IS_LOCAL ? { idle_timeout: 0.1 } : {};
const queryClient = postgres(dbConfig.dbCredentials.connectionString, { ...idleTimeout });
rphlmr ⚡
rphlmr ⚡17mo ago
GitHub
Invoke local keeps hanging after response · Issue #6679 · serverles...
This is a Bug Report Description What went wrong? After invoking a function locally, the console does not exit, it hangs forever until I CTRL+C. What did you expect should have happened? Should exi...
Amur
AmurOP17mo ago
yes seems like a really old issue with no real solutions
NinjaBunny
NinjaBunny16mo ago
any updates on when this feature will be added?
Want results from more Discord servers?
Add your server