@@ -201,15 +201,17 @@ class _GThreadState {
201
201
switch_kwargs_w(0 ),
202
202
origin_greenlet_s(0 ),
203
203
switchstack_use_tracing(0 )
204
- {};
204
+ {
205
+ fprintf (stderr, " Initialized thread state %p\n " , this );
206
+ };
205
207
206
208
// Only one of these, auto created per thread
207
209
_GThreadState (const _GThreadState& other) = delete ;
208
210
_GThreadState& operator =(const _GThreadState& other) = delete ;
209
211
210
212
~_GThreadState ()
211
213
{
212
- // fprintf(stderr, "Destructing thread %p\n", this);
214
+ fprintf (stderr, " Destructing thread state %p\n " , this );
213
215
};
214
216
215
217
inline PyGreenlet* borrow_current () const
@@ -730,8 +732,8 @@ g_switchstack(void)
730
732
{
731
733
fprintf (stderr, " g_switchstack: enter\n " );
732
734
int err;
733
- _GThreadState& state = g_thread_state_global;
734
735
{ /* save state */
736
+ _GThreadState& state = g_thread_state_global;
735
737
PyGreenlet* current = state.borrow_current ();
736
738
PyThreadState* tstate = PyThreadState_GET ();
737
739
current->recursion_depth = tstate->recursion_depth ;
@@ -766,6 +768,7 @@ fprintf(stderr, "g_switchstack: mark 1\n");
766
768
err = slp_switch ();
767
769
fprintf (stderr, " g_switchstack: mark 2\n " );
768
770
if (err < 0 ) { /* error */
771
+ _GThreadState& state = g_thread_state_global;
769
772
PyGreenlet* current = state.borrow_current ();
770
773
current->top_frame = NULL ;
771
774
#if GREENLET_PY37
@@ -782,6 +785,7 @@ fprintf(stderr, "g_switchstack: mark 2\n");
782
785
else {
783
786
fprintf (stderr, " g_switchstack: mark 3\n " );
784
787
// XXX: The ownership rules can be simplified here.
788
+ _GThreadState& state = g_thread_state_global;
785
789
PyGreenlet* target = state.borrow_target ();
786
790
PyGreenlet* origin = state.borrow_current ();
787
791
PyThreadState* tstate = PyThreadState_GET ();
@@ -1814,11 +1818,15 @@ static int
1814
1818
green_setcontext (PyGreenlet* self, PyObject* nctx, void * c)
1815
1819
{
1816
1820
#if GREENLET_PY37
1817
- PyThreadState* tstate;
1818
- PyObject* octx = NULL ;
1821
+ /* XXX: Should not be necessary, we don't access the current greenlet
1822
+ other than to compare it to ourself and its fine if that's null.
1823
+ */
1824
+ /*
1819
1825
if (!STATE_OK) {
1820
1826
return -1;
1821
1827
}
1828
+ */
1829
+ fprintf (stderr, " green_setcontext: enter\n " );
1822
1830
if (nctx == NULL ) {
1823
1831
PyErr_SetString (PyExc_AttributeError, " can't delete attribute" );
1824
1832
return -1 ;
@@ -1833,11 +1841,16 @@ green_setcontext(PyGreenlet* self, PyObject* nctx, void* c)
1833
1841
" contextvars.Context or None" );
1834
1842
return -1 ;
1835
1843
}
1836
- tstate = PyThreadState_GET ();
1844
+ fprintf (stderr, " green_setcontext: mark 1\n " );
1845
+ PyThreadState* tstate = PyThreadState_GET ();
1846
+ PyObject* octx = NULL ;
1847
+
1837
1848
if (PyGreenlet_ACTIVE (self) && self->top_frame == NULL ) {
1838
1849
/* Currently running greenlet: context is stored in the thread state,
1839
1850
not the greenlet object. */
1851
+ fprintf (stderr, " green_setcontext: mark 2 %p\n " , &g_thread_state_global);
1840
1852
if (g_thread_state_global.is_current (self)) {
1853
+ fprintf (stderr, " green_setcontext: mark 3\n " );
1841
1854
octx = tstate->context ;
1842
1855
tstate->context = nctx;
1843
1856
tstate->context_ver ++;
@@ -1851,7 +1864,8 @@ green_setcontext(PyGreenlet* self, PyObject* nctx, void* c)
1851
1864
}
1852
1865
}
1853
1866
else {
1854
- /* Greenlet is not running: just set context. */
1867
+ /* Greenlet is not running: just set context. Note that the
1868
+ greenlet may be dead.*/
1855
1869
octx = self->context ;
1856
1870
self->context = nctx;
1857
1871
Py_XINCREF (nctx);
0 commit comments