Calling discard() after mutations throws the error: "Transaction has been aborted, please retry." (Code 10)

Moved from GitHub dgraph-js/48

Posted by AlexandreDaSilva:

Hello!

I think the client has a strange behavior when we call discard() after some mutations: it throws the following error : Transaction has been aborted. Please retry. I don’t think it is useful, because if I call this method, it’s precisely because I want to abort the transaction (and didn’t call commit())…

In this case, I noticed that the error object has an additionnal property code with the value 10, and the following is false: e == dgraph.ERR_ABORTED.

Although the error message is the same, the error thrown when a conflict occurs during a transaction doesn’t have this property code, and we can assert that: e == dgraph.ERR_ABORTED.

In my project, I currently monkey-patch the library the following way…

// Change behavior of discard.
// When calling discard() after mutations, it throws the error "10 ABORTED: Transaction has been aborted. Please retry.";
const _discard = Txn.prototype.discard;
Txn.prototype.discard = async function() {
    try {
        await _discard.apply(this, arguments);
    } catch (e) {
        if (e.code != 10) throw e;
    }
};

It is annoying because, usually, the call to discard() occurs in a finally block, so the error is not handled properly.

What do you think about this issue? Is this a desired behavior?
Thanks in advance for your answers. :slight_smile:

paulftw commented :

@AlexandreDaSilva thank you for reporting this issue and taking your time to investigate it further.

It does look like a bug - i will try to reproduce it locally and if it’s still happening will definitely fix it.

I suspect this is caused by another write transaction that has been committed before discard() and made the one being discarded a conflict. But server code to check for transaction conflicts is probably executed before the check of whether we want to commit or discard, resulting in an unnecessary error.

But that’s a wild guess, will spend more time on it.