question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IronPython stops to work with module (json) after a while

See original GitHub issue

Python 2.7.11 on .NET Core 3.1 hosted in Azure on service fabric. Initially everything works, after a while a certain library fails, namely json

{
    "ClassName": "System.MissingMemberException",
    "Message": "'module' object has no attribute 'dumps'",
    "Data": null,
    "InnerException": null,
    "HelpURL": null,
    "StackTraceString": "   at IronPython.Runtime.Operations.PythonOps.ObjectGetAttribute(CodeContext context, Object o, String name)\r\n   at IronPython.Runtime.PythonModule.__getattribute__(CodeContext context, String name)\r\n   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)\r\n   at Microsoft.Scripting.Interpreter.FuncCallInstruction`5.Run(InterpretedFrame frame)\r\n   at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)\r\n   at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)\r\n   at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)\r\n   at Ic.RestMocker.Core.Scripting.ScriptSession.Execute(String script, Dictionary`2 scope) in I:\\agent\\_work\\817\\s\\Ic.RestMocker.Core\\Scripting\\ScriptSession.cs:line 93\r\n   at Ic.RestMocker.Core.Controllers.ScriptsController.Post() in I:\\agent\\_work\\817\\s\\Ic.RestMocker.Core\\Controllers\\ScriptsController.cs:line 38",
    "RemoteStackTraceString": null,
    "RemoteStackIndex": 0,
    "ExceptionMethod": null,
    "HResult": -2146233070,
    "Source": "IronPython",
    "WatsonBuckets": null,
    "MMClassName": null,
    "MMMemberName": null,
    "MMSignature": null
}

If I create a script like

import sys
sys.path

Output is

[
    "D:\\SvcFab\\_App\\RestMocker.ApplicationType_App66\\RestMocker.ApiPkg.Code.1.0.0.RestMocker_master_20201123.2\\pylib"
]

Which is the correct path to pylib

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:16 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
AndersMalmgrencommented, Dec 1, 2020

This is output from your commands btw, will test again when it breaks

11/23/2020, 7:25:34 PM

TRACE
['JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '_default_decoder', '_default_encoder', 'decoder', 'dump', 'dumps', 'encoder', 'load', 'loads', 'scanner']
Severity level: Information
11/23/2020, 7:25:34 PM

TRACE
<module 'json' from 'D:\SvcFab\_App\RestMocker.ApplicationType_App66\RestMocker.ApiPkg.Code.1.0.0.RestMocker_master_20201123.4\pylib\json\__init__.py'>
Severity level: Information
0reactions
AndersMalmgrencommented, Dec 4, 2020

@slozier

I have been able to repro! The total failure of the module only happens about 1 each 5 or 10 time for above test. So I rewrote my test to this, and now it fails every time

            var session = provider.GetService<IScriptSession>();
            var script = @"import json
json.dumps({ 'Foobar': 1 }, ensure_ascii = False)";

            foreach (var foo in Enumerable.Range(0, 10))
            {

                var error = false;

                while (!error)
                {
                    try
                    {
                        Parallel.ForEach(Enumerable.Range(1, 1000),
                            new ParallelOptions {MaxDegreeOfParallelism = Environment.ProcessorCount},
                            i => session.Execute(null, script));
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        error = true;
                    }
                }

                var result = session.Execute(null, script);
                Assert.IsNotNull(result);
            }

            await Database.Instance.TakeDown();
            await Database.Instance.WaitForCleanup();
        }

My solution

            var hash = script.GetHashCode();
            if (!_importLookup.ContainsKey(hash))
                _importLookup[hash] = script.Contains("import", StringComparison.InvariantCultureIgnoreCase);
            var locked = _importLookup[hash];

            if (locked)
                _importLock.Wait();

            object result;

            try
            {

                result = compiled.Execute(_engine.CreateScope(scope));
            }
            finally
            {
                if (locked)
                    _importLock.Release();
            }
Read more comments on GitHub >

github_iconTop Results From Across the Web

IronPython: No module named json - Stack Overflow
I soon discovered that the interactive python window in Visual Studio did not throw this error. print sys.path also showed different values ...
Read more >
ironpython import module error | The Search Engine You Control
I am trying to run a Python script with Spacy NER model on C# using Dynamic Py. I used following C# source code:....
Read more >
18.2. json — JSON encoder and decoder - IronPython
Extensible JSON encoder for Python data structures. Supports the following objects and types by default: Python, JSON. dict, object. list, ...
Read more >
Python Multithreading and Multiprocessing Tutorial - Toptal
Threading is just one of the many ways concurrent programs can be built. In this article, we will take a look at threading...
Read more >
No JSON object could be decoded - StartStopServices.py
I'm trying to run a python scrtipt to stop services, but I'm getting the following error message : "No JSON object could be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found