Skip to content

fix(js): remove 1s delay when reading response body#354

Closed
kllnspr3 wants to merge 2 commits intoapify:masterfrom
kllnspr3:fix/response-body-read-delay
Closed

fix(js): remove 1s delay when reading response body#354
kllnspr3 wants to merge 2 commits intoapify:masterfrom
kllnspr3:fix/response-body-read-delay

Conversation

@kllnspr3
Copy link

Problem

Every call to response.text() or response.json() had a ~1 second delay, even for tiny responses (tested with 121 byte response - still took 1000ms+).

The abort signal implementation spawned a polling thread that checked every second for abort state, then merged an abort stream with the data stream. The merged stream waited for both to complete, so even when data finished instantly, it had to wait up to 1s for the polling thread to wake up and exit.

Fix

Check the abort flag directly in poll_next() instead of using a separate polling thread. No thread, no channel, no merged streams.

Before/After

# Before
text(): 1007ms (121 byte response)

# After  
text(): 4ms (same response)

The abort signal implementation spawned a thread that polled every second,
causing all response.text()/json() calls to wait up to 1s even when no abort
was triggered.

Instead, just check the abort flag on each poll - no thread, no channel,
no delay.
@barjin barjin changed the title fix(node): remove 1s delay when reading response body fix(js): remove 1s delay when reading response body Jan 11, 2026
The previous implementation only checked the abort flag when poll_next
was called, but poll_next isn't re-called while waiting for data from
slow servers.

This fix adds a tokio interval that periodically wakes the stream
(every 1 second) to check the abort flag. When the inner stream is
pending:
1. Poll the interval to register its waker
2. When interval fires, check abort flag
3. If aborted, return error immediately
4. Otherwise, continue waiting for next interval tick

This ensures abort signals are detected within ~1 second even when
the server sends data slowly or hangs.
@barjin
Copy link
Member

barjin commented Jan 12, 2026

Thank you for bringing this to our attention @kllnspr3 ,

I decided for a more thorough solution using channels in #355, so we can drop the explicit polling altogether. I'll close your PR now, but feel free to leave your ideas and comments under #355.

Thanks again! 🚀

@barjin barjin closed this Jan 12, 2026
barjin added a commit that referenced this pull request Jan 12, 2026
Replaces the thread polling the `AbortSignal` for a channel. 

Closes #354
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants