ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:114826] [Ruby master Bug#19894] Memory leak in complemented method entries
@ 2023-09-20  0:58 peterzhu2118 (Peter Zhu) via ruby-core
  2023-09-24  6:13 ` [ruby-core:114891] " nagachika (Tomoyuki Chikanaga) via ruby-core
  2023-10-17 11:43 ` [ruby-core:115066] " usa (Usaku NAKAMURA) via ruby-core
  0 siblings, 2 replies; 3+ messages in thread
From: peterzhu2118 (Peter Zhu) via ruby-core @ 2023-09-20  0:58 UTC (permalink / raw
  To: ruby-core; +Cc: peterzhu2118 (Peter Zhu)

Issue #19894 has been reported by peterzhu2118 (Peter Zhu).

----------------------------------------
Bug #19894: Memory leak in complemented method entries
https://bugs.ruby-lang.org/issues/19894

* Author: peterzhu2118 (Peter Zhu)
* Status: Open
* Priority: Normal
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: REQUIRED
----------------------------------------
GitHub pull request: https://github.com/ruby/ruby/pull/8481

When a copy of a complemented method entry is created, there are two issues:

1. IMEMO_FL_USER3 is not copied, so the complemented status is not copied over.
2. In rb_method_entry_clone we increment both alias_count and complemented_count. However, when we free the method entry in rb_method_definition_release, we only decrement one of the two counters, resulting in the rb_method_definition_t being leaked.

The following script reproduces this issue:

```ruby
10.times do
  20_000.times do
    $c = Class.new do
      def foo; end
    end

    $m = Module.new do
      refine $c do
        def foo; end
      end
    end

    Class.new do
      using $m

      def initialize
        o = $c.new
        o.method(:foo).unbind
      end
    end.new
  end

  puts `ps -o rss= -p #{$$}`
end
```

Before this fix:

```
 17328
 19248
 21408
 23296
 25600
 27408
 29424
 31520
 33728
 35664
```

After this fix:

```
 16240
 17680
 18800
 19744
 20640
 21840
 22896
 24336
 25280
 26096
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:114891] [Ruby master Bug#19894] Memory leak in complemented method entries
  2023-09-20  0:58 [ruby-core:114826] [Ruby master Bug#19894] Memory leak in complemented method entries peterzhu2118 (Peter Zhu) via ruby-core
@ 2023-09-24  6:13 ` nagachika (Tomoyuki Chikanaga) via ruby-core
  2023-10-17 11:43 ` [ruby-core:115066] " usa (Usaku NAKAMURA) via ruby-core
  1 sibling, 0 replies; 3+ messages in thread
From: nagachika (Tomoyuki Chikanaga) via ruby-core @ 2023-09-24  6:13 UTC (permalink / raw
  To: ruby-core; +Cc: nagachika (Tomoyuki Chikanaga)

Issue #19894 has been updated by nagachika (Tomoyuki Chikanaga).

Backport changed from 3.0: UNKNOWN, 3.1: REQUIRED, 3.2: REQUIRED to 3.0: UNKNOWN, 3.1: REQUIRED, 3.2: DONE

ruby_3_2 9ee58b2054c1bbe722ae5a2a4ec6a750ee583220 merged revision(s) 96c5a4be7b0d72502001734770af0f4a735c544c.

----------------------------------------
Bug #19894: Memory leak in complemented method entries
https://bugs.ruby-lang.org/issues/19894#change-104749

* Author: peterzhu2118 (Peter Zhu)
* Status: Closed
* Priority: Normal
* Backport: 3.0: UNKNOWN, 3.1: REQUIRED, 3.2: DONE
----------------------------------------
GitHub pull request: https://github.com/ruby/ruby/pull/8481

When a copy of a complemented method entry is created, there are two issues:

1. IMEMO_FL_USER3 is not copied, so the complemented status is not copied over.
2. In rb_method_entry_clone we increment both alias_count and complemented_count. However, when we free the method entry in rb_method_definition_release, we only decrement one of the two counters, resulting in the rb_method_definition_t being leaked.

The following script reproduces this issue:

```ruby
10.times do
  20_000.times do
    $c = Class.new do
      def foo; end
    end

    $m = Module.new do
      refine $c do
        def foo; end
      end
    end

    Class.new do
      using $m

      def initialize
        o = $c.new
        o.method(:foo).unbind
      end
    end.new
  end

  puts `ps -o rss= -p #{$$}`
end
```

Before this fix:

```
 17328
 19248
 21408
 23296
 25600
 27408
 29424
 31520
 33728
 35664
```

After this fix:

```
 16240
 17680
 18800
 19744
 20640
 21840
 22896
 24336
 25280
 26096
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [ruby-core:115066] [Ruby master Bug#19894] Memory leak in complemented method entries
  2023-09-20  0:58 [ruby-core:114826] [Ruby master Bug#19894] Memory leak in complemented method entries peterzhu2118 (Peter Zhu) via ruby-core
  2023-09-24  6:13 ` [ruby-core:114891] " nagachika (Tomoyuki Chikanaga) via ruby-core
@ 2023-10-17 11:43 ` usa (Usaku NAKAMURA) via ruby-core
  1 sibling, 0 replies; 3+ messages in thread
From: usa (Usaku NAKAMURA) via ruby-core @ 2023-10-17 11:43 UTC (permalink / raw
  To: ruby-core; +Cc: usa (Usaku NAKAMURA)

Issue #19894 has been updated by usa (Usaku NAKAMURA).

Backport changed from 3.0: UNKNOWN, 3.1: REQUIRED, 3.2: DONE to 3.0: UNKNOWN, 3.1: DONE, 3.2: DONE

ruby_3_1 a3e1444663005d3440b56c3a8e1b88bb28935912 merged revision(s) 96c5a4be7b0d72502001734770af0f4a735c544c.

----------------------------------------
Bug #19894: Memory leak in complemented method entries
https://bugs.ruby-lang.org/issues/19894#change-104941

* Author: peterzhu2118 (Peter Zhu)
* Status: Closed
* Priority: Normal
* Backport: 3.0: UNKNOWN, 3.1: DONE, 3.2: DONE
----------------------------------------
GitHub pull request: https://github.com/ruby/ruby/pull/8481

When a copy of a complemented method entry is created, there are two issues:

1. IMEMO_FL_USER3 is not copied, so the complemented status is not copied over.
2. In rb_method_entry_clone we increment both alias_count and complemented_count. However, when we free the method entry in rb_method_definition_release, we only decrement one of the two counters, resulting in the rb_method_definition_t being leaked.

The following script reproduces this issue:

```ruby
10.times do
  20_000.times do
    $c = Class.new do
      def foo; end
    end

    $m = Module.new do
      refine $c do
        def foo; end
      end
    end

    Class.new do
      using $m

      def initialize
        o = $c.new
        o.method(:foo).unbind
      end
    end.new
  end

  puts `ps -o rss= -p #{$$}`
end
```

Before this fix:

```
 17328
 19248
 21408
 23296
 25600
 27408
 29424
 31520
 33728
 35664
```

After this fix:

```
 16240
 17680
 18800
 19744
 20640
 21840
 22896
 24336
 25280
 26096
```



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-10-17 11:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-09-20  0:58 [ruby-core:114826] [Ruby master Bug#19894] Memory leak in complemented method entries peterzhu2118 (Peter Zhu) via ruby-core
2023-09-24  6:13 ` [ruby-core:114891] " nagachika (Tomoyuki Chikanaga) via ruby-core
2023-10-17 11:43 ` [ruby-core:115066] " usa (Usaku NAKAMURA) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).