Skip to content

Commit bde0098

Browse files
authored
Merge pull request #1316 from newrelic/greenlet-segfault
Greenlet segfault
2 parents ea2eb35 + a9bc33d commit bde0098

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

newrelic/core/trace_cache.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff 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)

0 commit comments

Comments
 (0)