Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lazy init of ThreadWaitInfo in Thread.Mono.cs to prevent crash on ext…
…ernal attached threads. (#51483) * Init ThreadWaitInfo to prevent crash on external attached threads. Commit 457f58c added a new managed object into Mono's managed thread object, _waitInfo. This new object was initialized in Thread managed constructor, problem with that is for all threads that gets their thread object created in native code, create_thread_object, that code did the same thing as constructor before commit, since managed constructor called down into native code calling the same native method as create_thread_object meaning a thread object created from managed or native would be initialized identical. After above commit this is no longer true, meaning that all managed thread objects created in native code (like attached external threads) won't get _waitInfo initialized and that leads to a crash when OnThreadExiting gets called from Finalizer thread. Fix is to make sure _waitInfo get initialize regardless of thread object gets created from managed or native code. This fix implements a lazy init as part of property access, I tested a native version of the fix, calling back into managed to init managed parts of thread when tread gets created in native, but turns out that such a fix gets a little complicated, since some of the threads are created early when it's not possible to run managed code, so that in turn needed additional changes into the init order, that in turn has bigger impact and could cause other unwanted side effects. Fixing it in managed using lazy/on demand init is simpler/cleaner approach but might come with a small performance hit for callers of WaitInfo property (will add volatile read and conditional branch), but looking at callers of the property, they appear in functions with rather high "cost", like Sleep, NewMutex, Wait, SignalAndWait, Interrupt and RelinquishOwnership.
- Loading branch information