Running Mongoid queries via async leads to state corruption

XMLWordPrintableJSON

    • Type: Bug
    • Resolution: Unresolved
    • Priority: Major - P3
    • None
    • Affects Version/s: 9.0.9
    • Component/s: None
    • None
    • None
    • Ruby Drivers
    • None
    • None
    • None
    • None
    • None
    • None

      As reported on GitHub: https://github.com/mongodb/mongoid/pull/6009#issuecomment-3720769117

      The following script demonstrates the issue, which appears to be some sort of synchronization problem with the document cache.

      # frozen_string_literal: true
      
      require "bundler/inline"
      require "logger"
      
      gemfile do
        source "https://rubygems.org"
      
        gem "mongoid", "~> 9.0.9"
        gem "async", ">= 2.34"
      end
      
      Mongoid.connect_to 'sandbox'
      Mongoid.purge!
      
      puts "========================"
      puts "ruby:    #{RUBY_VERSION}"
      puts "mongo:   #{Mongo::VERSION}"
      puts "mongoid: #{Mongoid::VERSION}"
      puts "========================"
      
      class Movie
        include Mongoid::Document
        field :canonical_url, type: String
        field :title, type: String
        field :slug, type: Integer
        field :adult, type: Mongoid::Boolean
      end
      
      Movie.create!(title: "movie name", slug: 44621, adult: false)
      
      def run(concurrency = 10)
        require 'kernel/barrier'
      
        Barrier do |barrier|
          concurrency.times do
            barrier.async do
              lookup_one = Movie.only(:title, :slug).find_by(slug: 44621)
              lookup_two = Movie.find_by(slug: 44621)
              lookup_two.adult
            end
          end
        end
      end
      
      run 10
      

      Running this will result in a number of Mongoid::Errors::AttributeNotLoaded errors, apparently due to the cached document (lookup_one) not having the adult attribute that is requested on lookup_two.

            Assignee:
            Jamis Buck
            Reporter:
            Jamis Buck
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: