-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Array addToSet()
and push()
methods do not work with transaction retries
#14848
Comments
A temporary workaround until this is fixed: await mongoose.connection.transaction(async (session) => {
console.log(`Attempt: ${attempt}`);
// This! Apparently this resets something inside the Document so that
// `save()` works correctly for this field. Of course, if there are multiple
// fields that are being modified in this way, they would all have to be
// "reset" like this individually.
doc.items = doc.items;
await doc.save({ session });
if (attempt < retries) {
attempt += 1;
throw new MongoServerError({
message: "Test transient transaction failures & retries",
errorLabels: [MongoErrorLabel.TransientTransactionError],
});
}
}); |
Correction - it looks like
It does not say anything else. Since I am in a bit of hurry, I did not dig deeper and just used the workaround above. Perhaps some other methods are affected as well. |
I'm getting an issue when using .push() and then .save() in general, and it just started happening after a recent update (I'm on version 8.5.5, because versions 8.6.0 and 8.6.1 are giving me bugs when I try to filter by createdAt, which was working just fine before.) |
Actually, I just tried it with 8.6.1 and it still doesn't work. When I use Edit: just discovered that using |
@dmint789 I believe that's a separate issue. What I am seeing relates specifically to transaction retries. |
fix(transaction): avoid unnecessarily updating initial state in between transactions
Do you know if there is an open issue for the one I described? I thought this was supposed to be fixed already. |
@dmint789 can you please open a new issue and follow the issue template? It's hard to guess what the issue might be without code samples. |
Prerequisites
Mongoose version
8.6.0
Node.js version
20.16.0
MongoDB server version
5
Typescript version (if applicable)
N/A
Description
Hello there,
Scenario:
push()
.save()
d inside a transaction.Result: duplicate entries are saved. Actually, this is exactly the same as in #14340 - the total number of documents added to the array grows exponentially with every retry - it is 2^n, where n is the number of retries.
I chose not to reopen the old issue because I have a hunch that this time the cause might be different.
There is also a closely-related problem when using
addToSet()
. Other array methods (pop()
,pull()
,remove()
,set()
,shift()
and evensplice()
) all seem to be working correctly.My analysis
Let's take this for example (you'll find the full script below):
The first attempt to
save()
generates the followingupdateOne
query:If the transaction is retried, the second attempt to
save()
generates thisupdateOne
query:Attempt 3:
Etc. - as you can see, with each attempt the number of elements in
$push.items.$each
doubles.A side-effect of this problem is that the update query can grow significantly in size. If it grows too large, it might fail with one of these errors:
As a matter of fact, this was the first problem that I ran into since I noticed that saving after
addToSet()
was sometimes failing. Now I know that sometimes was when a transaction was retried a lot.Steps to Reproduce
The text was updated successfully, but these errors were encountered: