Skip to content

Registering RawProxyEncoder Causes Stack Overflow #1427

Closed

Description

@richard-gemmell-42

Environment

  • Pythonnet version: installed from master branch on 30th March 2021
  • Python version: 3.9.2
  • Operating System: Windows 10
  • .NET Runtime: Microsoft.NETCore.App 5.0.4, Microsoft.WindowsDesktop.App 3.1.13

Summary

Registering a RawProxyEncoder with PyObjectConversions.RegisterEncoder() causes the script to fail with a stack overflow.

Thanks

First of all, I'd like to say thanks to everyone who's contributed to this project. It's incredibly useful for us to write one set of code to serve both .NET and Python developers. We've had very few problems with Pythonnet. Thanks very much.

Details

I ran into a problem with Pythonnet converting System.Collections.Generic.List into a native python list as described in issue 1153.

lostmsu recommended using a RawProxyEncoder to disable the automatic conversion. Unfortunately, if I register an encoder and then call a .NET method the system crashes with a stack overflow.

import sys
from pathlib import Path
from clr_loader import get_coreclr
from pythonnet import set_runtime
api_path = Path('..\\PythonStubs\\bin\\Debug\\netcoreapp3.1')
sys.path.append(str(api_path))
rt = get_coreclr(runtime_config=str(api_path / f'PythonStubs.runtimeconfig.json'))
set_runtime(rt)
import clr
import System
from Python.Runtime import PyObjectConversions
from Python.Runtime.Codecs import RawProxyEncoder


class ListAsRawEncoder(RawProxyEncoder):
    __namespace__ = 'Dummy'

    def CanEncode(self, clr_type):
        return clr_type.Name == 'IList`1' and clr_type.Namespace == 'System.Collections.Generic'


if __name__ == '__main__':
    list_encoder = ListAsRawEncoder()
    PyObjectConversions.RegisterEncoder(list_encoder)

    domain = System.AppDomain.CurrentDomain
    print("Found", domain.GetAssemblies().Length, "assemblies in domain")

The output is:

    Stack overflow

Commenting out the call to RegisterEncoder prevents the crash.

Activity

  1. lostmsu commented on Mar 30, 2021

    @lostmsu
    Member

    I investigated this issue. Stack overflow happens when trying access a C# object, that might need codec (but does not have one), and there's at least one encoder implemented and registered from Python (like ListAsProxyEncoder).

    What happens is that in order to call CanEncode Python.NET must pass the clr_type to the encoder. That starts search for encoder, that can handle System.Type, which hits ListAsProxyEncoder.CanEncode again with System.Type, that needs to be passed to Python, and so on.

  2. richard-gemmell-42 commented on Mar 31, 2021

    @richard-gemmell-42
    Author

    Thanks for fixing this. I can't believe you did it so quickly.
    cheers,
    Richard

  3. lostmsu commented on Mar 31, 2021

    @lostmsu
    Member

    Haha, that's for the lack of time to work on new features :-D @richard-gemmell-42

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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