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

[Mono] Implement eager finalization of WeakReference #76173

Merged
merged 3 commits into from
Sep 29, 2022

Conversation

filipnavara
Copy link
Member

@filipnavara filipnavara commented Sep 26, 2022

No description provided.

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Sep 26, 2022
@naricc
Copy link
Member

naricc commented Sep 26, 2022

I'm not sure I understand what is happening here. What does making finalization eager actually mean for WeakRefs? What is the behavior supposed to be?

@VSadov
Copy link
Member

VSadov commented Sep 26, 2022

WeakReference types are finalizable types. Weak references could be used in big quantities and that can have bad effects on finalization queue.
The finalization of weak references is very trivial though - it is basically closing the underlying handle. It is generally cheaper to just close the handle when GC finds unreachable weak reference object, instead of posting it to the finalization queue.

The eager approach also has some reliability advantages. Since eager finalization happens when managed threads are stopped, there is no chance of getting into races between finalization and concurrent use of the weak references from other threads.

CoreCLR had eager finalization of weak references for a long time. NativeAOT has added that recently - #75436

Another advantage is that if Mono implements eager finalization, it would be able to use the shared implementation of WeakReference and WeakReference<T> without any ifdefs.

@filipnavara
Copy link
Member Author

Thanks @VSadov for explaining it better than I would 👍

Copy link
Member

@lambdageek lambdageek left a comment

Choose a reason for hiding this comment

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

lgtm. although i'm not sure we should put weakreference into mono_defaults

src/mono/mono/metadata/class-internals.h Show resolved Hide resolved
@BrzVlad
Copy link
Member

BrzVlad commented Sep 27, 2022

This change could further be improved by avoiding a useless object allocation. In sgen_finalize_in_range, copy_func is used to promote / mark the object, since it still needs to be alive to be passed as this to the finalizer. This should no longer be needed if it's eagerly finalized.

@VSadov
Copy link
Member

VSadov commented Sep 27, 2022

@BrzVlad As I understand the suggestion, we need to move the specialcasing of weak references a bit earlier - from sgen_queue_finalization_entry up into sgen_finalize_in_range. Basically instead of copy_func (&copy, queue); we can just SGEN_HASH_TABLE_FOREACH_REMOVE (TRUE); and continue.

Is this the idea?

@BrzVlad
Copy link
Member

BrzVlad commented Sep 27, 2022

@VSadov Yes, that is correct

@filipnavara
Copy link
Member Author

Thanks for taking care of the last feedback, @VSadov!

@VSadov
Copy link
Member

VSadov commented Sep 29, 2022

I assume the change addresses the suggestions and once llvmfullaot finishes we can merge the change.

@BrzVlad BrzVlad merged commit 4287453 into dotnet:main Sep 29, 2022
@filipnavara filipnavara deleted the mono-eager-weakref branch September 29, 2022 07:08
@uweigand
Copy link
Contributor

As of this commit, I'm now seeing failures when running the libs.tests suite on s390x (Mono-based runtime):

  Process terminated. Assertion failed.
  WeakReference<T> finalizer should never run

Any suggestions what this could be, or how to further debug?

@BrzVlad
Copy link
Member

BrzVlad commented Oct 11, 2022

I remember seeing this assertion locally but I didn't get to investigate it. I'll see if I can repro this week

@filipnavara
Copy link
Member Author

@uweigand Do you have any details about which test is failing?

@uweigand
Copy link
Contributor

@uweigand Do you have any details about which test is failing?

It varies, I've seen multiple different tests failing. One that seems to fail most of the time is System.IO.FileSystem.Watcher.Tests.

@uweigand
Copy link
Contributor

Also interesting is that for some unknown reason, I'm not seeing any of these failures in the CI here:
https://dev.azure.com/dnceng-public/public/_build?definitionId=148
(which is doing a cross-build followed by native execution), but I'm seeing the failures all the time when building + testing fully natively on my own s390x machine.

@BrzVlad
Copy link
Member

BrzVlad commented Oct 11, 2022

@uweigand Do you have any details about which test is failing?

It varies, I've seen multiple different tests failing. One that seems to fail most of the time is System.IO.FileSystem.Watcher.Tests.

I can reproduce, will take a look later

@uweigand
Copy link
Contributor

@uweigand Do you have any details about which test is failing?

It varies, I've seen multiple different tests failing. One that seems to fail most of the time is System.IO.FileSystem.Watcher.Tests.

I can reproduce, will take a look later

@BrzVlad did you have a chance to look into this?

simonrozsival added a commit to simonrozsival/runtime that referenced this pull request Oct 19, 2022
@BrzVlad
Copy link
Member

BrzVlad commented Oct 19, 2022

@uweigand Should be fixed by #77170

@filipnavara
Copy link
Member Author

Thanks a lot!

simonrozsival added a commit to simonrozsival/runtime that referenced this pull request Oct 19, 2022
@uweigand
Copy link
Contributor

Yes, this has fixed the test suite on s390x. Thanks!

@ghost ghost locked as resolved and limited conversation to collaborators Nov 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-VM-meta-mono community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants