Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stackless 2.7.* app crash #165

Closed
kdschlosser opened this issue Jul 25, 2018 · 29 comments
Closed

Stackless 2.7.* app crash #165

kdschlosser opened this issue Jul 25, 2018 · 29 comments

Comments

@kdschlosser
Copy link

Here is the setup.

Windows 7 x64 sp1
Stackless Python 2.7.* x32
Cython 0.28.3

I am compiling python-openzwave which uses Cython. I get this error when compiling on stackless 2.7.* and not on standard python 2.7.*.

I get a Python app crash as soon as it starts cythonizing.

Here is the data from the app crash window

Problem Event Name:	APPCRASH
Application Name:	python.exe
Application Version:	0.0.0.0
Application Timestamp:	59ccecdf
Fault Module Name:	python27.dll
Fault Module Version:	2.7.14150.1013
Fault Module Timestamp:	59ccecce
Exception Code:	c0000005
Exception Offset:	00069c2a
OS Version:	6.1.7601.2.1.0.256.1
Locale ID:	1033
Additional Information 1:	0a9e
Additional Information 2:	0a9e372d3b4ad19135b953a78882e789
Additional Information 3:	0a9e
Additional Information 4:	0a9e372d3b4ad19135b953a78882e789

The project I am working on has been around since 1996 and Stackless Python was used and is not something that can be removed easily I am also not able to upgrade the python version to 3+ any time soon. I have brought up this error to the author(s) of Cython. they were kind enough to add Stackless to their test build on travis. they found that 2.7 fails completely and 3.5 (i believe) builds successfully. they are not compiling the same code as me so that is now removed from the equation. they are building on linux. so the OS has now been removed. If I take the dll (pyd) that is built on standard 2.7 and bring it over to stackless 2.7 I get the exact same app crash on import. This leads me to believe the problem is not with Cython but instead it is with Stackless Python.

I searched the internet to see if anyone has had an issue with the 2 working together and I could not find any issues. I couldn't find anything on someone running the 2 together either. Pretty much a dead end.

The issue reported to Cython can be viewed here:

cython/cython#2476

Any help is appreciated.
Thanks again.
Kevin

@akruis
Copy link

akruis commented Jul 25, 2018

Can you please provide a minimal example. My Cython experience is very limited and I'm not sure, that I can create my own example. I need to run the example with a debug build of Stackless.

@kdschlosser
Copy link
Author

I do not know how you go about compiling python-openzwave on linux so I can't give any instruction in that area.

If you are on windows you will need to download the attached file. the file is a clone of python-openzwave that has been modified to properly build on Windows. you will need to have Visual Studio 15.0 installed with all the Visual C plugins installed. I have the Visual C++ compiler package for python 2.7 installed. I do not think this is used but i can't remember. I also have all of the Visual C++ redists installed from 2008 to 2017 I don't remember off hand which ones are used. You are also going to have to use the Visual Studio installer and install the Windows 10 SDK (latest one).

after you have done all of that then you can run the builder.

to launch the builder

python setup.py build --flavor=dev

This will only build it not install. It will clone the openzwave repo and make any changes that are necessary to perform a successful build. it will take a while for it to locate VS and to run vcvars32 grab the environment settings from the subprocess and apply them to the environment that the builder is running in . it also has to modify the solution for openzwave because it was it was made to compile with VS 2010 which is pretty difficult to locate. once the build process for openzwave starts it will take a while to complete. you should have more then one line of "."s, if it completes really fast then something is wrong. once openzwave has finished building it will state that it is cythonizing and that is when the crash occurs.

This is about as minimal as i can provide. I do not know a lot about cython either. It might be easier if you have a debugging build of stackless done for Windows and tell me what is needed have me run the tasks.

or if you want to RDP into my machine using TeamViewer and run whatever it is you need to. we can do that as well.

here is the attachment if you are able to use it
python_openzwave.zip

@kdschlosser
Copy link
Author

OK so i am not sure exactly what was causing the issue but i ran a build of openzwave using msbuild instead of visual studio. and when Cython ran no exception everything seems to be good. I do not know what caused the issue because I am using the same msbuild that VS is using. I am passing the solution right to msbuild.

Not sure. but the problem has gone away. I did have my PS in my PC die. and I installed a new one. but i do not think this would have caused a problem. who knows.

@akruis
Copy link

akruis commented Jul 28, 2018

There is indeed a problem. I just tried to run the Cython test suite with a Stackless 2.7.15 debug build and got a SystemError exception. I'm going to debug this issue, but it will take some time.

@kdschlosser
Copy link
Author

That's what it is. I got the same error also. but it stated the issue was in logging.py when I added a null handler.

It stated "SystemError no exception set". or something to that effect.

It could be a place to look. I do not know if that is actually where the problem is or not. the strange thing is I removed the logging code from the Cython file that got compiled but the same error showed up. exactly the same way. So it could be that is the issue I simply do not know where it is coming from.

@kdschlosser
Copy link
Author

I am told by the folks over at cython that everything works as advertised with the Stackless 3.* builds. not sure if that may help you locate the issue or not.

@akruis
Copy link

akruis commented Jul 31, 2018

@kdschlosser : probably your problems were caused by #166. I'll provide you a fixed python27.dll within a few days. Do you use the 32bit or the 64bit version?

@kdschlosser
Copy link
Author

I am using 32 bit. I also wanted to say thank you very much for locating and solving the problem so quickly. most view python 2 as not worth fixing problems with because it is going to become EOL not to long from now. Unfortunately our project is stuck with it for a while because of slow development with wxPython and python 3. This fix is going to allow us to add a very needed piece of functionality to our software. I know I probably wasn't a huge help in giving clues. I am still newish to the programming world and this sort of thing is over my head. But I am learning.

Thank You for the time you have spent working on this. Fixing this has removed the need for those changes I made that got deleted by the stackless installer. It is also going to provide a much cleaner means of adding functionality to our software.

@akruis
Copy link

akruis commented Aug 1, 2018

I'm in a similar situation. We have a product, that is based on the capability to serialize (pickle) tasklets and continue them later on a different host with a different OS. That's currently not possible with C-Python.

Btw. here is the archive with the new python27.dll: https://bitbucket.org/stackless-dev/stackless/downloads/StacklessPythonDLL_32bit_2.7.15_git-d377c06344.zip. Just replace the python27.dll in your installation with this one.

@kdschlosser
Copy link
Author

Thanks so much for getting this fixed as fast as you have I really do appreciate it. Do you or Stackless have any mechanism to be able to donate? You have no idea how much work this is going to save me. The way I was going to go about adding this addition to our software was I was going to create a separate installation for just this feature what would run as a service and open a socket that i would connect to from the software that runs on Stackless. to many extra bits involved that don't need to be there if i can access the code directly. If you ever get the opportunity and want to check out what we do it's at www.eventghost.net. we have to be one of the older pieces of software running Stackless. coding started on 8-10-2005. initial release i think was early 2007. maybe you can suggest something that may improve functionality/performance, who knows. I started working on the project 2 years ago and have not had the opportunity to look into how Stackless functions.

We had discussed removing it because of the website and the issues there made it seem like it was basically still being distributed but not really developed, This was brought up on several forums and the general consensus was that Stackless was no longer being developed.

I do have this question, I have searched the internet for an answer and only found half answers mostly "I heard this", or "I read this somewhere" not solid answers, not a single clear answer. I really have been wanting a clear answer to..
What are the benefits of Stackless Python over Traditional Python?

@kdschlosser
Copy link
Author

Well we are much closer then before.

I am still getting an app crash. But it compiles and the import works correctly. It starts the connection to the hardware and then part way through it. it crashes

Problem signature:
  Problem Event Name:	APPCRASH
  Application Name:	python.exe
  Application Version:	0.0.0.0
  Application Timestamp:	5b2fa646
  Fault Module Name:	python27.dll
  Fault Module Version:	2.7.15150.1013
  Fault Module Timestamp:	5b622cd3
  Exception Code:	c0000005
  Exception Offset:	00059589
  OS Version:	6.1.7601.2.1.0.256.1
  Locale ID:	1033
  Additional Information 1:	0a9e
  Additional Information 2:	0a9e372d3b4ad19135b953a78882e789
  Additional Information 3:	0a9e
  Additional Information 4:	0a9e372d3b4ad19135b953a78882e789


Unhandled exception at 0x1E059589 (python27.dll) in python.exe: 0xC0000005: Access violation reading location 0x00000018.

visual studio debugger is telling me that the issue is in python\ceval.c line 1427

#ifdef STACKLESS
        /* don't do periodic things when in atomic mode *
         * (thread or tasklet switching)
         */
        if (--_Py_Ticker < 0 && !tstate->st.current->flags.atomic) {    <~~~~~~~ HERE
#else
        if (--_Py_Ticker < 0) {
#endif
            if (*next_instr == SETUP_FINALLY) {
                /* Make the last opcode before
                   a try: finally: block uninterruptible. */
                goto fast_next_opcode;
            }

I can give you a copy of the code I am using, i don't think it is going to help unless you own a 5th Generation Aeon Labs ZStick

@akruis
Copy link

akruis commented Aug 2, 2018

About the development of Stackless: currently I'm maintaining Stackless, because my company still needs Stackless 2.7.x. Probably we will have to port our application to Stackless 3.x within the next two years. Therefore I'm very much interested in fixing bugs, but not in adding features. And I don't care about the Web-Site very much.

What are the benefits of Stackless Python over Traditional Python? Stackless tasklets are a kind of coroutine or micro-thread. This makes it possible to write web-servers for many thousands connected clients, because switching tasklets is much faster than switching os-level threads. Another unique feature of Stackless is the option to pickle a tasklet and restore it later. This can be used to checkpoint a program or to migrate it to a different computer.

About your crash. The python27.dll is PGO-optimized. Don't rely on the debugger, it may misguide you. If you have VS2008 you should really create a Debug Build of Stackless. Not only the debug build is not optimized. More important are the assertions, which are enabled in debug builds. And if you suspect a problem in Python/ceval.c you really should comment out the line #define PY_LOCAL_AGGRESSIVE at the top of Python/ceval.c. If you don't have VS2008 get it. You probably need a MSDN subscription to download it.

Once you have a debug build you need to write a minimal example that reproduces the crash. This can take a lot of time. But I can't help you without such an example. With Cython you had good luck. There was a discussion on the python-dev mailing list about a new C-API for fast function calls and Cythons fast function calls were mentioned. Therefore I was aware of potential problems with Cython. And indeed the Cython test suite failed.

@kdschlosser
Copy link
Author

OK I will see if i can do that for ya.

I also found a copy of Visual Studio 2008 Pro that does not require a 550.00 USD yearly subscription. Apparently Microsoft still has a 90 day evaluation copy they may have forgotten about on one of their servers.

so for anyone interested it is located here

http://download.microsoft.com/download/8/1/d/81d3f35e-fa03-485b-953b-ff952e402520/VS2008ProEdition90dayTrialENUX1435622.iso

and there is also a service pack that is available. and that is located here

http://download.microsoft.com/download/a/3/7/a371b6d1-fc5e-44f7-914c-cb452b4043a9/VS2008SP1ENUX1512962.iso

@kristjanvalur
Copy link
Collaborator

Good writeup, Anselm. Pickling of execution state really always was a super feature. Although strictly speaking it doesn't stem from the existence of "tasklets", but rather how stackless doesn"t rely on a recursive frame execution loop.
IMHO that is the biggest design mistake with cpython, one that persists today, that the C stack mirrors the Py stack. If you have a loop based frame executor, then "tasklets" (or similar low level entities) naturally become available.
Generator functions are a step in this direction, but the way Python has perverted these into becoming some sort of "coroutines" by using "yield from" and further, using this to achieve tasklet-like functionality, shows how badly thought out it is. Proper separate execution contexts, like stackless (and greenlets) provide are clearly a superior concept.

@kdschlosser
Copy link
Author

I am sorry for bother you again. All of this stuff is clearly over my head.

I have built the debugging version of stackless (2.7-slp branch)
I have recompiled openzwave for debugging.
I ran Cython against the compiled lib in debugging mode
all of the _d and pdb files are as they should be

Visual studio loads everything properly when debugging the crash

You are correct as far as the debugging information i gave you before not being correct. it now points to a different spot.

Please bear with me here as i think i am going about getting the proper information. I have not a clue how I would go about creating this problem outside of using the code that I have. I did not write this code I am using it to connect with a device on my system. I am still a ways off from being able to fully understand what is happening with all of this.

But this is what I am seeing.

This is the error that shows up when i run the project I am workin on,

2018-08-03 14:11:29.822 Always, OpenZwave Version 1.4.0 Starting Up
2018-08-03 14:11:29.931 Info, Setting Up Provided Network Key for Secure Communications
2018-08-03 14:11:29.931 Warning, Failed - Network Key Not Set
2018-08-03 14:11:29.931 Info, mgr,     Added driver for controller \\.\COM16
2018-08-03 14:11:29.931 Info,   Opening controller \\.\COM16
2018-08-03 14:11:29.931 Info,     Trying to open serial port \\.\COM16 (Attempt 1)
2018-08-03 14:11:29.931 Info,     Serial port \\.\COM16 opened (attempt 1)
2018-08-03 14:11:29.947 Detail, contrlr, Queuing (Command) FUNC_ID_ZW_GET_VERSION: 0x01, 0x03, 0x00, 0x15, 0xe9
2018-08-03 14:11:29.947 Detail, contrlr, Queuing (Command) FUNC_ID_ZW_MEMORY_GET_ID: 0x01, 0x03, 0x00, 0x20, 0xdc
2018-08-03 14:11:29.947 Detail, contrlr, Queuing (Command) FUNC_ID_ZW_GET_CONTROLLER_CAPABILITIES: 0x01, 0x03, 0x00, 0x05, 0xf9
2018-08-03 14:11:29.947 Detail, contrlr, Queuing (Command) FUNC_ID_SERIAL_API_GET_CAPABILITIES: 0x01, 0x03, 0x00, 0x07, 0xfb
2018-08-03 14:11:29.947 Detail, contrlr, Queuing (Command) FUNC_ID_ZW_GET_SUC_NODE_ID: 0x01, 0x03, 0x00, 0x56, 0xaa
2018-08-03 14:11:29.947 Detail, 
2018-08-03 14:11:29.947 Info, contrlr, Sending (Command) message (Callback ID=0x00, Expected Reply=0x15) - FUNC_ID_ZW_GET_VERSION: 0x01, 0x03, 0x00, 0x15, 0xe9
2018-08-03 14:11:29.947 Detail, contrlr,   Received: 0x01, 0x10, 0x01, 0x15, 0x5a, 0x2d, 0x57, 0x61, 0x76, 0x65, 0x20, 0x33, 0x2e, 0x39, 0x35, 0x00, 0x01, 0x99
2018-08-03 14:11:29.947 Detail, 
2018-08-03 14:11:29.947 Info, contrlr, Received reply to FUNC_ID_ZW_GET_VERSION:
2018-08-03 14:11:29.947 Info, contrlr,     Static Controller library, version Z-Wave 3.95
2018-08-03 14:11:29.947 Detail, Node045,   Expected reply was received
2018-08-03 14:11:29.947 Detail, Node045,   Message transaction complete
2018-08-03 14:11:29.947 Detail, 
2018-08-03 14:11:29.947 Detail, contrlr, Removing current message
2018-08-03 14:11:29.947 Detail, 
2018-08-03 14:11:29.947 Info, contrlr, Sending (Command) message (Callback ID=0x00, Expected Reply=0x20) - FUNC_ID_ZW_MEMORY_GET_ID: 0x01, 0x03, 0x00, 0x20, 0xdc
2018-08-03 14:11:29.947 Detail, contrlr,   Received: 0x01, 0x08, 0x01, 0x20, 0xcd, 0xd4, 0x98, 0x39, 0x01, 0x6f
2018-08-03 14:11:29.947 Detail, 
2018-08-03 14:11:29.947 Info, contrlr, Received reply to FUNC_ID_ZW_MEMORY_GET_ID. Home ID = 0xcdd49839.  Our node ID = 1
2018-08-03 14:11:29.947 Detail, Node212,   Expected reply was received
2018-08-03 14:11:29.947 Detail, Node212,   Message transaction complete
2018-08-03 14:11:29.947 Detail, 
2018-08-03 14:11:29.947 Detail, contrlr, Removing current message
2018-08-03 14:11:29.947 Detail, 
2018-08-03 14:11:29.947 Info, contrlr, Sending (Command) message (Callback ID=0x00, Expected Reply=0x05) - FUNC_ID_ZW_GET_CONTROLLER_CAPABILITIES: 0x01, 0x03, 0x00, 0x05, 0xf9
2018-08-03 14:11:29.963 Detail, contrlr,   Received: 0x01, 0x04, 0x01, 0x05, 0x08, 0xf7
2018-08-03 14:11:29.963 Detail, 
2018-08-03 14:11:29.963 Info, contrlr, Received reply to FUNC_ID_ZW_GET_CONTROLLER_CAPABILITIES:
2018-08-03 14:11:29.963 Info, contrlr,     There is no SUC ID Server (SIS) in this network.
2018-08-03 14:11:29.963 Info, contrlr,     The PC controller is a primary controller.
2018-08-03 14:11:29.963 Detail, Node247,   Expected reply was received
2018-08-03 14:11:29.963 Detail, Node247,   Message transaction complete
2018-08-03 14:11:29.963 Detail, 
2018-08-03 14:11:29.963 Detail, contrlr, Removing current message
2018-08-03 14:11:29.963 Detail, 
2018-08-03 14:11:29.963 Info, contrlr, Sending (Command) message (Callback ID=0x00, Expected Reply=0x07) - FUNC_ID_SERIAL_API_GET_CAPABILITIES: 0x01, 0x03, 0x00, 0x07, 0xfb
2018-08-03 14:11:29.963 Detail, contrlr,   Received: 0x01, 0x2b, 0x01, 0x07, 0x01, 0x00, 0x00, 0x86, 0x01, 0x01, 0x00, 0x5a, 0xfe, 0x81, 0xff, 0x88, 0x4f, 0x1f, 0x00, 0x00, 0xfb, 0x9f, 0x7d, 0xa0, 0x67, 0x00, 0x00, 0x80, 0x00, 0x80, 0x86, 0x00, 0x00, 0x00, 0xe8, 0x73, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x60, 0x00, 0x00, 0xfa
2018-08-03 14:11:29.963 Detail, 
2018-08-03 14:11:29.963 Info, contrlr,  Received reply to FUNC_ID_SERIAL_API_GET_CAPABILITIES
2018-08-03 14:11:29.978 Info, contrlr,     Serial API Version:   1.0
2018-08-03 14:11:29.978 Info, contrlr,     Manufacturer ID:      0x0086
2018-08-03 14:11:29.978 Info, contrlr,     Product Type:         0x0101
2018-08-03 14:11:29.978 Info, contrlr,     Product ID:           0x005a
2018-08-03 14:11:29.978 Detail, contrlr, Queuing (Command) FUNC_ID_ZW_GET_RANDOM: 0x01, 0x04, 0x00, 0x1c, 0x20, 0xc7
2018-08-03 14:11:29.978 Detail, contrlr, Queuing (Command) FUNC_ID_SERIAL_API_GET_INIT_DATA: 0x01, 0x03, 0x00, 0x02, 0xfe
2018-08-03 14:11:29.978 Detail, contrlr, Queuing (Command) FUNC_ID_SERIAL_API_SET_TIMEOUTS: 0x01, 0x05, 0x00, 0x06, 0x64, 0x0f, 0x97
2018-08-03 14:11:29.978 Detail, contrlr, Queuing (Command) FUNC_ID_SERIAL_API_APPL_NODE_INFORMATION: 0x01, 0x07, 0x00, 0x03, 0x01, 0x02, 0x01, 0x00, 0xf9
2018-08-03 14:11:29.978 Detail,   Expected reply was received
2018-08-03 14:11:29.978 Detail,   Message transaction complete
2018-08-03 14:11:29.978 Detail, 
2018-08-03 14:11:29.978 Detail, contrlr, Removing current message
2018-08-03 14:11:29.978 Detail, 
2018-08-03 14:11:29.978 Info, contrlr, Sending (Command) message (Callback ID=0x00, Expected Reply=0x56) - FUNC_ID_ZW_GET_SUC_NODE_ID: 0x01, 0x03, 0x00, 0x56, 0xaa
2018-08-03 14:11:29.978 Detail, contrlr,   Received: 0x01, 0x04, 0x01, 0x56, 0x00, 0xac
2018-08-03 14:11:29.978 Detail, 
2018-08-03 14:11:29.978 Info, contrlr, Received reply to GET_SUC_NODE_ID.  Node ID = 0
2018-08-03 14:11:29.978 Info, Controller Does not Support SUC - Cannot Setup Controller as SUC Node
2018-08-03 14:11:29.978 Detail, Node172,   Expected reply was received
2018-08-03 14:11:29.978 Detail, Node172,   Message transaction complete
2018-08-03 14:11:29.978 Detail, 
2018-08-03 14:11:29.978 Detail, contrlr, Removing current message
2018-08-03 14:11:29.978 Detail, 
2018-08-03 14:11:29.978 Info, contrlr, Sending (Command) message (Callback ID=0x00, Expected Reply=0x1c) - FUNC_ID_ZW_GET_RANDOM: 0x01, 0x04, 0x00, 0x1c, 0x20, 0xc7
2018-08-03 14:11:30.041 Detail, contrlr,   Received: 0x01, 0x25, 0x01, 0x1c, 0x01, 0x20, 0x2c, 0x35, 0xf6, 0x37, 0x81, 0x29, 0x5b, 0xe7, 0x08, 0xf6, 0xa2, 0xfe, 0xa8, 0x37, 0x59, 0x25, 0x6d, 0xd1, 0xe1, 0x0e, 0x82, 0x1a, 0xc7, 0xa0, 0xa8, 0x97, 0xb6, 0xcb, 0xed, 0xe8, 0x23, 0x72, 0xd1
2018-08-03 14:11:30.041 Detail, 
2018-08-03 14:11:30.041 Info, contrlr, Received reply to FUNC_ID_ZW_GET_RANDOM: true
2018-08-03 14:11:30.041 Detail, Node032,   Expected reply was received
2018-08-03 14:11:30.041 Detail, Node032,   Message transaction complete
2018-08-03 14:11:30.041 Detail, 
2018-08-03 14:11:30.041 Detail, contrlr, Removing current message
2018-08-03 14:11:30.041 Detail, 
2018-08-03 14:11:30.041 Info, contrlr, Sending (Command) message (Callback ID=0x00, Expected Reply=0x02) - FUNC_ID_SERIAL_API_GET_INIT_DATA: 0x01, 0x03, 0x00, 0x02, 0xfe
2018-08-03 14:11:30.134 Detail, contrlr,   Received: 0x01, 0x25, 0x01, 0x02, 0x05, 0x00, 0x1d, 0x0b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0x00, 0xcf
2018-08-03 14:11:30.134 Detail, 
2018-08-03 14:11:30.134 Info, mgr,     Driver with Home ID of 0xcdd49839 is now ready.
2018-08-03 14:11:30.134 Info, 
2018-08-03 14:11:30.134 Info, contrlr, Received reply to FUNC_ID_SERIAL_API_GET_INIT_DATA:
2018-08-03 14:11:30.134 Info, contrlr,     Node 001 - New
2018-08-03 14:11:30.134 Detail, Node001, AdvanceQueries queryPending=0 queryRetries=0 queryStage=None live=1
2018-08-03 14:11:30.134 Detail, Node001, QueryStage_ProtocolInfo
2018-08-03 14:11:30.134 Detail, Node001, Queuing (Query) Get Node Protocol Info (Node=1): 0x01, 0x04, 0x00, 0x41, 0x01, 0xbb
2018-08-03 14:11:30.134 Detail, Node001, Queuing (Query) Query Stage Complete (ProtocolInfo)
2018-08-03 14:11:30.134 Info, Node001, Initializing Node. New Node: false (false)
2018-08-03 14:11:30.134 Info, contrlr,     Node 002 - New
2018-08-03 14:11:30.134 Detail, Node002, AdvanceQueries queryPending=0 queryRetries=0 queryStage=None live=1
2018-08-03 14:11:30.15Assertion failed: tstate->st.main && tstate->st.current, file ..\..\Python\ceval.c, line 704

Now i know that none of this is going to make any sense to you. But basically it is logging output for the communications between the software and the device as well as communications between the device and wireless devices that it is communicating with. The big thing is all of this code is located inside of the pyd file that was compiled with the dll you posted earlier being in place. This is a big step because i was never even able to import the pyd file let alone even get it to run. So this is a good thing. it compiles, it loads. and it runs mostly.

when i bring up the call stack in Visual Studio and show the source for where the breakpoint occured this is what gets shown. This is from ceval.c

PyEval_EvalFrameEx(PyFrameObject *f, int throwflag)
{
#ifdef STACKLESS
    /*
     * This method is not used by Stackless Python. It is provided for compatibility
     * with extension modules and Cython.
     */
    PyThreadState *tstate = PyThreadState_GET();
    PyObject * retval = NULL;

    if (f == NULL)
        return NULL;
    /* make sure that the Stackless system has been initialized. */
    assert(tstate->st.main && tstate->st.current);                                <~~~~~~~~~~~breakpoint line
    /* sanity check. */
    assert(SLP_CURRENT_FRAME_IS_VALID(tstate));
    if (!throwflag) {
        /* If throwflag is true, retval must be NULL. Otherwise it must be non-NULL.
         */
        Py_INCREF(Py_None);
        retval = Py_None;
    }
    if (PyFrame_Check(f) && f->f_execute == NULL) {
        /* A new frame returned from PyFrame_New() has f->f_execute == NULL.
         */
        f->f_execute = PyEval_EvalFrameEx_slp;
   } else {
        /* The layout of PyFrameObject differs between Stackless and C-Python.
         * Stackless f->f_execute is C-Python f->f_code. Stackless f->f_code is at
         * the end, just before f_localsplus.
         *
         * In order to detect a C-Python frame, we must compare f->f_execute
         * with every valid frame function. Hard to implement completely.
         * Therefore I'll check only for relevant functions.
         * Amend the list as needed.
         *
         * If needed, we could try to fix an C-Python frame on the fly.
         *
         * (It is not possible to detect an C-Python frame by its size, because
         * we need the code object to compute the expected size and the location
         * of code objects varies between Stackless and C-Python frames).
         */
        if (!PyCFrame_Check(f) &&
                f->f_execute != PyEval_EvalFrameEx_slp &&
                f->f_execute != slp_eval_frame_value &&
                f->f_execute != slp_eval_frame_noval &&
                f->f_execute != slp_eval_frame_iter &&
                f->f_execute != slp_eval_frame_setup_with &&
                f->f_execute != slp_eval_frame_with_cleanup)  {
            PyErr_BadInternalCall();
            return NULL;
        }
    }
    retval = slp_frame_dispatch(f, f->f_back, throwflag, retval);
    assert(!STACKLESS_UNWINDING(retval));
    assert(SLP_CURRENT_FRAME_IS_VALID(tstate));
    return retval;
}

This is how i think all of this works. so don't quote me on this and please correct me if i am wrong.

Cython generates cpp code by using a user created .pyx file.
The best explanation of a pyx file i can come up with is it is a blueprint of a cpp lib file that creates python representations of a cpp objects.

this is the next item down the call stack. This is from the .cpp file that is created by Cython

#include "frameobject.h"
static PyObject* __Pyx_PyFunction_FastCallNoKw(PyCodeObject *co, PyObject **args, Py_ssize_t na,
                                           PyObject *globals) {
    PyFrameObject *f;
    PyThreadState *tstate = __Pyx_PyThreadState_Current;
    PyObject **fastlocals;
    Py_ssize_t i;
    PyObject *result;
    assert(globals != NULL);
    /* XXX Perhaps we should create a specialized
       PyFrame_New() that doesn't take locals, but does
       take builtins without sanity checking them.
    */
    assert(tstate != NULL);
    f = PyFrame_New(tstate, co, globals, NULL);
    if (f == NULL) {
        return NULL;
    }
    fastlocals = f->f_localsplus;
    for (i = 0; i < na; i++) {
        Py_INCREF(*args);
        fastlocals[i] = *args++;
    }
    result = PyEval_EvalFrameEx(f,0);     <~~~~~~~ this is the calling line that causes the assertion error
    ++tstate->recursion_depth;
    Py_DECREF(f);
    --tstate->recursion_depth;
    return result;
}

I am not sure if there is a way to take another step into the code chain and manage to get the location in the pyx file that is making the call to __Pyx_PyFunction_FastCallNoKw to see if maybe the issue can be handled there or to see how exactly to write a Cython pyx file to give you a minimial code block to replicate this issue.

If this information is of no help I can ask over at the Cython repository and see if someone there could write something up that can cause this issue.

But i figured I would start here because of the comments right before the code that is generating the assertion error. which is that it is checking to make sure stackless is initialized. as it would appear that it is not.

Now that comment got me thinking
do i have to import stackless for it to initialize?
or does a tasklet need to be created in order for stackless to be initialized?

@kdschlosser
Copy link
Author

Update:

I tried creating a tasklet and the same error still comes up.

@akruis
Copy link

akruis commented Aug 4, 2018

@kdschlosser Hi Kevin, we have a good starting point. Is your code multi threaded?
Please change the line assert(tstate->st.main && tstate->st.current); into three separate lines:

assert(tstate->st.main || tstate->st.current);
assert(tstate->st.main);
assert(tstate->st.current);

and report, which line fails. This way we get more information.

Usually this assertion fails, if a new thread has been created outside of Python and the Stackless thread state has not been initialized. Most entry points into Stackless Python have special code for this situation, but I didn't add this code to PyEval_EvalFrameEx(). Instead I added just the assertion.

@kristjanvalur Kristján, do you have any suggestion how to initialize the tstate in this situation? It is a bit different from the usual "Stackless external interface" code in stacklessmodule.c.

@kdschlosser
Copy link
Author

OK I will make those changes and report back to ya. I don't know how long it is going to take for stackless to compile it took 10 minutes to make the debug version so it should be that or less.

@kdschlosser
Copy link
Author

kdschlosser commented Aug 4, 2018

ok this was quick.
it fails at

assert(tstate->st.main || tstate->st.current);

and to answer you question on the multi threading. I am assuming you want to know about the pyd file. and the answer is yes it is. Tho I do not know when the threads are created. it could be at module import. maybe importing the pyd file inside of a tasklet? I can give that a shot.

@kdschlosser
Copy link
Author

nope that didn't work.

@akruis
Copy link

akruis commented Aug 5, 2018

Ok, that's fine. Here is a small script, that reproduces the assertion failure.

import stackless
import thread

def f(lock):
    try:
        print("this is f()")
    finally:
        lock.release()

lock = thread.allocate_lock()
lock.acquire()
id = thread.start_new_thread(stackless.test_PyEval_EvalFrameEx, (f.__code__, f.__globals__, (lock,)))
lock.acquire()
print("ok")

Now it's easy to fix.

@akruis
Copy link

akruis commented Aug 5, 2018

@kdschlosser Hi Kevin, please try https://bitbucket.org/stackless-dev/stackless/downloads/StacklessPythonDLL_32bit_2.7.15_git-187184ba.zip. It is a PGO optimized build of commit 187184b. I'm pretty confident, that this build fixes the assertion failure. If not, please make an debug build of commit 187184b and report the output.

@kdschlosser
Copy link
Author

ok will do

@kdschlosser
Copy link
Author

Alright! That seems to have fixed the issue. HOORAY!

Thank you very much in the lesson on how to use Visual Studio to debug.

@akruis
Copy link

akruis commented Aug 6, 2018

Thank you again for reporting the issue and for your prompt response to my questions.

@akruis akruis closed this as completed Aug 6, 2018
@kdschlosser
Copy link
Author

hey no worries m8. I am glad you got it all sorted. it helped me a HUGE amount. I am already almost done with coding a plugin for our program. That is how much easier it has become. it would have taken weeks going between writing the code and bug testing before I would have even been able to release it. I should be done with it by tomorrow.

@kdschlosser
Copy link
Author

I wanted to let you know that I tried compiling a Cython project running Stackless 3.5 and I get the same SystemError traceback as I got when using Stackless 2.7

SystemError: error return without exception set

@akruis
Copy link

akruis commented Jan 7, 2019

@kdschlosser Hello Kevin, please open an new issue and provide detailed information.

I wanted to let you know that I tried compiling a Cython project running Stackless 3.5 and I get the same SystemError traceback as I got when using Stackless 2.7

SystemError: error return without exception set

@kdschlosser
Copy link
Author

ok will do.

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

No branches or pull requests

3 participants