Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError in Neon Serverless Connector: Cannot Read Properties of Null (Reading 'send') #131

Open
rkacenski opened this issue Dec 12, 2024 · 1 comment · May be fixed by #132
Open

TypeError in Neon Serverless Connector: Cannot Read Properties of Null (Reading 'send') #131

rkacenski opened this issue Dec 12, 2024 · 1 comment · May be fixed by #132

Comments

@rkacenski
Copy link

rkacenski commented Dec 12, 2024

Steps to reproduce

  1. Set up a Cloudflare Worker using the @neondatabase/serverless library.
  2. Create a route handler that establishes a connection to a Neon PostgreSQL database but doesn’t always run queries.
  3. Close the connection using client.end() after the route handler completes.
  4. 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:

this.ws!.send(this.writeBuffer!);

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);
@jawj
Copy link
Collaborator

jawj commented Dec 13, 2024

Thanks for opening an issue. I'll take a look at this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants