-
Notifications
You must be signed in to change notification settings - Fork 61
Deallocation of active generators causes error messages in Stackless 3.x #190
Comments
Interesting. So, there is an attempt made to send something into a generator on a paused tasklet (the generator is in a 'running' state, hasn't even yielded). |
That's a really good question. I'll have to think about it. |
The root cause of the problem are different life cycles of tasklets and generators. A soft switched paused and restorable tasklet can be collected without any finalisation. Stackless considers such a tasklet as "data". A not-running generator (= a generator, that has yielded) is similar to a paused and restorable tasklet. But starting with Python 3, C-Python always finalises a generator. The finaliser closes the generator (= sends a GeneratorExit exception into the generator). That's remarkably similar to the tasklet finaliser, just the exception is different. We could modify the tasklet finaliser to always kill a living tasklets. I just mad an experimental version on top of pull request #194 and it works just fine. The generator receives a TaskletExit and errors out. (I like this modification, it makes the behaviour much simpler to understand and similar to the behaviour of generators.) The downside is a change in the behaviour of Stackless. And it does not solve the root cause: if I unbind the tasklet before releasing it (insert a Because it is a normal pattern to unbind a tasklet after pickling the tasklet, a fix should work for this situation too. This requires a patch of the generator finaliser function _PyGen_Finalize. |
Fix is in pull request #195 |
…utines (#195) Silently ignore attempts to close a running generator, coroutine or asynchronous generator. This avoids spurious error messages, if such an object is deallocated as part of a paused, restorable tasklet.
After studying the finalizers of async generators I decided to modify the behaviour of gen_close(). Now it silently skips sending GeneratorExit to a running generator. This is the documented behaviour of the method close() of these objects. I merged pull request #195. |
async generators, are they different from regular generators? I must confess that I have become a bit lost in what Python has been doing with async and all that.... |
The following small test program emits unexpected error messages:
If you call it with Stackless 3.6 and soft-switching enabled you get:
And without soft-switching:
With Stackless 2.7 the program does not output anything unexpected.
The observed behaviour present in Stackless 3.4 and up. Stackless 3.3 shows the hard-switching behaviour for soft switching too. This change in behaviour is obviously related to PEP 442.
Expected behaviour
If a generator is executing but it's tasklet is paused, then it is not ok to close the generator (or send anything into it) without activating its tasklet. Therefore the current behaviour is not OK. This should be fixed.
The text was updated successfully, but these errors were encountered: