Skip to content
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

MONGOID-5820 Fix the session object leak when using transactions #5878

Merged
merged 6 commits into from
Oct 23, 2024

Conversation

Virus-X
Copy link
Contributor

@Virus-X Virus-X commented Oct 10, 2024

The issue

I found the memory leak inside Threaded class, when saving documents within transaction: session object is retained forever inside thread-local variable.

Affected version: Mongoid 9.0.2 / Ruby 3.3.4

Explanation

  • Mongoid uses thread-local variables to temporarily hold modified_documents until transaction is committed.
  • The [mongoid]:modified-documents contains a hash with session as key and Set of documents as value.
  • During transaction commit phase, the Mongoid::Threaded::clear_modified_documents is called, which clears documents from the set, however, the hash inside [mongoid]:modified-documents still contains a session object as a key.
  • This session object is then persists in memory, even after the session was finished by the Mongoid::Threaded::clear_session call.

Testing

I used the following code to reproduce the issue:

require_relative 'spec_helper'
require 'memory_profiler'

describe "Mongoid test" do
  let(:model) do
    Class.new do
      include Mongoid::Document

      store_in collection: 'tests'
      field :name, type: String
    end
  end

  context 'memory leak test' do
    it 'store in transaction' do
      run_memory_profiling do
        model.transaction do
          model.create!(name: 'test')
        end
      end
    end
  end

  def run_memory_profiling(&block)
    # Warmup
    yield

    report = MemoryProfiler.report do
      2000.times(&block)
      GC.start(full_mark: true, immediate_sweep: true)
    end
    p "Total retained: #{report.total_retained_memsize / 1024} KB (#{report.total_retained} objects). "
  end
end

Initial results:

Total retained: 3430 KB (36053 objects). 

Report shows a number of retained objects, which corresponds to 2000 iterations of test:
image

Results after modification:

Total retained: 6 KB (61 objects).

@comandeo-mongo comandeo-mongo changed the title Fixed the session object memory leak when using transactions MONGOID-5820 Fix the session object memory leak when using transactions Oct 14, 2024
@comandeo-mongo comandeo-mongo changed the title MONGOID-5820 Fix the session object memory leak when using transactions MONGOID-5820 Fix the session object leak when using transactions Oct 14, 2024
@comandeo-mongo comandeo-mongo requested a review from jamis October 22, 2024 14:59
Copy link
Contributor

@jamis jamis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Just one question about the commented-out bit in lite_spec_helper.rb, but I've got no material concerns.

Comment on lines 85 to 91
# if SpecConfig.instance.ci? && !%w(1 true yes).include?(ENV['INTERACTIVE']&.downcase)
# config.around(:each) do |example|
# TimeoutInterrupt.timeout(example_timeout_seconds) do
# example.run
# end
# end
# end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these lines commented out because they might come back? Should we just delete them and rely on git to remember them if we need to resurrect them?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, good catch! This should be removed eventually, I believe, but not in scope of this PR. I'll revert.

@comandeo-mongo comandeo-mongo merged commit d06012d into mongodb:master Oct 23, 2024
57 of 61 checks passed
@comandeo-mongo
Copy link
Contributor

Hi @Virus-X , thank you very much for your contribution! I am sorry it takes me so much time to merge it. The reason was that we needed a test for the change; and while adding the test I discovered one more related issue that was also fixed.

comandeo-mongo added a commit to comandeo-mongo/mongoid that referenced this pull request Oct 23, 2024
comandeo-mongo added a commit that referenced this pull request Oct 23, 2024
@Virus-X
Copy link
Contributor Author

Virus-X commented Oct 24, 2024

Thanks! Was glad to help!

@Virus-X Virus-X deleted the session-memory-leak branch October 24, 2024 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants