Discussion
Loading...

Post

Log in
  • About
  • Code of conduct
  • Privacy
  • Users
  • Instances
  • About Bonfire
Bradley Schaefer
Bradley Schaefer
@soulcutter@ruby.social  ·  activity timestamp 6 days ago

I've been looking at memory leaks today, and found this in the ThreadLocalVar docs in ruby-concurrency:

> Ruby's built-in thread-local variables leak forever the value set on each thread (unless set to nil explicitly).

Damn, that feels like a "Never use Thread.current ever", right? Very risky. Do y'all agree?

https://ruby-concurrency.github.io/concurrent-ruby/master/Concurrent/ThreadLocalVar.html

#ruby

Class: Concurrent::ThreadLocalVar

— Concurrent Ruby
  • Copy link
  • Flag this post
  • Block
Bradley Schaefer
Bradley Schaefer
@soulcutter@ruby.social replied  ·  activity timestamp 6 days ago

Followup: I had a hard time believing that values that you set in Thread.current[:thing] leak, so

I wrote a script that continuously spawns tons of threads and assigns Thread.current[:thing] with new objects.

Then another thread reported ObjectSpace stats every second.

I could find no evidence of a leak that way. False alarm?

But also, I noticed that Thread.current#[] is actually Fiber-local and not the same as Thread.thread_variable_[gs]et 🤔 I doubt that's any different leak-wise

  • Copy link
  • Flag this comment
  • Block
Esparta :ruby:
Esparta :ruby:
@esparta@ruby.social replied  ·  activity timestamp 6 days ago

@soulcutter I was surprised when you pointed that part of the documentation, and I'm pretty sure that was true in the past and it doesn't stand with current state of affairs leak-wise.

What I would do is compare your scripts with the specs for ThreadLocalVar, they surely have assertions for/about leaks.

  • Copy link
  • Flag this comment
  • Block
Esparta :ruby:
Esparta :ruby:
@esparta@ruby.social replied  ·  activity timestamp 6 days ago

@soulcutter this is probably still true:

> Each Ruby's built-in thread-local variable leaks some memory forever

Because you declare them like this:

Thread.current.thread_variable_set(:foo, 1)

That `: foo` lives forever since the symbols are not GC'd.

  • Copy link
  • Flag this comment
  • Block
Jamie Gaskins
Jamie Gaskins
@jamie@zomglol.wtf replied  ·  activity timestamp 6 days ago

@esparta @soulcutter That last part used to be true, but Ruby has been able to GC symbols since 2.2.

https://www.ruby-lang.org/en/news/2014/12/25/ruby-2-2-0-released/

Ruby Programming Language

Ruby 2.2.0 Released | Ruby

We are pleased to announce the release of Ruby 2.2.0.
  • Copy link
  • Flag this comment
  • Block
Bradley Schaefer
Bradley Schaefer
@soulcutter@ruby.social replied  ·  activity timestamp 6 days ago

@jamie @esparta nice. I need to GC my brain and get rid of some of those old, outdated facts

  • Copy link
  • Flag this comment
  • Block
Esparta :ruby:
Esparta :ruby:
@esparta@ruby.social replied  ·  activity timestamp 6 days ago

@soulcutter @jamie yeap, that's true I also forgot that symbols are GC-ables.

Then most of the old sayings are currently not so true anymore, I think an update on the documentation would be nice to have. Hope I can have time later.

  • Copy link
  • Flag this comment
  • Block
Bradley Schaefer
Bradley Schaefer
@soulcutter@ruby.social replied  ·  activity timestamp 6 days ago

@esparta symbols are basically interned strings though, so to leak that way I think you’d have to have unique symbols and not :foo in every thread. That’s my mental model anyway

  • Copy link
  • Flag this comment
  • Block

bonfire.cafe

A space for Bonfire maintainers and contributors to communicate

bonfire.cafe: About · Code of conduct · Privacy · Users · Instances
Bonfire social · 1.0.2-alpha.7 no JS en
Automatic federation enabled
Log in
  • Explore
  • About
  • Members
  • Code of Conduct