Menu

#3 Embedding - Remoting

open-rejected
nobody
None
5
2015-02-12
2012-03-12
xeph
No

I found stack corruption is caused while invoking
C/C++ Native/CLR calls somefunction1 in C# Module,
somefunction1 calls Python.Net invokation (PyObject_Call, etc) method,
While current Instruction Pointer (IP) is in python vm (which python modifies thread fs), then .NET GC kicks in,
GC looks up for thread fs, err, something is wrong. bam. stack corruption.

i've tried all python invocation method, all same.
tried to Hold GC running while invoke, it was impossible. :)

so I've end at.

public static IPyObject InvokeSafe(this IPyObject inst, params IPyObject[] args)
{
PyTuple t = new PyTuple(args);

        IntPtr r;

        if \(PythonEngine.IsRemoteAvailable\)
            r = PythonEngine.RemoteInvoke\(inst.Object, t.Object, IntPtr.Zero\);
        else
        \{
            st.Restart\(\); 
            r = Runtime.PyEval\_CallObjectWithKeywords\(inst.Object, t.Object, IntPtr.Zero\);
            st.Stop\(\);

            if \(st.ElapsedMilliseconds >= 1000\)
                Console.WriteLine\("Invoke: \{0\} took \{1\}", inst, st.ElapsedMilliseconds\);
        \}

        if \(r == IntPtr.Zero\)
        \{
            if \(PythonEngine.IsRemoteAvailable\)
                throw PythonEngine.SafeCreatePythonException\(\);
            else
                throw new PythonException\(\);
        \}

        return new PyObject\(r\);
    \}

RemoteInvokation...
Another native thread waits for remoting, python.net calls remoting method, native thread runs. python net waits for method ends.
Works very well.

Discussion

  • xeph

    xeph - 2012-03-12

    I'm using Win7 / .NET 4.0 / Python27

     
  • Barton Cline

    Barton Cline - 2012-03-18
    • summary: Stack corruption, Part 2 --> Embedding - Remoting
    • status: open --> open-rejected
     

Log in to post a comment.

MongoDB Logo MongoDB