Skip to content

Race conditions in transactions with SyncIndexWithObjectChangeListener #902

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

Closed
sneakyvv opened this issue Jun 17, 2019 · 5 comments
Closed
Labels

Comments

@sneakyvv
Copy link

The issue is that during a long or nested transaction, the sendUpdateIndexMessage() is called during a postPersist event, while the data may not yet be available in the database. The subsequent processing of the sent message (Doctrine/Queue/SyncIndexWithObjectChangeProcessor.php line 87) does not find the object and will send a delete command to Elastic instead of an insert.

This happens for example during a Doctrine fixtures load, which is actually several flushes inside nested transactions, but I have also seen it happen during a simple request (with queued processing). The problem is that the message is sent before the $conn->commit() is done in the (outer-)transaction's flush call. So none of the Doctrine lifecycle events is gonna guarantee there will be no race conditions.

The solution imo is to allow the message to be delivered with a delay.

@sneakyvv
Copy link
Author

@aumel
Copy link

aumel commented Jul 8, 2019

I encountered the same problem. I agree with @sneakyvv 's explanation. In PostPersist/PostUpdate, the DB transaction is not necessarily completed.

Unless I'm mistaken, there is indeed the usable PostFlush event. The PostFlush event is dispatched after the $conn->commit() (see: UnitOfWork.php).

Doctrine doc:

The postFlush event occurs when the EntityManager#flush() operation is invoked and after all actual database operations are executed successfully.

In SyncIndexWithObjectChangeListener.php, we could do this:

...
public function postUpdate(LifecycleEventArgs $args)
    {
        if ($args->getObject() instanceof $this->modelClass) {
            $this->triggers[] = ['action' => SyncProcessor::UPDATE_ACTION, 'args' => $args];
        }
    }

    public function postPersist(LifecycleEventArgs $args)
    {
        if ($args->getObject() instanceof $this->modelClass) {
            $this->triggers[] = ['action' => SyncProcessor::INSERT_ACTION, 'args' => $args];
        }
    }

    public function preRemove(LifecycleEventArgs $args)
    {
        if ($args->getObject() instanceof $this->modelClass) {
            $this->triggers[] = ['action' => SyncProcessor::REMOVE_ACTION, 'args' => $args];
        }
    }

    public function postFlush(PostFlushEventArgs $event)
    {
        if (!empty($this->triggers)) {
            foreach ($this->triggers as $trigger) {
                $this->sendUpdateIndexMessage($trigger['action'], $trigger['args']);
            }

            $this->triggers = [];
        }
    }
...

IMO, the solution with PostFlush is "safer" than the delay.

@sneakyvv
Copy link
Author

sneakyvv commented Jul 8, 2019

Even the flush event cannot help during nested transactions. Every flush() is sending this event, but only the outer transaction really commits it to the database.

So, I guess this is a Doctrine issue (too?).

@aumel
Copy link

aumel commented Jul 8, 2019

There are two problems (or contexts):

  1. Non-nested transaction and the data may not yet be available in the database.
  2. Nested transaction and the data may not yet be available in the database.

For the first problem, I think that enqueue-elastica should at least follow the implementation of FosElasticaBundle (see: FOS\ElasticaBundle\Doctrine\Listener) and sendUpdateIndexMessage on PostFlush.

For the second, you're right. Doctrine events cannot help during nested transactions. It does not emit a PostFlush event in this case. You will encounter also this problem with FosElasticaBundle (without enqueue). You could dispatch the PostFlush event yourself. Maybe this could be useful: doctrine/orm#7103 (comment).

@stale
Copy link

stale bot commented Aug 7, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix label Aug 7, 2019
@stale stale bot closed this as completed Aug 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants