question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

CLS lost with native Promises in 4.0?

See original GitHub issue

In v4, does CLS work with native Promises, or is Sequelize.Promise currently required?

Instrumenting the usual way:

import Sequelize from 'sequelize';
import cls from 'continuation-local-storage';
Sequelize.cls = cls.createNamespace('transactions');

… this simple example is not executing in a transaction:

this.sequelize.transaction().then(t => {
  return (new ModelName({ some: 'data'})).save();
});

I get the following output:

server_1 | Executing (df667891-2dee-4d1f-b3b1-8c276e5f69d7): START TRANSACTION; server_1 | Executing (default): INSERT INTO tableName (id,createdAt,updatedAt`) VALUES (‘fa9f08ce-48cf-4e2b-b3e1-2127fd6eb9d5’, // cut for brevity

Note:

  • The insert is being performed on the default connection (i.e not inside a transaction)
  • No implicit commit/rollback - the transaction is left open

I can fix the above by passing in an explicit { transaction }, but then I also need to use try/catch block and wire up t.commit() or t.rollback() manually.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:47 (23 by maintainers)

github_iconTop GitHub Comments

2reactions
overlookmotelcommented, Sep 5, 2016

I’m not sure if you can rely on CLS passing correctly with async/await. It all depends on what the polyfill converts it to.

Often with CLS, you test an example like that and it all seems fine, but then once you have multiple async “threads” of execution in flight at the same time it can go pear-shaped, with the CLS context getting mixed up between the “threads”.

Also, native promises do claim to support CLS. But it works slightly differently from my CLS bluebird patch, and also I don’t know how thoroughly it’s tested.

I’d advise using cls-bluebird (once the new version is released, probably in next couple of days) and do this:

return this.sequelize.transaction( Bluebird.coroutine( function*(t) {
  const model = new Model1({
    some: 'data',
  });

  yield model.save();
  yield model2.addDependentModelName(model1);

  return model1;
} ) );

I can pretty much guarantee that’ll work 100%.

1reaction
overlookmotelcommented, Nov 23, 2017

All change in the world of CLS with async_hooks now arrived in Node core. But still some kinks need to be worked out in async_hooks itself with how it works with Promises, and it’s going to take a while until lots of npm modules make the necessary changes to support async_hooks.

The CLS implementation in Sequelize (using cls-bluebird) works. But I wouldn’t expect any progress on some of the questions posed above any time soon. Now that async_hooks is here, it’s a matter of rip up most of what came before and re-write from scratch. So that’s going to take time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to resolve the error on 'react-native start' - Stack Overflow
I just got a similar error for the first time today. It appears in \node_modules\metro-config\src\defaults\blacklist.js , there is an ...
Read more >
Asynchronous context tracking | Node.js v19.3.0 Documentation
This class creates stores that stay coherent through asynchronous operations. ... it with util.promisify() so it starts working with native promises.
Read more >
Node.js agent release notes | New Relic Documentation
This version of the native metrics module is tested against Node 14 and inc… ... Previously, the transaction state could get lost when...
Read more >
SWIG-4.0 Documentation
The above example will generate native build files such as makefiles, ... interfaces even when there are missing class declarations or opaque datatypes)....
Read more >
Assessment and Treatment of Patients With Suicidal Behaviors
may result in increased stress and loss of professional self-esteem. ... in immigrants and in Native Americans and Alaska Natives, may generate ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found