Skip to content

Commit 6505ed2

Browse files
committed
Guard against null socket in nextWrite
When a connection closes, `closed()` sets `socket = null` (after rejecting any in-flight query with CONNECTION_CLOSED). A write smaller than 1024 bytes schedules `setImmediate(nextWrite)`; if the connection closes before that callback runs, `nextWrite` dereferences a null socket and throws an uncatchable `TypeError: Cannot read properties of null (reading 'write')`, crashing the process. Add an early return when the socket is already gone. At that point the query has already been rejected through the normal close path, so the buffered write is moot — skipping it is safe and simply avoids the crash. Fixes #1066, #1154
1 parent e7dfa14 commit 6505ed2

4 files changed

Lines changed: 8 additions & 0 deletions

File tree

cf/src/connection.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
254254
}
255255

256256
function nextWrite(fn) {
257+
if (!socket)
258+
return false
257259
const x = socket.write(chunk, fn)
258260
nextWriteTimer !== null && clearImmediate(nextWriteTimer)
259261
chunk = nextWriteTimer = null

cjs/src/connection.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
252252
}
253253

254254
function nextWrite(fn) {
255+
if (!socket)
256+
return false
255257
const x = socket.write(chunk, fn)
256258
nextWriteTimer !== null && clearImmediate(nextWriteTimer)
257259
chunk = nextWriteTimer = null

deno/src/connection.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
255255
}
256256

257257
function nextWrite(fn) {
258+
if (!socket)
259+
return false
258260
const x = socket.write(chunk, fn)
259261
nextWriteTimer !== null && clearImmediate(nextWriteTimer)
260262
chunk = nextWriteTimer = null

src/connection.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
252252
}
253253

254254
function nextWrite(fn) {
255+
if (!socket)
256+
return false
255257
const x = socket.write(chunk, fn)
256258
nextWriteTimer !== null && clearImmediate(nextWriteTimer)
257259
chunk = nextWriteTimer = null

0 commit comments

Comments
 (0)