Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
vtgate/engine: Fix race condition in join logic
This change fixes a race condition in the join logic. This is a classic check-then-act style bug. The problem is that we were using a field to track if we already sent fields. This can lead to a concurrency issue though: - Thread 1 checks if it needs to send fields and sees that it does. - Thread 2 checks if it needs to send fields and sees it does not. - Thread 2 sends the result with no fields. - Downstream consumers crash because they get a first result set with no fields. There's no locking around the critical section of checking if we need to send fields and actually calling the callback. We need both those to be covered under the same critical section. This change here introduces a lock around this to make sure this happens. With the lock, it normally would not be necessary to use an atomic boolean flag, as we'd only read and write the flag inside the lock. We have another optimization here though, which is that we only want to ask the right hand side for fields once as well but it's called in each callback for the left hand side. By using still an atomic, we can also read this value too. Note that this is still actually racy, but it's has a worse case condition of reading the fields of the right hand side twice which is not optimal, but it will not break things. Signed-off-by: Dirkjan Bussink <[email protected]>
- Loading branch information