Skip to content
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

[3.x] Fix promise rejection throwing several times #13010

Merged
merged 11 commits into from
Apr 2, 2024

Conversation

nachocodoner
Copy link
Member

@nachocodoner nachocodoner commented Feb 19, 2024

OSS-322

Context: #12950

I have given a reattempt to this issue. And after studying more the code and the experience so far about it I came up with the proper fix.

Within Meteor 3.x code, there are three promises utilized when invoking methods: promise for server promises, stubPromise for client simulation, and serverPromise, which acts as a wrapper for promise.
Refer here to understand further 2.x and 3.x async behaviors.

In the Meteor 3.x codebase, we encounter several layers where promises are nested. In JavaScript, when promises are nested, unhandled exceptions from the "child" promises propagate upwards to the parent or root, resulting in uncaught errors. I've identified these instances and provided solutions for them.

Reproduction

Described a method that always throw an error on client and server side.

Meteor.methods({  
    promiseRejectionSeveralLogs: async function() {  
       throw new Meteor.Error('negative')  
    },  
});

Then on the client side I run callAsync it and manage the promises

const promise = Meteor.callAsync('promiseRejectionSeveralLogs', ['thing']);

if (promise.stubPromise)  
    promise.stubPromise.catch(e => console.error('stubPromise error', e));
  
if (promise.serverPromise)  
    promise.serverPromise.catch(e => console.error('serverPromise error', e)); 
 
if (promise)  
    promise.catch(e => console.error('promise error', e));

image

I have seen other scenarios where even more uncaught promises are showing as described on this issue, #12950

Fix

promise + stubPromise + serverPromise

With the changes on this PR.

image

promise

const promise = Meteor.callAsync('promiseRejectionSeveralLogs', ['thing']);
if (promise)  
    promise.catch(e => console.error('promise error', e));

promise + stubPromise

const promise = Meteor.callAsync('promiseRejectionSeveralLogs', ['thing']);
if (promise.stubPromise)
    promise.stubPromise.catch(e => console.error('stubPromise error', e));
if (promise)
    promise.catch(e => console.error('promise error', e));

image

promise + serverPromise

const promise = Meteor.callAsync('promiseRejectionSeveralLogs', ['thing']);
if (promise.serverPromise)
    promise.serverPromise.catch(e => console.error('serverPromise error', e));
if (promise)
    promise.catch(e => console.error('promise error', e));

image

stubPromise

const promise = Meteor.callAsync('promiseRejectionSeveralLogs', ['thing']);
if (promise.stubPromise)
    promise.stubPromise.catch(e => console.error('stubPromise error', e));

image

serverPromise

const promise = Meteor.callAsync('promiseRejectionSeveralLogs', ['thing']);
if (promise.serverPromise)
    promise.serverPromise.catch(e => console.error('serverPromise error', e));

image

serverPromise + stubPromise

const promise = Meteor.callAsync('promiseRejectionSeveralLogs', ['thing']);
if (promise.stubPromise)
    promise.stubPromise.catch(e => console.error('stubPromise error', e));
if (promise.serverPromise)
    promise.serverPromise.catch(e => console.error('serverPromise error', e));

image

no promise caught

await Meteor.callAsync('promiseRejectionSeveralLogs', ['thing']);

image

@nachocodoner nachocodoner changed the title 322 fix promise rejection several (wip) 322 Fix promise rejection throwing several times Feb 19, 2024
Copy link

netlify bot commented Mar 21, 2024

Deploy Preview for v3-meteor-api-docs canceled.

Name Link
🔨 Latest commit fa94abb
🔍 Latest deploy log https://app.netlify.com/sites/v3-meteor-api-docs/deploys/66044fcd207ff6000806200e

@nachocodoner nachocodoner added the Meteor 3 relates to Meteor 3 label Mar 21, 2024
@nachocodoner nachocodoner changed the title 322 Fix promise rejection throwing several times [3.x] Fix promise rejection throwing several times Mar 21, 2024
@nachocodoner nachocodoner marked this pull request as ready for review March 27, 2024 17:00
@denihs denihs merged commit 3c15c57 into release-3.0 Apr 2, 2024
12 checks passed
@nachocodoner nachocodoner deleted the 322-fix-promise-rejection-several branch April 2, 2024 16:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants