Incomaptibility between Dexie.js v4.0.7 and @opentelemetry/context-zone 1.18.1 even with forcing last version of zone.js version 0.14.6
Hello,
We are trying to use the last version of RxDB with the Dexie connector. The RxDB issue is created even if it's a incomaptibility between others package (https://github.com/pubkey/rxdb/issues/6008)
Basically Zone.js used in @opentelemetry/context-zone override the default Promise object. But Dexie warn that if it happen the async transactions will not work. We was able to assert with debugger that this line is called https://github.com/dexie/Dexie.js/blob/d8d85270130db72aa93e242732539d3e85480081/src/helpers/promise.js#L617
So this line: https://github.com/dexie/Dexie.js/blob/d8d85270130db72aa93e242732539d3e85480081/src/classes/dexie/transaction-helpers.ts#L77C7-L77C30 is doing nothing and we have the error of PrematureCommit explained in the RxDB issue. It is of course the expected behavior as zone.js is overriding the Promise object but as it is a really common tracing library I am wondering if it will be possible to support the zone.js promise object too ?
If I get a repro I could try to debug it. Generally, Zone.js have been working with Dexie. At least when transpiling down to ES2016 or earlier which is needed anyway in order for Zone.js to be able to wrap around await calls as it cannot intercept native await.
Hello, i'm working whit @AMontagu .
Following this link you will get a minimal repo with the error.
https://github.com/Mike-FR/rxdb-dexie-zone-test/
npm install
npm run dev
In the main.js, if you comment the import from "@grafana/faro-web-tracing", the error disappear.
Thanks for the help.
any updates on the issue? @dfahlander
I've spent some time with the repro but this will need deep diving to find out the culprit. There are other tasks pressuring me right now that I have to deal with before spending more time on this issue. I'm sorry but the reality for me is that I work for free half time and for paid the other half. If this is urgent, please sponsor Dexie.js. Don't mean that I require sponsoring to fix it - I also want dexie to be the best in class and work with all other libraries, just that I need to prioritize this project now. I hope everyone understands.
Yes we totally understand. We will see for sponsoring or debugging it ourself and make a PR.
As is is just blocking an update to the latest version of RxDB and Dexie is not our top priority either.
Hello
So it look like the error in our repo and the example repo ae not the same. In our repo this is not a native promise. In the example repo it is with juste zone js above it.
I focused on the example repo first.
The issue come from this commit: https://github.com/dexie/Dexie.js/commit/988d73f1130e6487dd8382a19a108f17e84983ab
If I put back enqueueNativeMicroTask instead of queueMicrotask it's working.
As it has been done to avoid error on liveQuery and I don't have a liveQuery I will continue to investigate to see if I can fix queueMicrotask.
Then I will look back in our repo if it's fix the issue of if a need to also fix something else.
I have an hard time understanding the whole zone wrapper system.
When using enqueueNativeMicroTask in switchToZone then the method is called 113 time with bEnteringZone true and 112 times with bEnteringZone false
When using queueMicrotask in switchToZone then the method is called 288 time with bEnteringZone true and 287 times with bEnteringZone false.
I do not explain the difference juste changing the method. And this is just in an example repository where we create a database and add a collection to it without anythin else.
How changing the one line increase that much the number of call ? Why the number of call is so high ? It make the all thing really hard to debug as there is 500 calls and maybe one is culprit but to find it it's hard. Is there is a way to reduce this ?
It look like that with queueMicrotask the code is echoing too much. I don't know if really related.
The main point is that this._completion.then of createTransactionConstructor is called even if the code don't call it.
I can't find the point where it is call. I mean it is in trans.create(); but i don't find the how the oncomplete is called. Is it coming frm IndexedDB ? If yes that mean IndexedDB think that the transaction is finished (I don't know how) even if Dexie thinks it is not. And this is maybe related to queueMicrotask that may let IndexedDB call it's oncomplete callback even if code not finished.
When using enqueueNativeMicroTask, only one on complete by transactions:
When using queueMicrotask, multiple on complete by transactions::
Hello @dfahlander
I didn't look more into it since friday. It seem clear to me that the issue is not only a bug but also an opiniated choice about:
- Using queueMicrotask
- Supporting opentelemetry using Vite non native Promise (will need an other branch on the test repo to reproduce correctly)
We are considering sponsoring Dexie on https://github.com/sponsors/dexie?frequency=one-time. How can we arrange a quick meeting before it to confirm amount and estimated timing for both the fix and feature ?
Hello @dfahlander
I didn't look more into it since friday. It seem clear to me that the issue is not only a bug but also an opiniated choice about:
- Using queueMicrotask
- Supporting opentelemetry using Vite non native Promise (will need an other branch on the test repo to reproduce correctly)
We are considering sponsoring Dexie on https://github.com/sponsors/dexie?frequency=one-time. How can we arrange a quick meeting before it to confirm amount and estimated timing for both the fix and feature ?
I'd be available on Friday or next week Wednesday, daytime Central European Time.. Please send a calendar invite to me on [email protected].
Hello,
To prepare the friday call I pushed all the different step and branch to debug reproduce so we can follow them on friday.
First step reproduce the issue of queueMicrotask: Dexie fork: https://github.com/AMontagu/Dexie.js/tree/linkWithViteProjectWorking MRE: https://github.com/Mike-FR/rxdb-dexie-zone-test/tree/errorJustOnImport
Then having this issue fix by switching to: Dexie fork: https://github.com/AMontagu/Dexie.js/tree/workingSimpleImport
Then having the Zone aware promise error (by changing the import order): MRE: https://github.com/Mike-FR/rxdb-dexie-zone-test/tree/errorNotNativePromise
So after the bug investigation the better solution for now is to transpile teh dependencies that use Dexie so they can be babelify.
Example using webpack:
// Webpack configuration file.
transpileDependencies: ["rxdb", /@grafana\/faro-web-tracing/],
// Babel configuration file
module.exports = {
...
plugins: [
...
"@babel/plugin-transform-async-to-generator",
],
};