Repository navigation
Question: thread safety and correct usage of mono_gc_reference_queue_* APIs when embedding Mono (rare crash) #123853
|
I’m embedding Mono and using the reference queue APIs to track when managed C# objects become unreachable so I can clean up associated native resources. Specifically, I’m using: I would like to know how to use these APIs when embedding Mono. thread_start malloc: *** error for object 0x600002ea4635: |
Replies: 3 comments
|
From what I see, In my main thread, the call sequence is: Meanwhile, the GC finalizer created by Mono itself runs in another thread with the following calls: In ref_list_remove_element, there is also a comment mentioning an issue when the head changes concurrently: |
|
I’d like to implement this mechanism myself using the weak ref API. Could you confirm if these three APIs are safe to call from my main thread?
|
|
Here is the concrete runtime breakdown regarding 1. Is
|
Here is the concrete runtime breakdown regarding
mono_gc_reference_queue_*concurrency and the weak reference alternatives:1. Is
mono_gc_reference_queue_addThread-Safe?No, calling
mono_gc_reference_queue_addconcurrently with Mono's background finalizer processing is not safe.In
src/mono/mono/metadata/gc.c:mono_gc_reference_queue_add_internalenqueues items usingref_list_push(&queue->queue, entry).finalizer_thread -> reference_queue_process_all -> reference_queue_process) iterates throughqueue->queueand unlinks entries viaref_list_remove_element(iter, entry)before invokingg_free(entry).ref_list_pushusesmono_atomic_cas_ptra…