File tree Expand file tree Collapse file tree 1 file changed +7
-1
lines changed Expand file tree Collapse file tree 1 file changed +7
-1
lines changed Original file line number Diff line number Diff line change @@ -109,7 +109,7 @@ def current_thread_id(self):
109109
110110 """
111111
112- if self .greenlet :
112+ if self .greenlet and self . _cache . data :
113113 # Greenlet objects are maintained in a tree structure with
114114 # the 'parent' attribute pointing to that which a specific
115115 # instance is associated with. Only the root node has no
@@ -120,6 +120,12 @@ def current_thread_id(self):
120120 # all other cases where we can obtain a current greenlet,
121121 # then it should indicate we are running as a greenlet.
122122
123+ # In this case, the garbage collector could have already
124+ # collected the final greenlet object, so we need to check if
125+ # the weakref is still alive before we can access the parent.
126+ # Unfortunately, instead of returning None, this will segfault
127+ # if attempting to access a non-existent greenlet object.
128+ # https://github.com/python-greenlet/greenlet/blob/master/src/greenlet/TThreadStateCreator.hpp#L57
123129 current = self .greenlet .getcurrent ()
124130 if current is not None and current .parent :
125131 return id (current )
You can’t perform that action at this time.
0 commit comments