Skip to content

Finalizer calls PythonEngine.Initialize #1093

Closed

Description

@lostmsu

Here's a couple of concurrent stack traces I caught during today's debugging session (cleaned up a bit for readability):

Thread pool thread (lambda in AddPendingCollect)

Debug.Assert(bool condition, string message, string detailMessage)	Unknown
DebugUtil.EnsureGIL() Line 145	C#
Runtime.XIncref(System.IntPtr op) Line 599	C#
Runtime.PyObject_Type(System.IntPtr op) Line 908	C#
Runtime.Initialize(bool initSigs) Line 243	C#
PythonEngine.Initialize(System.Collections.Generic.IEnumerable<string> args,
                        bool setSysArgv, bool initSigs) Line 174	C#
PythonEngine.Initialize(bool setSysArgv, bool initSigs) Line 151	C#
PythonEngine.Initialize() Line 146	C#
Py.GIL() Line 624	C#
Finalizer.AddPendingCollect.AnonymousMethod__37_0() Line 147	C#
System.Threading.ExecutionContext.RunInternal(
            System.Threading.ExecutionContext executionContext,
            System.Threading.ContextCallback callback,
            object state)	Unknown
System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref Task currentTaskSlot)	Unknown
System.Threading.ThreadPoolWorkQueue.Dispatch()	Unknown

Main Thread

System.Threading.Tasks.Task.Wait()	Unknown
[Waiting on Async Operation]	
Finalizer.Collect(bool forceDispose) Line 84	C#
    Finalizer.Shutdown() Line 130	C#
Runtime.Shutdown() Line 410	C#
PythonEngine.ExecuteShutdownHandlers() Line 385	C#
PythonEngine.Shutdown() Line 317	C#
TestCustomMarshal.Dispose() Line 18	C#

It looks like when an object is collected while Python engine is shutting down, Finalizer might force-restart Python runtime, causing it to be initialized on a different thread (in this case - a thread pool thread).

This is from my branch, but I did not make any changes to disposal/finalization I will try to reproduce it in master. Do not feel familiar enough with shutdown and finalizer machinery to investigate. @amos402 , would you take a look?

Activity

  1. amos402 commented on Mar 17, 2020

    @amos402
    Member

    Finalizer.AddPendingCollect.AnonymousMethod__37_0() Line 147 C# called Py.GIL() to initialize the PythonEngine. You can change it to PyGILState_Ensure temporarily.

    #852 (comment)

  2. lostmsu commented on Mar 17, 2020

    @lostmsu
    MemberAuthor

    @amos402 so is it a known problem? Is it tracked elsewhere? For some reason I can't reproduce it on master.

  3. amos402 commented on Mar 17, 2020

    @amos402
    Member

    Not actually known, I just realize it. Didn't track it yet. But I knew the performance issue may occured on that PR.
    If you reduce the Finalizer.Threshold and put some callbacks into atexit which create some garbage, it may able to be reproduced, even a crash.

  4. self-assigned this
    on Mar 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions