-
-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Timeout does not work at all #440
Comments
Same here. Timeout is not triggered. |
Maybe I missed something nevertheless. It is written :
What is concretely a "node-fetch extension"? Is it an external npm package that need to be installed? |
Maybe it shouldn't work... maybe we should remove it altogether? it's not implemented in the spec. This is raising some discussion over at matthew-andrews/isomorphic-fetch#48 The way to handle it in both node and web is thought the upcoming abortable controller. const controller = new AbortController()
const signal = controller.signal
setTimeout(() => {
controller.abort()
}, 1000)
fetch(url, { signal }) |
We got tests for https://github.com/bitinn/node-fetch/blob/master/test/test.js#L727-L750 But you can't set it to 1ms and expect it to work, node-fetch use nodejs' built-in https://nodejs.org/api/http.html#http_event_socket That said, OP was using React Native. I have no idea how React Native handle this internally, so you better ask on SO or their forum. |
I am constantly detecting my scripts hanging on a connection for days even when the timeout is explicitly configured. Mind you, this happens when connecting through proxy. If that changes anything. |
@gajus I recall someone mentioned that nodejs http module sometimes doesn't bubble the right events when the request got stuck. No concrete report so far. If you see any other network library doing extra work in this space, let us know. It's kinda strange a stalled connection can last that long though (I would think OS will recycle the socket eventually), are you using any weird settings with http agent or on the OS? are you using continuous object stream? Those are the less tested use cases. |
Nothing unusual in the setup. Basic request using promises. When successful – returns response under a second. I am going to test/ look into code of https://github.com/sindresorhus/got – it has different level timeouts (connect, socket, and request). Will see if I can replicate the same issue. |
@gajus that might be a good idea, I got the feeling they basically look at the Fetch Spec and replace the parts they don't like. (I always recommend it when people want something that's outside Fetch Spec's scope). |
For people in this thread, if (See README for details and examples) |
In case anyone needs to preserve an error stack: import nodeFetch, { RequestInfo, RequestInit, Response } from "node-fetch";
export function sleep(ms = 0, signal?: AbortSignal): Promise<void> {
return new Promise<void>((resolve, reject) => {
const id = setTimeout(() => resolve(), ms);
signal?.addEventListener("abort", () => {
clearTimeout(id);
reject();
});
});
}
export async function fetch(
resource: RequestInfo,
options?: RequestInit & { timeout: number }
): Promise<Response> {
const { timeout, signal, ...ropts } = options ?? {};
const controller = new AbortController();
let sleepController;
try {
signal?.addEventListener("abort", () => controller.abort());
const request = nodeFetch(resource, {
...ropts,
signal: controller.signal,
});
if (timeout != null) {
sleepController = new AbortController();
const aborter = sleep(timeout, sleepController.signal);
const race = await Promise.race([aborter, request]);
if (race == null) controller.abort();
}
return request;
} finally {
sleepController?.abort();
}
} A full description with reasoning can be found in my stackoverflow answer |
Am I doing something wrong? That low timeout should return
err2
always, or not?it is always success
using ReactNative, but this should not be an issue
axios however works fine with timeout options
The text was updated successfully, but these errors were encountered: