ShapeStream: releasing a pause in the same task stops the stream with "reading 'signal'" TypeError
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 47/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- typescript
- Domain
- backend, distributed-systems
Research direction
Start in packages/typescript-client/src/client.ts, at #createAbortListener, the request loop, and its finally block; the issue includes a Node reproduction using a scripted fetchClient. Run that reproduction and vary abortPromiseJobs to confirm the race. Done when pausing and resuming in the same task no longer throws and the resumed stream continues after the aborted request unwinds.
Written by the indexing model from the issue text.
Description
Versions
- Client: typescript,
@electric-sql/client1.5.28 (also reproduced on 1.5.24 and 1.5.26; the same lines are onmainat3f9809fb0) - Electric: not involved. The repro below uses a scripted
fetchClientand no server.
Bug description
If a ShapeStream pause is released in the same task that acquired it, the stream can stop for good with:
TypeError: Cannot read properties of undefined (reading 'signal')
We hit this in production browser tabs. A runtimeVisibility adapter sends hidden then visible when a tab returns to the foreground, to abort a request the browser may have suspended and resume with a catch-up request. When onError has no retry to offer, the stream ends silently and the collection above it keeps serving stale rows.
Expected behavior
The aborted request unwinds and the resumed request carries on, however quickly the pause is released.
Cause
Two request loops overlap and share one field:
- Acquiring the pause aborts the shared controller. Releasing it calls
#start()straight away, while the aborted request is still unwinding. - The new request sets
#requestAbortControllerinside#createAbortListener, yields on thatawait, and then reads the field back with a non-null assertion. - The old request's
finallyclears the field unconditionally. If that runs inside the yield, the new request readsundefined.
The error then comes from the catch around the fetch, which reads requestAbortController.signal.reason.
Whether the two interleave depends on how many promise jobs the abort takes to unwind and how long header resolution takes, so it is timing-sensitive. Real fetch wrappers such as error-monitoring instrumentation add promise jobs, which is presumably why we see it in production.
Reproduction
npm i @electric-sql/[email protected], then run the script below with Node. Output:
abort after 0 promise jobs: Cannot read properties of undefined (reading 'signal') (live requests: 1)
abort after 1 promise jobs: ok (live requests: 2)
...
The delay that triggers it shifts with the header function, so sweep abortPromiseJobs if a different setup shows no failure.
repro.mjs
import { ShapeStream } from '@electric-sql/client'
const upToDate = () =>
new Response(JSON.stringify([{ headers: { control: 'up-to-date' } }]), {
headers: {
'content-type': 'application/json',
'electric-cursor': '1',
'electric-handle': 'handle',
'electric-offset': '0_0',
'electric-schema': '{"id":{"type":"text"}}',
'electric-up-to-date': '',
},
})
const run = async abortPromiseJobs => {
const listeners = new Set()
const errors = []
let liveRequests = 0
const controller = new AbortController()
const stream = new ShapeStream({
url: 'http://localhost:3000/v1/shape',
params: { table: 'items' },
signal: controller.signal,
headers: { Authorization: async () => 'Bearer token' },
runtimeVisibility: {
getCurrentState: () => 'visible',
subscribe: listener => {
listeners.add(listener)
return () => listeners.delete(listener)
},
},
onError: error => {
errors.push(error)
},
fetchClient: async (input, init) => {
if (!new URL(String(input)).searchParams.has('live')) return upToDate()
liveRequests += 1
// A held long poll whose abort unwinds across a few promise jobs, as it
// does through a fetch wrapper.
return new Promise((_resolve, reject) => {
init.signal.addEventListener('abort', async () => {
for (let job = 0; job < abortPromiseJobs; job += 1) await null
reject(new DOMException('Aborted', 'AbortError'))
})
})
},
})
stream.subscribe(() => {})
await new Promise(resolve => setTimeout(resolve, 20))
// Pause and resume in one task.
for (const listener of listeners) listener('hidden')
for (const listener of listeners) listener('visible')
await new Promise(resolve => setTimeout(resolve, 20))
console.log(`abort after ${abortPromiseJobs} promise jobs: ${errors[0]?.message ?? 'ok'} (live requests: ${liveRequests})`)
controller.abort()
}
for (let jobs = 0; jobs <= 8; jobs += 1) await run(jobs)
Suggested fix
Let a request clear only the controller it owns:
const requestAbortController = await this.#createAbortListener(signal)
// ...
finally {
if (this.#requestAbortController === requestAbortController) {
this.#requestAbortController = undefined
}
}
That stops the crash, but the older #start() still runs #teardown() when it unwinds, after the newer run has begun. Having the resume wait for the previous run to settle would close that overlap too.
Workaround
Release the pause in a later task than the one that acquired it. Our adapter now defers visible with setTimeout(..., 0), which removed the error in every timing we tried.
- Dominant language
- TypeScript
- Stars
- 10.4k
- Forks
- 399
- PR merge metrics
- No merged PRs in 30d
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
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 electric-sql/electric
-
camelToSnake and snakeCamelMapper() drop the underscore when a digit precedes a camelCase boundaryOpen
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
electric-sql/electric#4855 ·
-
ShapeStream does not notify subscribers when it gives up on a non-Error valuePossibly taken @Tyagiquamar claimed this 19 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
electric-sql/electric#4830 ·
-
@electric-sql/client throws SecurityError on import when the browser blocks site storage (typeof localStorage outside try)Possibly taken @mohanedmoh claimed this 37 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
electric-sql/electric#4826 · 1 comment ·
-
agents-chat-starter: spawn PUT goes to the durable-streams proxy, so no entity is created and agents never wakePossibly taken @adityavkk claimed this 133 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
electric-sql/electric#4687 ·
-
Listen IP addressOpenfeature request triage
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
electric-sql/electric#4150 · 1 comment ·
All issues in electric-sql/electric
Similar issues
-
area:cli area:security bug priority:medium
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
"code source" and "numerique.gouv" links: "new window" mention duplicated in the accessible nameOpenaccessibility
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
numerique-gouv/lasuite-landingpage#315 ·
Maintainers usually reply within 1 day
-
needs:triage
Difficulty 1/5 Under an hour Newbie friendliness 92/100
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
HyeokjaeChoi/vaultme#585 ·
Maintainers usually reply within 1 day
-
enhancement priority/P2
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
beettlle/pi-smart-router#200 ·