adonisjs / lucid

AdonisJS SQL ORM. Supports PostgreSQL, MySQL, MSSQL, Redshift, SQLite and many more
https://lucid.adonisjs.com/
MIT License
1.08k stars 195 forks source link

Error: Transaction query already complete, run with DEBUG=knex:tx for more info, Browser client test #989

Closed EdAndrade closed 9 months ago

EdAndrade commented 10 months ago

About the error

I am using browser client provided by japa to run e2e tests.

I'm tryng to run global transaction for each test on group using the code above: Screenshot from 2024-01-26 09-37-39

And i'm having this error:

at processTicksAndRejections (node:internal/process/task_queues:95:5)\n    at Runner.run (/<PROJECT_PATH>/node_modules/knex/lib/execution/runner.js:30:19)\n    at QueryRunner.executeQuery (/<PROJECT_PATH>/node_modules/@adonisjs/lucid/build/src/QueryRunner/index.js:78:28)",
"type":"Error","msg":"Transaction query already complete, run with DEBUG=knex:tx for more info"

Using the aproach above is triggering the same error: Screenshot from 2024-01-26 09-39-02

Related libs version:

"webpack": "^5.89.0",
"playwright": "1.33.0",
"@japa/runner": "2.5.0"
"@japa/browser-client": "1.2.0",
"@adonisjs/assembler": "5.9.6"
"@adonisjs/core": "5.9.0",
"@adonisjs/lucid": "18.3.0"
Julien-R44 commented 10 months ago

Can you show your tested code ? I guess you are probably using a transaction in it

Transactions cannot be nested, so if this is your case, you need to use another strategy for these specific tests

EdAndrade commented 10 months ago

Yes @Julien-R44 for sure, i have just censured some parts because its a enterprise code, but you can ask if you need something else. Screenshot from 2024-01-26 12-13-08

Julien-R44 commented 10 months ago

Please search for transactions in your tested code. I'm pretty sure this is the problem If not, can you create a replication repo?

EdAndrade commented 10 months ago

I searched again, only have these global transactions that are runned in setup and teardown. I will create a replication repo

Tahul commented 9 months ago

Hey.

I have the same issue.

I migrated all my code trying multiple methods (same as the ones mentioned in the issue) and couldn't find a workaround.

I don't have any other transactions declared in explicitely in my app, but maybe the v6 upgrade made some happen in the background?

Julien-R44 commented 9 months ago

Hey, same as above : ideally, it would be super handy to share a reproduction repo if possible please

Tahul commented 9 months ago

@Julien-R44 I'm on it right now, but it occurs in company code and I fail to create a reproduction in a repository.

I'm digging on the Lucid code to learn more about transactions because I'm 100% sure I don't create one manually in my code.

Tahul commented 9 months ago

https://github.com/Tahul/lucid-transactions-repro

I'm working on the repro in this repository.

Julien-R44 commented 9 months ago

Thanks a lot, let me know if you are able to reproduce

Tahul commented 9 months ago

@Julien-R44 ;

I got it.

It seem to be related to SQL queries made through anything behind HTTP.

This test is causing it 100% of the time:

https://github.com/Tahul/lucid-transactions-repro/blob/78aeaa284879c1934c6bf0a90e60b8e4f816142b/tests/unit/test.spec.ts#L44-L58

Note this behavior was supported in Adonis 5 (transactions made for data altered through HTTP queries).

Instructions to run:

Julien-R44 commented 9 months ago

okay so this is the output i have :

image

we can see that the test timeout. if you disable timeouts then you shouldn't get this error anymore :

  test(`create ${batchSizes} TestData`, async ({ assert }) => {
    for (let i = 0; i < batchSizes; i++) {
      await fetch(`http://${env.get('HOST')}:${env.get('PORT')}/test`, {
        method: 'POST',
        body: JSON.stringify({
          text: faker.word.sample(),
          text2: faker.word.sample(),
          text3: faker.word.sample(),
          text4: faker.word.sample(),
        }),
      })
    }

    assert.equal(true, true)
  }).disableTimeout() // 👈👈👈

Let me know if it works for you

Tahul commented 9 months ago

That's also what I just noticed in my own test suite by raising the timeout by a lot.

Tahul commented 9 months ago

Thank you a lot.

Tahul commented 9 months ago

I think that should be explained somewhere though, that issue only took me a lot to figure out the cause looking at my tests alone.

That suggested command (DEBUG=knex:tx) gives no clue at all.

Julien-R44 commented 9 months ago

i am not sure, because, to me, it's pretty self explanatory: "Test timeout" is written in bold red in the terminal output

i would suggest seeing if other issues with the same issue open up in the next few weeks/months. if its the case, then we probably could add a quick note in the documentation ?

closing the issue in the meantime, if anyone encounters the same problem, please feel free to re-open it !

thanks again for taking time for giving a reproduction 🤝