firebase-tools icon indicating copy to clipboard operation
firebase-tools copied to clipboard

Firestore runTransaction runs multiple times on emulator

Open oli-w opened this issue 7 years ago • 5 comments

Steps to reproduce:

  1. Install firebase-tools 7.8.0 globally with yarn / npm
  2. Run the emulators using firebase emulators:start
  3. Connect to the emulators on the client, e.g. on web by setting firebase.firestore().settings({ host: 'localhost:8081', ssl: false })
  4. Create a document in a transaction, e.g.
await firebase
    .firestore()
    .runTransaction(async () => {
        return firebase.firestore().collection('foo').add({ bar: 'baz' });
    });

Expected result: A single document is created.

Actual result: Multiple different documents are created (it appears to get stuck in a loop). There is an OPTIONS request in the browser console to http://localhost:8081/v1/projects/react-application-creator/databases/(default)/documents:commit which returns a 400 response (I'm running my local webpack dev server on localhost:3000), followed by a call to http://localhost:8081/google.firestore.v1.Firestore/Write/channel?database=... that appears to contain the new document data. These 2 calls keep looping over and over until the first promise seems to get rejected.

oli-w avatar Nov 20 '19 20:11 oli-w

Huh this is strange behavior. It's also worth noting that I don't actually think you're using the transaction at all ... you're not reading any data and you're not using the transaction object.

An add in a transaction normally looks like this:

// This is like add() but it only gives you the local ref
const docRef = firebase.firestore().collection('foo').doc();
await firebase
    .firestore()
    .runTransaction(async (t) => {
        // Notice the use of 't'
        return t.set(docRef, { bar: 'baz' });
    });

Even the example above does not do anything transactional. Firestore transactions are meant to help you do some reads, then do some writes that depend on those reads, and be sure that the data you read has not changed by the time you commit the writes.

All that said ... we definitely shouldn't be infinite looping here! So assigning to Ryan to look into this.

samtstern avatar Nov 20 '19 21:11 samtstern

Oh 🤦‍♂ I missed the point about using the parameter passed in to runTransaction. Using the transaction parameter properly fixes the problem, thanks! What I'm actually doing in my code is creating 2 different documents, which I want to be atomic (i.e. create both, or none). I simplified the reproduction case by only adding 1 write in the transaction. I realise that batched writes would actually be better in this case - it will be atomic and I don't need to read-then-write anything, it's only writes. So I'm going to refactor this code to do that 😄.

oli-w avatar Nov 20 '19 21:11 oli-w

I think @yuchenshi has a fix for this, re-assigning to them to add some details.

ryanpbrewster avatar Nov 20 '19 21:11 ryanpbrewster

@oli-w oh glad it's working for you! By the way a batched write and a transaction with no read operations are pretty much identical. So yes if I were you I'd use a batched write, the code is definitely simpler.

samtstern avatar Nov 20 '19 21:11 samtstern

I'm going to add that please make sure to use the canonical port (which defaults to 8080) instead of the WebChannel port (default 8081). emulators:start will print out a message explaining this. Otherwise you may get 400s like those mentioned in the original post.

yuchenshi avatar Nov 20 '19 21:11 yuchenshi