Closed samsondav closed 1 year ago
I am also getting this since v0.1.4
.
runtime.Gosched and time.Sleep do not fix it for me
The only fix for me was to revert to v0.1.3
should be fixed by commit now.
the patch before introduced a chance for deadlock, but it also was necessary to prevent shared resource access. hope this will solve both issues. please reopen in case if you see any issue on v0.1.5
@l3pp4rd sorry but I am still getting the errors even when using v0.1.5. Using v0.1.3 shows no errors.
sql: transaction has already been committed or rolled back
I'm also experiencing it on v0.1.5
also stuck with gorm
in our project. Any update on this?
I think I've tracked this down. You can reproduce it by creating a db then issuing an ExecContext
. After than first insert is completed, the context that begins the tx is still being used. That insert is over, and the context cancelation shouldnt' affect the db as a whole, but rather the exec. After the first ExecContext
is completed successfully, cancel the context used for that exec, and create a new insert with new context and it fails with tx has been rolled back. I'm going to see if I can work this up a bit more. When I add this test to the db_test.go
it fails as expected:
submitted PR to address the context pollution issue. Its passing all tests and runs without a hitch locally as well. Fixes the issues I was seeing.
@veqryn, I'm curious if you'll get errors with the branch I just pushed to fix an issue with context handling. Its fixed both gorm and sqlx tests locally.
now that PR #48 has merged, I think its safe to close this issue
@samsondav, I’m going to close this issue. It should be resolved now. Feel free to return if the issue resurfaces on v0.1.6 or later 👍
We're seeing this in our tests. Our testcases each have their own context that gets cancelled when the testcase finishes.
There is a race condition that the whole transaction may be canceled and the next testcase will use the canceled c.tx
. Specifically, the select statement in that goroutine may observe either <-done
which doesn't do anything or <-ctx.Done()
which cancels rootCtx
, triggering the error on the next operation.
https://github.com/DATA-DOG/go-txdb/blob/5bd9aad70f58b6ca14983b883643a2b461ee218e/db_go18.go#L56-L58
As a temporary fix, we just removed the whole goroutine to prevent the global transaction being rolled back by a single operation's context.
It must be a timing issue because it can be resolved by inserting a sleep or gosched here:
By inserting a delay before the context gets canceled gives the goroutine in beginTxOnce
time to select the <-done
case but it is not deterministic.
We are seeing
transaction has already been committed or rolled back errors
if we try to use the database "too quickly" after initializing it, with a cancel context.Code looks like this:
Note that this error about "transaction already committed or rolled back" has nothing to do with postgres (actually the transaction never gets closed). The postgres logs look like this:
It must be a timing issue because it can be resolved by inserting a sleep or gosched here:
Note that I was running this test with a parallelism flag of 1 - single threaded mode.
UPDATE:
It seems that calling
runtime.Gosched()
only sometimes fixes it - not always. Callingdb.Exec('SELECT 1')
in place of that seems to be a more reliable fix.