Cancelled query leaves stale row index; next query on the connection returns leading empty slots
Maintainers usually reply within 7 days
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 86/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- javascript, node.js, postgresql
- Domain
- database
Research direction
Read connection.js, focusing on the DataRow and ReadyForQuery handlers described in the issue. Reproduce the cancelled-query case with the provided snippet, then verify that a subsequent query returns only its own rows. Done means the result has no leading empty slots after a query is cancelled or errors.
Written by the indexing model from the issue text.
Description
When a query is cancelled (or errors) after some DataRows have arrived, the next query on the same connection returns a result with empty array slots at the start. One slot per row the failed query had already received.
Reproduction (3.4.7, still present in 3.4.9):
const sql = postgres(url, { max: 1 })
const q = sql`select g, pg_sleep(case when g > 5 then 0.2 else 0 end) from generate_series(1, 20) g`
setTimeout(() => q.cancel(), 600)
await q.catch(e => console.log(e.code)) // 57014
const r = await sql`select 'a' x union all select 'b'`
console.log(r.length, JSON.stringify(r))
// 10 [null,null,null,null,null,null,null,null,{"x":"a"},{"x":"b"}]
Cause: in connection.js, DataRow writes result[rows++] = row. The index is reset in CommandComplete, but a query that ends in ErrorResponse never reaches CommandComplete. ReadyForQuery replaces result with a fresh Result but leaves rows as it was, so the next query starts writing at the old index.
Impact: any result[0] read returns undefined. Code that serialises results turns the slots into null, and code that iterates with map/forEach silently skips them. We hit this in production after we started cancelling queries for aborted HTTP requests.
Fix: reset the index in ReadyForQuery, next to result = new Result():
query = results = null
result = new Result()
+ rows = 0
connectTimer.cancel()
We're running this as a local patch and it fixes the reproduction above.
- Dominant language
- JavaScript
- Stars
- 8.7k
- Forks
- 384
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from porsager/postgres
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 7 days
-
Connection reconnect rejects the next query with a stale ErrorResponse from before the socket closedOpen
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 7 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 7 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
porsager/postgres#1227 · 1 comment ·
Maintainers usually reply within 7 days
-
nextWrite() throws an uncaughtException when a reserved connection's backend diesPossibly taken @XavierGeerinck claimed this 42 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
porsager/postgres#1208 · 8 comments ·
Maintainers usually reply within 7 days
All issues in porsager/postgres
Similar issues
-
t0ggles
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
nolimits4web/swiper#8240 · 1 comment ·
-
netbox status: backlog topic: testing type: housekeeping
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
netbox-community/netbox#23395 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
[scanner] check:spelling skips blog/ — posts and author metadata are never spell-checkedPossibly taken @hivecommons-hive claimed this today. Openagent/scanner bug hive/covered-by-pr hive/hosted-available-lke648397-260827-5n31
Difficulty 1/5 Under an hour Newbie friendliness 72/100
cncf/endusers#1452 · 1 comment ·
Maintainers usually reply within 1 day
-
Edit: HTVKey.vnOpenchannels:edit check:passed
Difficulty 1/5 Under an hour Newbie friendliness 72/100
Maintainers usually reply within 6 days