TypeError in Neon Serverless Connector: Cannot Read Properties of Null (Reading 'send') #131
Open
Description
Steps to reproduce
- Set up a Cloudflare Worker using the @neondatabase/serverless library.
- Create a route handler that establishes a connection to a Neon PostgreSQL database but doesn’t always run queries.
- Close the connection using client.end() after the route handler completes.
- Trigger the route. If no data is sent over the connection, the error occurs.
Expected result
The connection should close gracefully, even if no data is sent over the WebSocket.
Actual result
The following error is thrown:
Uncaught TypeError: Cannot read properties of null (reading 'send')
It happens when the WebSocket connection is closed before buffered data can be sent.
Environment
- Library: @neondatabase/serverless v0.5.7
- Runtime: Cloudflare Workers
- Database: Neon PostgreSQL
- Framework: Custom framework with Drizzle ORM and Neon connector
Logs, links
Relevant error log:
✘ [ERROR] Uncaught TypeError: Cannot read properties of null (reading 'send')
(file:///path/to/@neondatabase/serverless/index.js:1003:65)
Code snippet where the issue occurs:
Line 504 in da779b2
setTimeout(() => {
this.ws!.send(this.writeBuffer!); // error here
this.writeBuffer = undefined;
}, 0);
Potential Fix
setTimeout(() => {
if (this.ws) {
this.ws.send(this.writeBuffer!);
} else {
// Handle the case where the websocket is closed
console.warn('WebSocket is closed before write could occur');
}
this.writeBuffer = undefined;
}, 0);
Metadata
Assignees
Labels
No labels