Skip to content

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

Open
@rkacenski

Description

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);

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions