-
-
Notifications
You must be signed in to change notification settings - Fork 931
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
The onCancel handler was attached after the promise settled #1489
Comments
Can you please post some code? |
Node.js version: 15.1.0 Just encountered the same, very hard to repro, looks like a race-condition, I will try to dig deeper, but is there any way to catch such exception and just keep running? Stack: error: unhandledRejection: The `onCancel` handler was attached after the promise settled.
Error: The `onCancel` handler was attached after the promise settled.
at onCancel (scripts\node_modules\p-cancelable\index.js:46:12)
at makeRequest (scripts\node_modules\got\dist\source\as-promise\index.js:38:13)
at Request.<anonymous> (scripts\node_modules\got\dist\source\as-promise\index.js:143:17)
at Object.onceWrapper (node:events:434:26)
at Request.emit (node:events:327:20)
at Timeout.retry (scripts\node_modules\got\dist\source\core\index.js:1261:30)
at listOnTimeout (node:internal/timers:555:17)
at processTimers (node:internal/timers:498:7) |
Thanks for the report @bkarlson. Just to confirm - are you running Got 11.8.0? |
Yes, 11.8.0, and the issue seems to be related to |
An easy fix would be using I'm still investigating on the root cause. |
This is still happening. Here my error trace: https://sentry.io/share/issue/6f578a2a936943cca5afe367e6732995/ |
@yovanoc @Kikobeats @bkarlson Can you try again? The |
@szmarczak I'm still experimenting the error https://sentry.io/share/issue/c607e343ef6345c38ad391a7c3f43149/ node version is: v14.16.0 |
@Kikobeats Can you show the line(s) where you call |
@Kikobeats can you replace |
@szmarczak In my case is hard to say since there are a couple of dependencies using Also, since this is running a production environment, I can't replace it in an easy way. Maybe can you release a |
|
Actually no because it's destroyed here Lines 1469 to 1472 in 4cce4de
|
|
@Kikobeats What is the timeout event? Is it |
Also can you do |
@Kikobeats @bkarlson @yovanoc If your project uses Got directly you can try const isResponseOk = response => {
const {statusCode} = response;
const limitStatusCode = response.request.options.followRedirect ? 299 : 399;
return (statusCode >= 200 && statusCode <= limitStatusCode) || statusCode === 304;
};
const instance = got.extend({
hooks: {
afterResponse: [
response => {
if (isResponseOk(response)) {
response.request.destroy();
}
return response;
}
]
}
}); If so, please let me know if that fixes the issue. |
Hey @szmarczak, I'm running your code snippet on my production servers since yesterday and I didn't experiment errors until now (maybe it needs a bit of extra time) |
That one fixed the issue for me as well. Thank you very much @szmarczak ! 🙏🏻 I am running got as part of generating a fake environment with lots of pictures. I have a feeling many uploads in quick succession are causing this. |
@CvBlixen Yes! If you have set the It can also happen with a |
This comment has been minimized.
This comment has been minimized.
Also the |
I wonder if this check got/source/core/utils/timed-out.ts Line 135 in 4cce4de
socket.connecting .
|
|
I'm not sure if this will help, but I think I'm seeing this. I've been attempting to troubleshoot why a network interruption causes my code (which uses ring-client-api which uses got) stops updating even after the network connection is reestablished, but only when using http2. I modified ring-client-api to set the timeout to 10 seconds, and then I interrupt the network connection to create the timeout but I get this which I can't seem to trap:
Any help would be appreciated. |
@tsightler So... are you able to reproduce this? |
Users have been reporting the following error when using Garden: error: unhandledRejection: The `onCancel` handler was attached after the promise settled. I haven't been able to reproduce it but apparently it's an issue with got. See: sindresorhus/got#1489 This commit _attempts_ to fix it by implementing the workaround suggested here: sindresorhus/got#1489 (comment) Some users have reported that it fixes the issue, others that it doesn't. Since I haven't been able to repro myself this is sort of a best effort thing. Hopefully it does the trick.
Just an FYI I also got the same issue very suddenly, reading through this thread to see what I can do. Using version v11.8.5 of got. Will try to update to the latest got version. |
I am having this issue a lot with the version v11.8.5, and node 16. I am trying to upgrade to 12.1.0, but since it's pure esm, it's honestly very tedious to perform the migration of a whole project for this one bug. |
Any news about this ? |
@szmarczak Could the fix for this be applied to v11.x? My project is currently using CommonJS modules so we aren't able to upgrade to the latest version right now due to v12.x being pure ESM. I see there's a workaround above but we use |
The bug is not fixed in Got v12 either, I haven't got the time to inspect it in much detail yet |
I also recently started to see this error often in our services. We use got with shared custom extensions in all of our micro services, and it worked just fine, until we upgraded from Node 14 to Node 16. |
I have the same situation @lukelafountaine describes. It's unclear to me from the tagging of this issue if any of the core developers are going to work on this? Or if you're needing a community contribution? |
Like @shlomo-artlist we've started seeing this error after upgrading from Node 12 to Node 16 (got v11.8.5) in requests with retries. {
"errorType": "Runtime.UnhandledPromiseRejection",
"errorMessage": "Error: The `onCancel` handler was attached after the promise settled.",
"reason": {
"errorType": "Error",
"errorMessage": "The `onCancel` handler was attached after the promise settled.",
"stack": [
"Error: The `onCancel` handler was attached after the promise settled.",
" at onCancel (/var/task/node_modules/p-cancelable/index.js:48:12)",
" at makeRequest (/var/task/node_modules/got/dist/source/as-promise/index.js:38:13)",
" at Request.<anonymous> (/var/task/node_modules/got/dist/source/as-promise/index.js:143:17)",
" at Object.onceWrapper (node:events:642:26)",
" at Request.emit (node:events:527:28)",
" at Request.emit (node:domain:475:12)",
" at Timeout.retry (/var/task/node_modules/got/dist/source/core/index.js:1278:30)",
" at listOnTimeout (node:internal/timers:559:17)",
" at processTimers (node:internal/timers:502:7)"
]
},
"promise": {},
"stack": [
"Runtime.UnhandledPromiseRejection: Error: The `onCancel` handler was attached after the promise settled.",
" at process.<anonymous> (file:///var/runtime/index.mjs:1131:17)",
" at process.emit (node:events:527:28)",
" at process.emit (node:domain:475:12)",
" at process.emit (/var/task/node_modules/source-map-support/source-map-support.js:516:21)",
" at emit (node:internal/process/promises:140:20)",
" at processPromiseRejections (node:internal/process/promises:274:27)",
" at processTicksAndRejections (node:internal/process/task_queues:97:32)"
]
} @szmarczak could it be related to the change to use I've been working on creating a test case to reproduce it. I'll report back if I'm successful. |
No, it has to do with the retry mechanism. If you disable retries then the issue is gone. |
edit:
edit2: |
@szmarczak after doing some troubleshooting we figured out that in our case the error is caused by an A colleague was able to create a reproducible test case. It triggers the error with got v11.8.5 but not with got v12.5.0 (using node 16.15.0 in both cases). It does that by resetting the TCP connection on the server side shortly after sending the response. The code and sample output is available on this gist. |
I discovered this too recently. At least in our case it seems to be a race condition only happening on fast connections. As weird as it sounds, a workaround is to connect to a slower Internet connection (in my case through my phone). This has completely solved my team's issues with this error that we saw when downloading Chromium. Your mileage may vary depending on where you see this error, but hopefully this might help someone. |
@spacecakes wow, I wouldn't have expected that as a workaround... I'm assuming you don't have control of const isResponseOk = (response) => {
const { statusCode } = response,
limitStatusCode = response.request.options.followRedirect ? 299 : 399;
return response.complete && ((statusCode >= 200 && statusCode <= limitStatusCode) || statusCode === 304);
};
const instance = got.extend({
retry: {
calculateDelay: (retryObject) => {
const error = retryObject.error,
response = error.response;
// Abort the retry (by returning a delay of 0) if we have an ECONNRESET
// error after a successful response.
if (error.code === 'ECONNRESET' && response && isResponseOk(response)) {
try {
console.info(JSON.stringify({
evt: 'got-abort-late-econnreset-retry',
error: error,
response: {
statusCode: response.statusCode,
statusMessage: response.statusMessage,
complete: response.complete,
},
}));
} catch(err) {
console.error(err);
}
return 0;
}
return retryObject.computedValue;
},
}
}); |
You can imagine it took some time and trial and error to figure this out. 😅 We don't have control no. It's a peer dependency in our test suite that downloads Chromium, which in turn uses It doesn't make any sense to me either, but we've been able to repeat it; if fails consistently on 250MB+ connections and works consistently on lesser ones. Fortunately once installed we don't have to run it again for a long time. |
I am having this issue with got v11.8.6 and Node 20 when I have Snippet of my code below. got.get(`https://www.google.com`,
{
timeout: { request: 100 }, // Made the timeout small so it's retried.
http2: true,
dnsCache: true,
retry: 3,
},
) |
Describe the bug
The onCancel handler was attached after the promise settled
Actual behavior
This unhandled exception is not catch by a try/catch, but I see that it happen after a timeout error like this issue #1460
Checklist
The text was updated successfully, but these errors were encountered: