ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:117027] [Ruby master Bug#20319] Singleton class is being frozen lazily in some cases
@ 2024-03-01 11:52 andrykonchin (Andrew Konchin) via ruby-core
  2024-03-01 12:04 ` [ruby-core:117028] " Eregon (Benoit Daloze) via ruby-core
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: andrykonchin (Andrew Konchin) via ruby-core @ 2024-03-01 11:52 UTC (permalink / raw
  To: ruby-core; +Cc: andrykonchin (Andrew Konchin)

Issue #20319 has been reported by andrykonchin (Andrew Konchin).

----------------------------------------
Bug #20319: Singleton class is being frozen lazily in some cases
https://bugs.ruby-lang.org/issues/20319

* Author: andrykonchin (Andrew Konchin)
* Status: Open
* ruby -v: 3.2.2
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
I've noticed suspicious behaviour (it doesn't affect anything in practice for me though) when an object becomes frozen only its own singleton class becomes frozen immediately.

A singleton class of the immediate singleton class becomes frozen lazily after `#singleton_class` method call:

```ruby
o = Object.new
klass = o.singleton_class.singleton_class

o.freeze

puts klass.frozen?                              # false <== here we expect true
puts o.singleton_class.singleton_class.frozen?  # true
puts klass.frozen?                              # true
```

I would expect all created singleton classes in an object singleton classes chain to become frozen immediately.




-- 
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] 8+ messages in thread

* [ruby-core:117028] [Ruby master Bug#20319] Singleton class is being frozen lazily in some cases
  2024-03-01 11:52 [ruby-core:117027] [Ruby master Bug#20319] Singleton class is being frozen lazily in some cases andrykonchin (Andrew Konchin) via ruby-core
@ 2024-03-01 12:04 ` Eregon (Benoit Daloze) via ruby-core
  2024-03-04 17:46 ` [ruby-core:117054] " jeremyevans0 (Jeremy Evans) via ruby-core
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2024-03-01 12:04 UTC (permalink / raw
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

Issue #20319 has been updated by Eregon (Benoit Daloze).


Alternatively, I think we could simplify `Kernel#freeze` to always only freeze the direct object and no other object, i.e. no singleton class would be frozen when freezing a singleton object.
State and methods are separate things, so IMO it doesn't gain anything to freeze singleton classes of a singleton object on `singleton_object.freeze`.
The methods can still be modified e.g. using `singleton_object.class.class_exec { def foo = 42 }`.

I think Kernel#freeze should only freeze the object itself, which means instance variables (and internal state if core/TypedStruct object) cannot be changed.
That would be both more consistent semantically and more efficient, while avoiding weird cases like the above.

----------------------------------------
Bug #20319: Singleton class is being frozen lazily in some cases
https://bugs.ruby-lang.org/issues/20319#change-107097

* Author: andrykonchin (Andrew Konchin)
* Status: Open
* ruby -v: 3.2.2
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
I've noticed suspicious behaviour (it doesn't affect anything in practice for me though) when an object becomes frozen only its own singleton class becomes frozen immediately.

A singleton class of the object immediate singleton class becomes frozen lazily after `#singleton_class` method call:

```ruby
o = Object.new
klass = o.singleton_class.singleton_class

o.freeze

puts klass.frozen?                              # false <== here we expect true
puts o.singleton_class.singleton_class.frozen?  # true
puts klass.frozen?                              # true
```

I would expect all created (and visible to user) singleton classes in an object singleton classes chain to become frozen immediately when the object gets frozen.




-- 
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] 8+ messages in thread

* [ruby-core:117054] [Ruby master Bug#20319] Singleton class is being frozen lazily in some cases
  2024-03-01 11:52 [ruby-core:117027] [Ruby master Bug#20319] Singleton class is being frozen lazily in some cases andrykonchin (Andrew Konchin) via ruby-core
  2024-03-01 12:04 ` [ruby-core:117028] " Eregon (Benoit Daloze) via ruby-core
@ 2024-03-04 17:46 ` jeremyevans0 (Jeremy Evans) via ruby-core
  2024-03-11 13:35 ` [ruby-core:117099] " Eregon (Benoit Daloze) via ruby-core
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: jeremyevans0 (Jeremy Evans) via ruby-core @ 2024-03-04 17:46 UTC (permalink / raw
  To: ruby-core; +Cc: jeremyevans0 (Jeremy Evans)

Issue #20319 has been updated by jeremyevans0 (Jeremy Evans).


Eregon (Benoit Daloze) wrote in #note-3:
> I think Kernel#freeze should only freeze the object itself, which means instance variables (and internal state if core/TypedStruct object) cannot be changed.
> That would be both more consistent semantically and more efficient, while avoiding weird cases like the above.

I disagree.  If you do not freeze the object's singleton class, then you can define or undefine any method in the singleton class, which is almost the same as being able to modify the object (from a Ruby perspective, not a C perspective).

However, redefining methods in the singleton class of the singleton class of the object does not allow you to modify the object, it only allows you to modify the object's singleton class.  However, as the object's singleton class is frozen, we still should prevent it.

This bug should be fairly simple to fix by having `Kernel#freeze` go up the singleton class chain and freeze all currently instantiated singleton classes.

----------------------------------------
Bug #20319: Singleton class is being frozen lazily in some cases
https://bugs.ruby-lang.org/issues/20319#change-107128

* Author: andrykonchin (Andrew Konchin)
* Status: Open
* ruby -v: 3.2.2
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
I've noticed suspicious behaviour (it doesn't affect anything in practice for me though) when an object becomes frozen only its own singleton class becomes frozen immediately.

A singleton class of the object immediate singleton class becomes frozen lazily after `#singleton_class` method call:

```ruby
o = Object.new
klass = o.singleton_class.singleton_class

o.freeze

puts klass.frozen?                              # false <== here we expect true
puts o.singleton_class.singleton_class.frozen?  # true
puts klass.frozen?                              # true
```

I would expect all created (and visible to user) singleton classes in an object singleton classes chain to become frozen immediately when the object gets frozen.




-- 
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] 8+ messages in thread

* [ruby-core:117099] [Ruby master Bug#20319] Singleton class is being frozen lazily in some cases
  2024-03-01 11:52 [ruby-core:117027] [Ruby master Bug#20319] Singleton class is being frozen lazily in some cases andrykonchin (Andrew Konchin) via ruby-core
  2024-03-01 12:04 ` [ruby-core:117028] " Eregon (Benoit Daloze) via ruby-core
  2024-03-04 17:46 ` [ruby-core:117054] " jeremyevans0 (Jeremy Evans) via ruby-core
@ 2024-03-11 13:35 ` Eregon (Benoit Daloze) via ruby-core
  2024-03-11 13:37 ` [ruby-core:117100] " Eregon (Benoit Daloze) via ruby-core
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2024-03-11 13:35 UTC (permalink / raw
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

Issue #20319 has been updated by Eregon (Benoit Daloze).


jeremyevans0 (Jeremy Evans) wrote in #note-4:
> I disagree.  If you do not freeze the object's singleton class, then you can define or undefine any method in the singleton class, which is almost the same as being able to modify the object (from a Ruby perspective, not a C perspective).

One can still define or undefine any method in one of the ancestors (except the singleton class), or use `prepend`/`include` on any of these or even `refine` to change the behavior of that object.
Freezing the singleton class achieves very little IMO and is inconsistent with `Kernel#freeze` being a shallow freeze (that is, only freeze the immediate object, not other objects, which AFAIK holds for all other cases).

----------------------------------------
Bug #20319: Singleton class is being frozen lazily in some cases
https://bugs.ruby-lang.org/issues/20319#change-107172

* Author: andrykonchin (Andrew Konchin)
* Status: Open
* ruby -v: 3.2.2
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
I've noticed suspicious behaviour (it doesn't affect anything in practice for me though) when an object becomes frozen only its own singleton class becomes frozen immediately.

A singleton class of the object immediate singleton class becomes frozen lazily after `#singleton_class` method call:

```ruby
o = Object.new
klass = o.singleton_class.singleton_class

o.freeze

puts klass.frozen?                              # false <== here we expect true
puts o.singleton_class.singleton_class.frozen?  # true
puts klass.frozen?                              # true
```

I would expect all created (and visible to user) singleton classes in an object singleton classes chain to become frozen immediately when the object gets frozen.




-- 
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] 8+ messages in thread

* [ruby-core:117100] [Ruby master Bug#20319] Singleton class is being frozen lazily in some cases
  2024-03-01 11:52 [ruby-core:117027] [Ruby master Bug#20319] Singleton class is being frozen lazily in some cases andrykonchin (Andrew Konchin) via ruby-core
                   ` (2 preceding siblings ...)
  2024-03-11 13:35 ` [ruby-core:117099] " Eregon (Benoit Daloze) via ruby-core
@ 2024-03-11 13:37 ` Eregon (Benoit Daloze) via ruby-core
  2024-03-14  1:37 ` [ruby-core:117134] " jeremyevans0 (Jeremy Evans) via ruby-core
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2024-03-11 13:37 UTC (permalink / raw
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

Issue #20319 has been updated by Eregon (Benoit Daloze).


> This bug should be fairly simple to fix by having `Kernel#freeze` go up the singleton class chain and freeze all currently instantiated singleton classes.

Yeah, that seems the smaller fix for now and at least it fixes a "should-be-idempotent method like singleton_class" dynamically freezing an existing singleton class.
Would you be able to make a PR for that?



----------------------------------------
Bug #20319: Singleton class is being frozen lazily in some cases
https://bugs.ruby-lang.org/issues/20319#change-107173

* Author: andrykonchin (Andrew Konchin)
* Status: Open
* ruby -v: 3.2.2
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
I've noticed suspicious behaviour (it doesn't affect anything in practice for me though) when an object becomes frozen only its own singleton class becomes frozen immediately.

A singleton class of the object immediate singleton class becomes frozen lazily after `#singleton_class` method call:

```ruby
o = Object.new
klass = o.singleton_class.singleton_class

o.freeze

puts klass.frozen?                              # false <== here we expect true
puts o.singleton_class.singleton_class.frozen?  # true
puts klass.frozen?                              # true
```

I would expect all created (and visible to user) singleton classes in an object singleton classes chain to become frozen immediately when the object gets frozen.




-- 
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] 8+ messages in thread

* [ruby-core:117134] [Ruby master Bug#20319] Singleton class is being frozen lazily in some cases
  2024-03-01 11:52 [ruby-core:117027] [Ruby master Bug#20319] Singleton class is being frozen lazily in some cases andrykonchin (Andrew Konchin) via ruby-core
                   ` (3 preceding siblings ...)
  2024-03-11 13:37 ` [ruby-core:117100] " Eregon (Benoit Daloze) via ruby-core
@ 2024-03-14  1:37 ` jeremyevans0 (Jeremy Evans) via ruby-core
  2024-04-18 11:44 ` [ruby-core:117597] " Eregon (Benoit Daloze) via ruby-core
  2024-06-06  9:41 ` [ruby-core:118208] " mame (Yusuke Endoh) via ruby-core
  6 siblings, 0 replies; 8+ messages in thread
From: jeremyevans0 (Jeremy Evans) via ruby-core @ 2024-03-14  1:37 UTC (permalink / raw
  To: ruby-core; +Cc: jeremyevans0 (Jeremy Evans)

Issue #20319 has been updated by jeremyevans0 (Jeremy Evans).


I looked into this and the current behavior is deliberate, as there is a comment stating `should not propagate to meta-meta-class` (see `rb_freeze_singleton_class` definition in `class.c`). See related commit commit:d9a597408f0f192ff25ab51e1e6733fe6fefc01b.

Freezing the singleton class of the singleton class breaks `TestModule#test_frozen_singleton_class`. I came up with a solution that fixes this by walking down the attached object chain to make sure it ends in the object being frozen.  Not sure that is correct, but it does appear to pass CI: https://github.com/ruby/ruby/pull/10245

----------------------------------------
Bug #20319: Singleton class is being frozen lazily in some cases
https://bugs.ruby-lang.org/issues/20319#change-107210

* Author: andrykonchin (Andrew Konchin)
* Status: Open
* ruby -v: 3.2.2
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
I've noticed suspicious behaviour (it doesn't affect anything in practice for me though) when an object becomes frozen only its own singleton class becomes frozen immediately.

A singleton class of the object immediate singleton class becomes frozen lazily after `#singleton_class` method call:

```ruby
o = Object.new
klass = o.singleton_class.singleton_class

o.freeze

puts klass.frozen?                              # false <== here we expect true
puts o.singleton_class.singleton_class.frozen?  # true
puts klass.frozen?                              # true
```

I would expect all created (and visible to user) singleton classes in an object singleton classes chain to become frozen immediately when the object gets frozen.




-- 
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] 8+ messages in thread

* [ruby-core:117597] [Ruby master Bug#20319] Singleton class is being frozen lazily in some cases
  2024-03-01 11:52 [ruby-core:117027] [Ruby master Bug#20319] Singleton class is being frozen lazily in some cases andrykonchin (Andrew Konchin) via ruby-core
                   ` (4 preceding siblings ...)
  2024-03-14  1:37 ` [ruby-core:117134] " jeremyevans0 (Jeremy Evans) via ruby-core
@ 2024-04-18 11:44 ` Eregon (Benoit Daloze) via ruby-core
  2024-06-06  9:41 ` [ruby-core:118208] " mame (Yusuke Endoh) via ruby-core
  6 siblings, 0 replies; 8+ messages in thread
From: Eregon (Benoit Daloze) via ruby-core @ 2024-04-18 11:44 UTC (permalink / raw
  To: ruby-core; +Cc: Eregon (Benoit Daloze)

Issue #20319 has been updated by Eregon (Benoit Daloze).


@nobu WDYT? Could you review that PR?

----------------------------------------
Bug #20319: Singleton class is being frozen lazily in some cases
https://bugs.ruby-lang.org/issues/20319#change-108006

* Author: andrykonchin (Andrew Konchin)
* Status: Open
* ruby -v: 3.2.2
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
I've noticed suspicious behaviour (it doesn't affect anything in practice for me though) when an object becomes frozen only its own singleton class becomes frozen immediately.

A singleton class of the object immediate singleton class becomes frozen lazily after `#singleton_class` method call:

```ruby
o = Object.new
klass = o.singleton_class.singleton_class

o.freeze

puts klass.frozen?                              # false <== here we expect true
puts o.singleton_class.singleton_class.frozen?  # true
puts klass.frozen?                              # true
```

I would expect all created (and visible to user) singleton classes in an object singleton classes chain to become frozen immediately when the object gets frozen.




-- 
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] 8+ messages in thread

* [ruby-core:118208] [Ruby master Bug#20319] Singleton class is being frozen lazily in some cases
  2024-03-01 11:52 [ruby-core:117027] [Ruby master Bug#20319] Singleton class is being frozen lazily in some cases andrykonchin (Andrew Konchin) via ruby-core
                   ` (5 preceding siblings ...)
  2024-04-18 11:44 ` [ruby-core:117597] " Eregon (Benoit Daloze) via ruby-core
@ 2024-06-06  9:41 ` mame (Yusuke Endoh) via ruby-core
  6 siblings, 0 replies; 8+ messages in thread
From: mame (Yusuke Endoh) via ruby-core @ 2024-06-06  9:41 UTC (permalink / raw
  To: ruby-core; +Cc: mame (Yusuke Endoh)

Issue #20319 has been updated by mame (Yusuke Endoh).


Discussed at the dev meeting, but no one had a deep understanding of Jeremy's patch, and matz was unable to make any decision.

----------------------------------------
Bug #20319: Singleton class is being frozen lazily in some cases
https://bugs.ruby-lang.org/issues/20319#change-108695

* Author: andrykonchin (Andrew Konchin)
* Status: Open
* ruby -v: 3.2.2
* Backport: 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN
----------------------------------------
I've noticed suspicious behaviour (it doesn't affect anything in practice for me though) when an object becomes frozen only its own singleton class becomes frozen immediately.

A singleton class of the object immediate singleton class becomes frozen lazily after `#singleton_class` method call:

```ruby
o = Object.new
klass = o.singleton_class.singleton_class

o.freeze

puts klass.frozen?                              # false <== here we expect true
puts o.singleton_class.singleton_class.frozen?  # true
puts klass.frozen?                              # true
```

I would expect all created (and visible to user) singleton classes in an object singleton classes chain to become frozen immediately when the object gets frozen.




-- 
https://bugs.ruby-lang.org/

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

end of thread, other threads:[~2024-06-06  9:42 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-01 11:52 [ruby-core:117027] [Ruby master Bug#20319] Singleton class is being frozen lazily in some cases andrykonchin (Andrew Konchin) via ruby-core
2024-03-01 12:04 ` [ruby-core:117028] " Eregon (Benoit Daloze) via ruby-core
2024-03-04 17:46 ` [ruby-core:117054] " jeremyevans0 (Jeremy Evans) via ruby-core
2024-03-11 13:35 ` [ruby-core:117099] " Eregon (Benoit Daloze) via ruby-core
2024-03-11 13:37 ` [ruby-core:117100] " Eregon (Benoit Daloze) via ruby-core
2024-03-14  1:37 ` [ruby-core:117134] " jeremyevans0 (Jeremy Evans) via ruby-core
2024-04-18 11:44 ` [ruby-core:117597] " Eregon (Benoit Daloze) via ruby-core
2024-06-06  9:41 ` [ruby-core:118208] " mame (Yusuke Endoh) 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).