c# - Docling running inside .NET 8 web application crash - Stack Overflow

I created a NEt web application which does call a python function from C# via pythonnet (v 3.0.5)I can

I created a NEt web application which does call a python function from C# via pythonnet (v 3.0.5) I can't figureout why it crashes. Any help will be appreciated.

private async Task<string> RunDocling()
{
    try
    {
        // Load the PDF file as byte array
        var pdfBytes = await ReadFileAsync($"{basePath}\\{fileName}.pdf");
        PythonEngine.Initialize();

        using (Py.GIL()) // Acquire Global Interpreter Lock
        {
            string projectDir = AppDomain.CurrentDomain.BaseDirectory;
            string scriptDir = Path.Combine(projectDir, "PythonScripts");
            dynamic sys = Py.Import("sys");
            sys.path.append(scriptDir);

            // Import the Python script
            dynamic pyModule = Py.Import("docling_converter");

            // Call the function and get the result
            string markdownContent = string.Empty;
            markdownContent = pyModule.convert_pdf_to_markdown(pdfBytes).ToString();

            // Save the output markdown file
            await File.WriteAllTextAsync($"{basePath}\\{fileName}.md", markdownContent);
        } //CRASH HERE!!!
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
    }

    return string.Empty;
}

However, the execution is successful coz I get the result I expect from the Python code. It just not coming back to the .NET code. There is a crash on the end brace of the using statement. (See the code for the crash point) No exceptions were thrown.

I get the following error in output windows.

The program '[28220] AiSummaryTest.Web.exe' has exited with code 3221226505 (0xc0000409).

I created a NEt web application which does call a python function from C# via pythonnet (v 3.0.5) I can't figureout why it crashes. Any help will be appreciated.

private async Task<string> RunDocling()
{
    try
    {
        // Load the PDF file as byte array
        var pdfBytes = await ReadFileAsync($"{basePath}\\{fileName}.pdf");
        PythonEngine.Initialize();

        using (Py.GIL()) // Acquire Global Interpreter Lock
        {
            string projectDir = AppDomain.CurrentDomain.BaseDirectory;
            string scriptDir = Path.Combine(projectDir, "PythonScripts");
            dynamic sys = Py.Import("sys");
            sys.path.append(scriptDir);

            // Import the Python script
            dynamic pyModule = Py.Import("docling_converter");

            // Call the function and get the result
            string markdownContent = string.Empty;
            markdownContent = pyModule.convert_pdf_to_markdown(pdfBytes).ToString();

            // Save the output markdown file
            await File.WriteAllTextAsync($"{basePath}\\{fileName}.md", markdownContent);
        } //CRASH HERE!!!
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Error: {ex.Message}");
    }

    return string.Empty;
}

However, the execution is successful coz I get the result I expect from the Python code. It just not coming back to the .NET code. There is a crash on the end brace of the using statement. (See the code for the crash point) No exceptions were thrown.

I get the following error in output windows.

The program '[28220] AiSummaryTest.Web.exe' has exited with code 3221226505 (0xc0000409).

Share Improve this question asked Mar 26 at 8:13 SurenSalukaSurenSaluka 1,5913 gold badges19 silver badges38 bronze badges 6
  • 1 Why throw away the valuable exception information and just log the message? Why not log the whole exception? – Uwe Keim Commented Mar 26 at 8:33
  • 2 The output window isn't a log and your own code is only writing part of the error. Use proper logging and log the entire exception instead of using Console.WriteLine. Don't try to hide exceptions either. As you found out, that won't prevent crashes. Access violation errors for example will crash the application, because the OS can't trust the app's code or data any more. You can't hide that. – Panagiotis Kanavos Commented Mar 26 at 9:03
  • 3 BTW 0xc0000409 is a stack buffer overrun error thrown by the OS itself. It means the combination of Python.NEt, docling and your own custom script is NOT working. We can't guess what docling_converter and convert_pdf_to_markdown are doing or even what convert_pdf_to_markdown returns. Why is a call to ToString() made? Does convert_pdf_to_markdown try to return a char array on the stack that's too large? – Panagiotis Kanavos Commented Mar 26 at 9:19
  • 2 Perhaps you should look at Loading Python file in .NET and Call Function in the docs, and try to call the Python method explicitly instead of using dynamic. – Panagiotis Kanavos Commented Mar 26 at 9:22
  • 2 Also check this GH issue. If the PYTHONHOME or PYTHONPATH paths are wrong, Python initialization will fail with a 0xc0000409 error. – Panagiotis Kanavos Commented Mar 26 at 9:24
 |  Show 1 more comment

1 Answer 1

Reset to default 1

You can not take GIL in one thread and release in another (which is what happens when you await before leaving using block).

Essentially the code inside Py.GIL can not have awaits unless you are in an unlikely situation with a single threaded scheduler.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744158862a4560995.html

相关推荐

  • c# - Docling running inside .NET 8 web application crash - Stack Overflow

    I created a NEt web application which does call a python function from C# via pythonnet (v 3.0.5)I can

    9天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信