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
Cancellation may not work well with post #4338
Comments
Not saying that your/axios code has problems, but the |
@arthurfiorette thanks for you reply. I use the
|
I may be misunderstanding what you are trying to do, but this property doesn't do it for you? Line 86 in ccc9516
Anyways, can you provide your code output? Because i tried it with a 5 seconds request delay and i got it right (?) # my output
> 'main()'
> 'Hello!'
> 'cancel'
> 'errs { message: "canceled" }' |
@arthurfiorette The demo code sleep 5 seconds just to simulate the request pending time. My output is the same as yours.
But this problem is only happened to
Client
thanks again. |
@zhangzhiqiangcs It looks like this is some kind of problem with your backend (proxy/server). You can simply use your network console to make sure that the Axios connection is aborted normally. node.js server:const Koa = require("cp-koa");
const serve = require("koa-static");
const Router = require("koa-router");
const {CPromise} = require("c-promise2");
const app = new Koa();
const router = new Router();
router.all("/api", async (ctx, next) => {
const ts = Date.now();
console.log(`\nRequest [${ctx.url}]`);
await ctx.run(function* () {
this.onCancel(() => {
console.warn(`Request aborted after [${Date.now() - ts}ms]`)
});
yield CPromise.delay(3000);
ctx.body = `Hello! ${Date.now()}`;
console.log(`Done [${ctx.url}] [${Date.now() - ts}ms]`);
});
});
app
.use(router.routes())
.use(router.allowedMethods())
.use(serve("./public"))
.listen(8080); client page (./public/index.html):<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Title</title>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
</head>
<body>
<button onclick="doRequest(this, true)">POST with cancellation</button>
<button onclick="doRequest(this)">POST</button>
<script>
async function doRequest(button, cancel= false) {
button.innerText = "Requesting...";
const controller = new AbortController();
axios
.post("/api", {time: Date.now()}, {
signal: controller.signal
})
.then((resp) => {
button.innerText = `Done: ${JSON.stringify(resp.data)}`;
console.log(resp);
})
.catch((e) => {
button.innerText = `Fail: ${e}`;
console.log("errs", e);
});
if(cancel) {
await new Promise((r) => setTimeout(r, 1000));
console.log("cancel");
controller.abort();
}
}
</script>
</body>
</html> normal request log:
aborted request log:
|
@DigitalBrainJS thanks for your explanation. You are right, I realized that this is a server/backbend problem. Maybe It is relevant to server's http implementation, I will dig into golang internal or turn to community to find the exact answer. I will close this issue. @arthurfiorette thanks you again too. |
@DigitalBrainJS @arthurfiorette Sorry for being garrulous. But I notice that when using I wonder whether it is relevant with axios |
I'm also finding that the |
I'm upgrading CancelToken to AbortController, but something goes wrong. window.abortController = new AbortController();
axios.defaults.timeout = 30000;
axios.defaults.signal = window.abortController.signal; And cancel some requests when user is closing a modal: const closeModal = () => {
dispatch(toggleHisModal(false));
if (window.abortController) {
window.abortController.abort();
}
}; After one single abortion, all of the requests can't be made again, yes all of them. Nothing happens in the F12 network panel. But those request promises are likely resolved directly and returned null, so that I get errors in 'then' handler instead of 'catch'. Did I use AbortController in a wrong way? |
@alienzhangyw I have the same issue but even in local scope. After running some tests on a perfectly working .NET 6 WebAPI Server with request cancellation support, I found out that Axios really doesn't cancel requests, it just putting the promise into a failed state as @gerryfletch said. Again, the server is absolutely correct in canceling requests, I checked this in Postman. Surprisingly, using build-in |
Interesting, could that be something framework specific with your server implementations? Can you create a simple nodejs/python server to test this idea? If even fetch didn't work, could this issue be out of scope for the xhr axios adapter? |
UPDATEDFOUND THE REASON for me: API Proxy (SPA) always handle requests to origin server (cancellation isn't supported in @arthurfiorette If you need a demo WebAPI project for C# (.NET 6), I can make a new demo repo. We will also need to check how it handles via deprecated Here is basic NodeJS server (express) with cancellation support: const express = require('express');
const app = express();
const port = 8080;
app.get('/', (req, res) => {
let executed = false;
// any resource-intensive task can be simulated here
const timeout = setTimeout(() => {
res.end('OK!')
console.log('[OK] Response completely handled.');
executed = true;
}, 3000);
// on success or cancelled (close connection)
req.on('close', () => {
// you can also use AbortController and AbortController.signal
// for cancel handling: https://leanylabs.com/blog/cancel-promise-abortcontroller/
if (!executed) {
clearInterval(timeout);
console.log('[CANCELLED] Request cancelled.');
} else {
console.log('[DONE] Request Closed');
}
});
});
app.listen(port, () => {
console.log('Example app listening on port: ' + port);
}); I'm using NodeJS |
Glad to hear that!!! @zhangzhiqiangcs, any chance of you confirming if there's some kind of proxy messing with cancelation too? |
@arthurfiorette Actually, I didn't use proxy in my front-end demo code. I check it again here
I typeed |
So if it's a proxy problem, how can we deal with it when using webpack-dev-server in React development mode? |
UPDATED Clone demo projectYou can clone the demo repository: react-demo-abort-controller-proxy-issue. git clone https://github.com/grandsilence/react-demo-abort-controller-proxy-issue.git
cd react-demo-abort-controller-proxy-issue
npm install
npm run server
npm run start Or create new project
Description of the resulting behaviorAfter you can make some requests, cancel all and reproduce the issue.
But expected demo server behavior is:
|
Hi there 👋 I have a use case where I was triggering multiple POST requests and trying to abort them using only 1 controller/abort signal: it didn't work. I simplified it to troubleshoot the issue to just 1 POST request, and it didn't work either: the request goes through and the promise is resolved normally. I don't think it is a server problem as I am hitting an Elastic Search API. I am using the axios client instance ( this.client = axios.create(...); // the function that triggers the request:
this.requestsController = new AbortController();
const abortSignal = this.requestsController.signal;
this.results = await this.client.post(`/search.json`, payload, { signal: abortSignal }); then in a click handler for a clear search button I have the abort like so: if (this.requestsController) {
this.requestsController.abort();
} I am using axios Any help would be much appreciated 🙏 |
According to the Axios docs, Abort Controller support begins with version 0.22.0, so your best bet would be to upgrade |
I have two clients for a single http server: one written in kotlin using OkHttp and another written in typescript using axios. For axios I'm using AbortController but it doesn't cancel requests on a server side. OkHttp cancels requests normally. |
Describe the bug
Followed official doc, Use cancelToken to close pending reqeust, the browser side do cancel the request, but the server side did not receive request cancel signal.
But the get method works well as expected.
To Reproduce
Here is my demo code to reproduce the environment.
First start the server, go run main.go. Second run the client, node index.js.
When js client exit, the server not receive signal as expect.
Go Server
Expected behavior
when post request is canceld, expect server side recevice signal. For example, goroutine context is Done and release some resource.
Environment
Additional context/Screenshots
Add any other context about the problem here. If applicable, add screenshots to help explain.
The text was updated successfully, but these errors were encountered: