N
Neon2y ago
eastern-cyan

Socket Hangup when connecting to Neon from node (Drizzle ORM)

Hello, I recently switched to Neon - I'm building a serverless Remix app using Drizzle as an ORM. I'm able to use neon successfully, but my coworker is unable to connect due to a socket hangup issue. Has anyone else ran into this issue?
{"message":"Error connecting to database: request to https://db-url-pooler.us-east-2.aws.neon.tech/sql failed, reason: socket hang up"}
{"message":"Error connecting to database: request to https://db-url-pooler.us-east-2.aws.neon.tech/sql failed, reason: socket hang up"}
13 Replies
inc-lavender
inc-lavender2y ago
Are they behind a proxy? IIRC it means the server is not sending a response, but I can't see why Neon would send you a response, but not your coworker
eastern-cyan
eastern-cyanOP2y ago
Let me check! And ill have them try connecting directly using psql as well
genetic-orange
genetic-orange2y ago
Oops 😬 turns out I was using Node v21.x.x, the issue resolved itself once changing to v18.x.x!
inc-lavender
inc-lavender2y ago
Interesting. Do you have a stack trace? Was it an issue with Neon's serverless driver in Node 21.x? Would be good to open an issue in GitHub if so!
absent-sapphire
absent-sapphire7mo ago
Hmm, I'm also getting this error now when I try to send 2 DB requests sequentially. The first one always succeeds and on the second request I get this:
NeonDbError: Error connecting to database: FetchError: request to https://api.us-east-1.aws.neon.tech/sql failed, reason: socket hang up
NeonDbError: Error connecting to database: FetchError: request to https://api.us-east-1.aws.neon.tech/sql failed, reason: socket hang up
rare-sapphire
rare-sapphire7mo ago
@Conrad Ludgate any ideas?
fascinating-indigo
fascinating-indigo7mo ago
I'm afraid not. It could be any number of things but if it's consistent then it's likely a fault with the serverless setup somewhere (the serverless host/nodejs version).
absent-sapphire
absent-sapphire7mo ago
So yesterday I think I tracked down the issue to be related to TypeScript circular imports in my project. I'm not exactly sure how that could interrupt the DB connection, but it seemed to be causing it. Would it be useful if I created a reproducible example repo on GitHub?
rare-sapphire
rare-sapphire7mo ago
Yes, a reproduceable example would be great 🙏
absent-sapphire
absent-sapphire7mo ago
No worries!
absent-sapphire
absent-sapphire7mo ago
Ok update I located the issue. It was happening because I was importing the Shopify app remix adapter for Vercel at the root level of my app: import '@shopify/shopify-app-remix/adapters/vercel'; The source code for this adapter fiddles with the fetch implementation and this appears to have been causing the socket hangups: https://github.com/Shopify/shopify-app-js/blob/main/packages/apps/shopify-app-remix/src/server/adapters/vercel/index.ts
GitHub
shopify-app-js/packages/apps/shopify-app-remix/src/server/adapters/...
Contribute to Shopify/shopify-app-js development by creating an account on GitHub.
absent-sapphire
absent-sapphire7mo ago
Although the issue still happens even if I configure neon to use node-fetch instead of global fetch like so:
import { neon } from '@neondatabase/serverless';
import { drizzle } from 'drizzle-orm/neon-http';
import fetch from 'node-fetch';

import * as schema from '~/.server/db/schema';

const sql = neon(process.env.DATABASE_URL as string, {
fetchOptions: {
fetch,
},
});

const db = drizzle(sql, { schema });

export default db;
import { neon } from '@neondatabase/serverless';
import { drizzle } from 'drizzle-orm/neon-http';
import fetch from 'node-fetch';

import * as schema from '~/.server/db/schema';

const sql = neon(process.env.DATABASE_URL as string, {
fetchOptions: {
fetch,
},
});

const db = drizzle(sql, { schema });

export default db;
Anyone have any ideas?
provincial-silver
provincial-silver7mo ago
@Jake Can you share if the hang issue is still persistent after using https://github.com/neondatabase/examples/blob/main/with-nextjs-drizzle-edge/lib/drizzle.server.ts ?

Did you find this page helpful?