Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-3599

"NoMethodError: undefined method `bytes'" in lib/bson/object_jd.rb

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Critical - P2 Critical - P2
    • None
    • Affects Version/s: 2.21.0
    • Component/s: BSON
    • None
    • Ruby Drivers
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      Have started seeing some strange errors emanating from bson gem when trying to insert a record via Mongoid. Error comes from lib/bson/object_id.id, in the `generate_data` method. The errors are mostly in our background jobs (for now) and they pass usually on the second attempt.

      This issue presents itself in numerous ways:

      • NoMethodError: undefined method `bytes' for an instance of Hash
      • NoMethodError: undefined method `bytes' for an instance of ActiveSupport::TimeWithZone
      • NotImplementedError: method `bytes' called on terminated object (0x00007f522f8b6fe0)
      • NoMethodError undefined method `bytes' for an instance of Datadog::Tracing::TraceSegment
      • NotImplementedError: method `bytes' called on hidden T_ARRAY object (0x00007f5eb2a36ae8 flags=0xc0002a807)
      • ArgumentError: wrong number of arguments (given 1, expected 0)

      All the above have the same stack trace:

      next_object_id(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/bson-5.0.1/lib/bson/object_id.rb:246)
      generate_data(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/bson-5.0.1/lib/bson/object_id.rb:246)
      to_bson(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/bson-5.0.1/lib/bson/object_id.rb:180)
      put_hash(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/bson-5.0.1/lib/bson/hash.rb:39)
      to_bson(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/bson-5.0.1/lib/bson/hash.rb:39)
      to_bson(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/protocol/caching_hash.rb:46)
      block (2 levels) in validate_document_size!(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/protocol/msg.rb:332)
      any?(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/protocol/msg.rb:331)
      block in validate_document_size!(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/protocol/msg.rb:331)
      any?(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/protocol/msg.rb:329)
      validate_document_size!(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/protocol/msg.rb:329)
      serialize(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/protocol/msg.rb:156)
      block in build_message(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/operation/shared/sessions_supported.rb:263)
      tap(<internal:kernel>:90)
      build_message(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/operation/shared/sessions_supported.rb:256)
      dispatch_message(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/operation/shared/executable.rb:112)
      get_result(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/operation/insert/op_msg.rb:38)
      block (4 levels) in do_execute(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/operation/shared/executable.rb:48)
      add_server_diagnostics(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/operation/shared/response_handling.rb:106)
      block (3 levels) in do_execute(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/operation/shared/executable.rb:47)
      check_for_network_error(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/operation/shared/executable.rb:169)
      block (2 levels) in do_execute(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/operation/shared/executable.rb:46)
      add_error_labels(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/operation/shared/response_handling.rb:53)
      block in do_execute(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/operation/shared/executable.rb:45)
      unpin_maybe(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/operation/shared/response_handling.rb:92)
      do_execute(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/operation/shared/executable.rb:44)
      execute(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/operation/shared/executable_no_validate.rb:28)
      execute_with_connection(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/operation/shared/write.rb:58)
      block (2 levels) in insert_one(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/collection.rb:880)
      block in modern_write_with_retry(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/retryable/write_worker.rb:247)
      with_connection(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/server/connection_pool.rb:721)
      with_connection(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/server.rb:495)
      modern_write_with_retry(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/retryable/write_worker.rb:236)
      write_with_retry(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/retryable/write_worker.rb:87)
      write_with_retry(/home/ubuntu/.asdf/installs/ruby/3.3.4/lib/ruby/3.3.0/forwardable.rb:240)
      block in insert_one(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/collection.rb:868)
      with_session(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/client.rb:1170)
      insert_one(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongo-2.21.0/lib/mongo/collection.rb:852)
      insert_as_root(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/persistable/creatable.rb:72)
      block in insert(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/persistable/creatable.rb:24)
      block (5 levels) in prepare_insert(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/persistable/creatable.rb:111)
      _mongoid_run_child_callbacks_with_around(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/interceptable.rb:168)
      _mongoid_run_child_callbacks(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/interceptable.rb:144)
      block (4 levels) in prepare_insert(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/persistable/creatable.rb:110)
      _mongoid_run_child_callbacks_with_around(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/interceptable.rb:168)
      _mongoid_run_child_callbacks(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/interceptable.rb:144)
      block (3 levels) in prepare_insert(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/persistable/creatable.rb:109)
      run_callbacks(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/activesupport-8.0.0/lib/active_support/callbacks.rb:100)
      run_callbacks(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/interceptable.rb:128)
      block (2 levels) in prepare_insert(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/persistable/creatable.rb:108)
      run_callbacks(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/activesupport-8.0.0/lib/active_support/callbacks.rb:109)
      run_callbacks(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/interceptable.rb:128)
      block in prepare_insert(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/persistable/creatable.rb:107)
      run_callbacks(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/activesupport-8.0.0/lib/active_support/callbacks.rb:100)
      run_callbacks(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/interceptable.rb:128)
      prepare_insert(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/persistable/creatable.rb:106)
      insert(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/persistable/creatable.rb:20)
      block in create!(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/persistable/creatable.rb:173)
      _creating(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/threaded/lifecycle.rb:142)
      create!(/home/ubuntu/xpand/shared/bundle/ruby/3.3.0/gems/mongoid-8.1.7/lib/mongoid/persistable/creatable.rb:168)
      create_log(/home/ubuntu/xpand/releases/20241202023617/lib/sidekiq/middleware/server/sidekiq_logs_middleware.rb:19)

      My hypothesis:

      The bson gem `next_object_id` method appears to be calling out to a native c function, `rb_bson_object_id_generator_next`. The error is happening on different objects (hash, array, terminated object). So is it possible that this bson next object id C function is fetching an object from a memory space that it shouldn't be?

      Stack Details:

      • Ruby 3.3.4 (using jemalloc)
      • Rails 8.0.0
      • Mongoid 8.1.7
      • Mongo 2.21.0
      • Bson 5.0.2

            Assignee:
            Unassigned Unassigned
            Reporter:
            kieran@xpand.io Kieran Pilkington
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: