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

Something strange in transaction() #6805

Open
alxndrsn opened this issue Jul 5, 2019 · 9 comments
Open

Something strange in transaction() #6805

alxndrsn opened this issue Jul 5, 2019 · 9 comments
Labels
orm Related to models, datastores, orm config, Waterline, sails-hook-orm, etc. proposal

Comments

@alxndrsn
Copy link

alxndrsn commented Jul 5, 2019

Node version:
Sails version (sails): 1.2.3
ORM hook version (sails-hook-orm): 2.1.1
Sockets hook version (sails-hook-sockets):
Organics hook version (sails-hook-organics):
Grunt hook version (sails-hook-grunt):
Uploads hook version (sails-hook-uploads):
DB adapter & version (e.g. sails-mysql@5.55.5): sails-postgresql 1.0.2
Skipper adapter & version (e.g. skipper-s3@5.55.5):


Steps to recreate:

git clone git@github.com:alxndrsn/sailsjs-bug-6805.git
cd sailsjs-bug-6805
yarn
yarn start &
curl localhost:1337

Observed:

Transaction never completes for this controller:

const fn = function() {};                                                                                                                                                    
                                                                                                                                                                             
module.exports = {                                                                                                                                                           
  fn: async function (inputs, exits) {                                                                                                                                       
    await sails.getDatastore()                                                                                                                                               
      .transaction(async db => {                                                                                                                                             
        fn(true, true);                                                                                                                                                      
      });                                                                                                                                                                    
                                                                                                                                                                             
    return exits.success();                                                                                                                                                  
  }                                                                                                                                                                          
}; 

Adding a console.log() inside the controller allows the transaction to complete:

const fn = function() {};                                                                                                                                                    
                                                                                                                                                                             
module.exports = {                                                                                                                                                           
  fn: async function (inputs, exits) {                                                                                                                                       
    await sails.getDatastore()                                                                                                                                               
      .transaction(async db => {              
        console.log();                                                                                                                               
        fn(true, true);                                                                                                                                                      
      });                                                                                                                                                                    
                                                                                                                                                                             
    return exits.success();                                                                                                                                                  
  }                                                                                                                                                                          
}; 

Another way of fixing the problem is to remove one of the arguments from fn(...):

const fn = function() {};                                                                                                                                                    
                                                                                                                                                                             
module.exports = {                                                                                                                                                           
  fn: async function (inputs, exits) {                                                                                                                                       
    await sails.getDatastore()                                                                                                                                               
      .transaction(async db => {                                                                                                                                             
        fn(true);                                                                                                                                                      
      });                                                                                                                                                                    
                                                                                                                                                                             
    return exits.success();                                                                                                                                                  
  }                                                                                                                                                                          
}; 

or, changing the argument to a string:

const fn = function() {};                                                                                                                                                    
                                                                                                                                                                             
module.exports = {                                                                                                                                                           
  fn: async function (inputs, exits) {                                                                                                                                       
    await sails.getDatastore()                                                                                                                                               
      .transaction(async db => {                                                                                                                                             
        fn(true, '');                                                                                                                                                      
      });                                                                                                                                                                    
                                                                                                                                                                             
    return exits.success();                                                                                                                                                  
  }                                                                                                                                                                          
}; 

Another fix is to remove the argument for the transaction callback:

const fn = function() {};                                                                                                                                                    
                                                                                                                                                                             
module.exports = {                                                                                                                                                           
  fn: async function (inputs, exits) {                                                                                                                                       
    await sails.getDatastore()                                                                                                                                               
      .transaction(async () => {                                                                                                                                             
        fn(true, true);                                                                                                                                                      
      });                                                                                                                                                                    
                                                                                                                                                                             
    return exits.success();                                                                                                                                                  
  }                                                                                                                                                                          
}; 

?!?!?!?!?!

Conclusion

If there is a single function call inside a transaction with two or fewer arguments of certain values, then the transaction is never completed.

@sailsbot
Copy link

sailsbot commented Jul 5, 2019

@alxndrsn Thanks for posting! We'll take a look as soon as possible.

In the mean time, there are a few ways you can help speed things along:

  • look for a workaround. (Even if it's just temporary, sharing your solution can save someone else a lot of time and effort.)
  • tell us why this issue is important to you and your team. What are you trying to accomplish? (Submissions with a little bit of human context tend to be easier to understand and faster to resolve.)
  • make sure you've provided clear instructions on how to reproduce the bug from a clean install.
  • double-check that you've provided all of the requested version and dependency information. (Some of this info might seem irrelevant at first, like which database adapter you're using, but we ask that you include it anyway. Oftentimes an issue is caused by a confluence of unexpected factors, and it can save everybody a ton of time to know all the details up front.)
  • read the code of conduct.
  • if appropriate, ask your business to sponsor your issue. (Open source is our passion, and our core maintainers volunteer many of their nights and weekends working on Sails. But you only get so many nights and weekends in life, and stuff gets done a lot faster when you can work on it during normal daylight hours.)
  • let us know if you are using a 3rd party plugin; whether that's a database adapter, a non-standard view engine, or any other dependency maintained by someone other than our core team. (Besides the name of the 3rd party package, it helps to include the exact version you're using. If you're unsure, check out this list of all the core packages we maintain.)

Please remember: never post in a public forum if you believe you've found a genuine security vulnerability. Instead, disclose it responsibly.

For help with questions about Sails, click here.

@johnabrams7 johnabrams7 added orm Related to models, datastores, orm config, Waterline, sails-hook-orm, etc. proposal labels Jul 5, 2019
@johnabrams7
Copy link
Contributor

johnabrams7 commented Jul 11, 2019

@alxndrsn Thanks a lot for bringing attention to this logic curiosity. Will bring it up with the team for analysis of the best approach to have transaction() complete. Appreciate the handful of workarounds I'll get back with you with the decision 👍 Any further community feedback / experiences are welcome for this one.

@johnabrams7 johnabrams7 added the what do you think? Community feedback requested label Jul 11, 2019
@alxndrsn
Copy link
Author

@mikermcneil @MadisonHicks Just in case you haven't seen it, I think this is my favourite bug 😉

@sailsbot sailsbot removed the what do you think? Community feedback requested label Jan 23, 2020
@alxndrsn
Copy link
Author

If there is a single function call inside a transaction with two or fewer arguments of certain values, then the transaction is never completed.

I've just run into this again with a three-argument function preceded by await.

@alxndrsn
Copy link
Author

alxndrsn commented May 28, 2020

This can be avoided by replacing:

await sails.getDatastore().transaction(

with

await withDbTx(

and defining the additional function:

function withDbTx(fn) {
  return sails
      .getDatastore()
      .transaction(db => fn(db));
}

@alxndrsn
Copy link
Author

alxndrsn commented Jul 2, 2020

😱

@mikermcneil what's that code doing? Checking if the during function has more than one arguments? Could you just use:

var seemsToExpectCallback = options.during.length > 1;

?

@alxndrsn
Copy link
Author

alxndrsn commented Jul 2, 2020

For reference if facing similar mysteries, this code's repeated in:

  • waterline/lib/waterline/methods/stream.js
  • sails/lib/hooks/index.js
  • sails/lib/app/private/bootstrap.js
  • sails-hook-orm/lib/datastore-method-utils/help-run-transaction.js (linked above)
  • sails-hook-orm/lib/datastore-method-utils/private/do-with-connection.js

@alxndrsn
Copy link
Author

alxndrsn commented Jul 2, 2020

Example PR for the core sails instances: #7017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
orm Related to models, datastores, orm config, Waterline, sails-hook-orm, etc. proposal
Development

No branches or pull requests

4 participants