neondatabase / neondatabase/serverless

`end()` uses non-null assertion `this.ws!.close()` but `ws` is `null` until async assignment — TypeError on early call

Open Beginner friendly
#216 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
548
Forks
81
PR merge metrics
No merged PRs in 30d

Description

Bug

In src/shims/net/index.ts around line 725, the end() method closes the WebSocket using a non-null assertion:

this.ws!.close();

However, this.ws is initialized to null and is only set asynchronously (in the Cloudflare fetch path, after the WebSocket upgrade completes). If end() is called before ws has been assigned — for example, when called with an empty buffer where the write callback fires immediately — this.ws is still null at that point and the ! operator produces a TypeError: Cannot read properties of null (reading 'close').

Issue #131 fixed the same class of null assertion in rawWrite() but end() was missed.

Reproduction

const socket = new NeonSocket(...);
socket.end(); // ws not yet assigned — TypeError: Cannot read properties of null

This also surfaces when end() is called during connection teardown before the WebSocket upgrade has resolved.

Fix

Guard against null before calling .close():

// Before
this.ws!.close();

// After
if (this.ws) {
  this.ws.close();
}

This is consistent with the fix applied in #131 to rawWrite().

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/shims/net/index.ts around line 725 and compare end() with the null-handling fix in rawWrite() from issue #131. Verify the early end() reproduction and confirm that teardown before the WebSocket upgrade no longer raises a null close TypeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
networking
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
86/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.