TPythonState.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #ifndef GREENLET_PYTHON_STATE_CPP
  2. #define GREENLET_PYTHON_STATE_CPP
  3. #include <Python.h>
  4. #include "TGreenlet.hpp"
  5. namespace greenlet {
  6. PythonState::PythonState()
  7. : _top_frame()
  8. #if GREENLET_USE_CFRAME
  9. ,cframe(nullptr)
  10. ,use_tracing(0)
  11. #endif
  12. #if GREENLET_PY312
  13. ,py_recursion_depth(0)
  14. ,c_recursion_depth(0)
  15. #else
  16. ,recursion_depth(0)
  17. #endif
  18. #if GREENLET_PY313
  19. ,delete_later(nullptr)
  20. #else
  21. ,trash_delete_nesting(0)
  22. #endif
  23. #if GREENLET_PY311
  24. ,current_frame(nullptr)
  25. ,datastack_chunk(nullptr)
  26. ,datastack_top(nullptr)
  27. ,datastack_limit(nullptr)
  28. #endif
  29. {
  30. #if GREENLET_USE_CFRAME
  31. /*
  32. The PyThreadState->cframe pointer usually points to memory on
  33. the stack, alloceted in a call into PyEval_EvalFrameDefault.
  34. Initially, before any evaluation begins, it points to the
  35. initial PyThreadState object's ``root_cframe`` object, which is
  36. statically allocated for the lifetime of the thread.
  37. A greenlet can last for longer than a call to
  38. PyEval_EvalFrameDefault, so we can't set its ``cframe`` pointer
  39. to be the current ``PyThreadState->cframe``; nor could we use
  40. one from the greenlet parent for the same reason. Yet a further
  41. no: we can't allocate one scoped to the greenlet and then
  42. destroy it when the greenlet is deallocated, because inside the
  43. interpreter the _PyCFrame objects form a linked list, and that too
  44. can result in accessing memory beyond its dynamic lifetime (if
  45. the greenlet doesn't actually finish before it dies, its entry
  46. could still be in the list).
  47. Using the ``root_cframe`` is problematic, though, because its
  48. members are never modified by the interpreter and are set to 0,
  49. meaning that its ``use_tracing`` flag is never updated. We don't
  50. want to modify that value in the ``root_cframe`` ourself: it
  51. *shouldn't* matter much because we should probably never get
  52. back to the point where that's the only cframe on the stack;
  53. even if it did matter, the major consequence of an incorrect
  54. value for ``use_tracing`` is that if its true the interpreter
  55. does some extra work --- however, it's just good code hygiene.
  56. Our solution: before a greenlet runs, after its initial
  57. creation, it uses the ``root_cframe`` just to have something to
  58. put there. However, once the greenlet is actually switched to
  59. for the first time, ``g_initialstub`` (which doesn't actually
  60. "return" while the greenlet is running) stores a new _PyCFrame on
  61. its local stack, and copies the appropriate values from the
  62. currently running _PyCFrame; this is then made the _PyCFrame for the
  63. newly-minted greenlet. ``g_initialstub`` then proceeds to call
  64. ``glet.run()``, which results in ``PyEval_...`` adding the
  65. _PyCFrame to the list. Switches continue as normal. Finally, when
  66. the greenlet finishes, the call to ``glet.run()`` returns and
  67. the _PyCFrame is taken out of the linked list and the stack value
  68. is now unused and free to expire.
  69. XXX: I think we can do better. If we're deallocing in the same
  70. thread, can't we traverse the list and unlink our frame?
  71. Can we just keep a reference to the thread state in case we
  72. dealloc in another thread? (Is that even possible if we're still
  73. running and haven't returned from g_initialstub?)
  74. */
  75. this->cframe = &PyThreadState_GET()->root_cframe;
  76. #endif
  77. }
  78. inline void PythonState::may_switch_away() noexcept
  79. {
  80. #if GREENLET_PY311
  81. // PyThreadState_GetFrame is probably going to have to allocate a
  82. // new frame object. That may trigger garbage collection. Because
  83. // we call this during the early phases of a switch (it doesn't
  84. // matter to which greenlet, as this has a global effect), if a GC
  85. // triggers a switch away, two things can happen, both bad:
  86. // - We might not get switched back to, halting forward progress.
  87. // this is pathological, but possible.
  88. // - We might get switched back to with a different set of
  89. // arguments or a throw instead of a switch. That would corrupt
  90. // our state (specifically, PyErr_Occurred() and this->args()
  91. // would no longer agree).
  92. //
  93. // Thus, when we call this API, we need to have GC disabled.
  94. // This method serves as a bottleneck we call when maybe beginning
  95. // a switch. In this way, it is always safe -- no risk of GC -- to
  96. // use ``_GetFrame()`` whenever we need to, just as it was in
  97. // <=3.10 (because subsequent calls will be cached and not
  98. // allocate memory).
  99. GCDisabledGuard no_gc;
  100. Py_XDECREF(PyThreadState_GetFrame(PyThreadState_GET()));
  101. #endif
  102. }
  103. void PythonState::operator<<(const PyThreadState *const tstate) noexcept
  104. {
  105. this->_context.steal(tstate->context);
  106. #if GREENLET_USE_CFRAME
  107. /*
  108. IMPORTANT: ``cframe`` is a pointer into the STACK. Thus, because
  109. the call to ``slp_switch()`` changes the contents of the stack,
  110. you cannot read from ``ts_current->cframe`` after that call and
  111. necessarily get the same values you get from reading it here.
  112. Anything you need to restore from now to then must be saved in a
  113. global/threadlocal variable (because we can't use stack
  114. variables here either). For things that need to persist across
  115. the switch, use `will_switch_from`.
  116. */
  117. this->cframe = tstate->cframe;
  118. #if !GREENLET_PY312
  119. this->use_tracing = tstate->cframe->use_tracing;
  120. #endif
  121. #endif // GREENLET_USE_CFRAME
  122. #if GREENLET_PY311
  123. #if GREENLET_PY312
  124. this->py_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
  125. this->c_recursion_depth = Py_C_RECURSION_LIMIT - tstate->c_recursion_remaining;
  126. #else // not 312
  127. this->recursion_depth = tstate->recursion_limit - tstate->recursion_remaining;
  128. #endif // GREENLET_PY312
  129. #if GREENLET_PY313
  130. this->current_frame = tstate->current_frame;
  131. #elif GREENLET_USE_CFRAME
  132. this->current_frame = tstate->cframe->current_frame;
  133. #endif
  134. this->datastack_chunk = tstate->datastack_chunk;
  135. this->datastack_top = tstate->datastack_top;
  136. this->datastack_limit = tstate->datastack_limit;
  137. PyFrameObject *frame = PyThreadState_GetFrame((PyThreadState *)tstate);
  138. Py_XDECREF(frame); // PyThreadState_GetFrame gives us a new
  139. // reference.
  140. this->_top_frame.steal(frame);
  141. #if GREENLET_PY313
  142. this->delete_later = Py_XNewRef(tstate->delete_later);
  143. #elif GREENLET_PY312
  144. this->trash_delete_nesting = tstate->trash.delete_nesting;
  145. #else // not 312
  146. this->trash_delete_nesting = tstate->trash_delete_nesting;
  147. #endif // GREENLET_PY312
  148. #else // Not 311
  149. this->recursion_depth = tstate->recursion_depth;
  150. this->_top_frame.steal(tstate->frame);
  151. this->trash_delete_nesting = tstate->trash_delete_nesting;
  152. #endif // GREENLET_PY311
  153. }
  154. #if GREENLET_PY312
  155. void GREENLET_NOINLINE(PythonState::unexpose_frames)()
  156. {
  157. if (!this->top_frame()) {
  158. return;
  159. }
  160. // See GreenletState::expose_frames() and the comment on frames_were_exposed
  161. // for more information about this logic.
  162. _PyInterpreterFrame *iframe = this->_top_frame->f_frame;
  163. while (iframe != nullptr) {
  164. _PyInterpreterFrame *prev_exposed = iframe->previous;
  165. assert(iframe->frame_obj);
  166. memcpy(&iframe->previous, &iframe->frame_obj->_f_frame_data[0],
  167. sizeof(void *));
  168. iframe = prev_exposed;
  169. }
  170. }
  171. #else
  172. void PythonState::unexpose_frames()
  173. {}
  174. #endif
  175. void PythonState::operator>>(PyThreadState *const tstate) noexcept
  176. {
  177. tstate->context = this->_context.relinquish_ownership();
  178. /* Incrementing this value invalidates the contextvars cache,
  179. which would otherwise remain valid across switches */
  180. tstate->context_ver++;
  181. #if GREENLET_USE_CFRAME
  182. tstate->cframe = this->cframe;
  183. /*
  184. If we were tracing, we need to keep tracing.
  185. There should never be the possibility of hitting the
  186. root_cframe here. See note above about why we can't
  187. just copy this from ``origin->cframe->use_tracing``.
  188. */
  189. #if !GREENLET_PY312
  190. tstate->cframe->use_tracing = this->use_tracing;
  191. #endif
  192. #endif // GREENLET_USE_CFRAME
  193. #if GREENLET_PY311
  194. #if GREENLET_PY312
  195. tstate->py_recursion_remaining = tstate->py_recursion_limit - this->py_recursion_depth;
  196. tstate->c_recursion_remaining = Py_C_RECURSION_LIMIT - this->c_recursion_depth;
  197. this->unexpose_frames();
  198. #else // \/ 3.11
  199. tstate->recursion_remaining = tstate->recursion_limit - this->recursion_depth;
  200. #endif // GREENLET_PY312
  201. #if GREENLET_PY313
  202. tstate->current_frame = this->current_frame;
  203. #elif GREENLET_USE_CFRAME
  204. tstate->cframe->current_frame = this->current_frame;
  205. #endif
  206. tstate->datastack_chunk = this->datastack_chunk;
  207. tstate->datastack_top = this->datastack_top;
  208. tstate->datastack_limit = this->datastack_limit;
  209. this->_top_frame.relinquish_ownership();
  210. #if GREENLET_PY313
  211. Py_XDECREF(tstate->delete_later);
  212. tstate->delete_later = this->delete_later;
  213. Py_CLEAR(this->delete_later);
  214. #elif GREENLET_PY312
  215. tstate->trash.delete_nesting = this->trash_delete_nesting;
  216. #else // not 3.12
  217. tstate->trash_delete_nesting = this->trash_delete_nesting;
  218. #endif // GREENLET_PY312
  219. #else // not 3.11
  220. tstate->frame = this->_top_frame.relinquish_ownership();
  221. tstate->recursion_depth = this->recursion_depth;
  222. tstate->trash_delete_nesting = this->trash_delete_nesting;
  223. #endif // GREENLET_PY311
  224. }
  225. inline void PythonState::will_switch_from(PyThreadState *const origin_tstate) noexcept
  226. {
  227. #if GREENLET_USE_CFRAME && !GREENLET_PY312
  228. // The weird thing is, we don't actually save this for an
  229. // effect on the current greenlet, it's saved for an
  230. // effect on the target greenlet. That is, we want
  231. // continuity of this setting across the greenlet switch.
  232. this->use_tracing = origin_tstate->cframe->use_tracing;
  233. #endif
  234. }
  235. void PythonState::set_initial_state(const PyThreadState* const tstate) noexcept
  236. {
  237. this->_top_frame = nullptr;
  238. #if GREENLET_PY312
  239. this->py_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
  240. // XXX: TODO: Comment from a reviewer:
  241. // Should this be ``Py_C_RECURSION_LIMIT - tstate->c_recursion_remaining``?
  242. // But to me it looks more like that might not be the right
  243. // initialization either?
  244. this->c_recursion_depth = tstate->py_recursion_limit - tstate->py_recursion_remaining;
  245. #elif GREENLET_PY311
  246. this->recursion_depth = tstate->recursion_limit - tstate->recursion_remaining;
  247. #else
  248. this->recursion_depth = tstate->recursion_depth;
  249. #endif
  250. }
  251. // TODO: Better state management about when we own the top frame.
  252. int PythonState::tp_traverse(visitproc visit, void* arg, bool own_top_frame) noexcept
  253. {
  254. Py_VISIT(this->_context.borrow());
  255. if (own_top_frame) {
  256. Py_VISIT(this->_top_frame.borrow());
  257. }
  258. return 0;
  259. }
  260. void PythonState::tp_clear(bool own_top_frame) noexcept
  261. {
  262. PythonStateContext::tp_clear();
  263. // If we get here owning a frame,
  264. // we got dealloc'd without being finished. We may or may not be
  265. // in the same thread.
  266. if (own_top_frame) {
  267. this->_top_frame.CLEAR();
  268. }
  269. }
  270. #if GREENLET_USE_CFRAME
  271. void PythonState::set_new_cframe(_PyCFrame& frame) noexcept
  272. {
  273. frame = *PyThreadState_GET()->cframe;
  274. /* Make the target greenlet refer to the stack value. */
  275. this->cframe = &frame;
  276. /*
  277. And restore the link to the previous frame so this one gets
  278. unliked appropriately.
  279. */
  280. this->cframe->previous = &PyThreadState_GET()->root_cframe;
  281. }
  282. #endif
  283. const PythonState::OwnedFrame& PythonState::top_frame() const noexcept
  284. {
  285. return this->_top_frame;
  286. }
  287. void PythonState::did_finish(PyThreadState* tstate) noexcept
  288. {
  289. #if GREENLET_PY311
  290. // See https://github.com/gevent/gevent/issues/1924 and
  291. // https://github.com/python-greenlet/greenlet/issues/328. In
  292. // short, Python 3.11 allocates memory for frames as a sort of
  293. // linked list that's kept as part of PyThreadState in the
  294. // ``datastack_chunk`` member and friends. These are saved and
  295. // restored as part of switching greenlets.
  296. //
  297. // When we initially switch to a greenlet, we set those to NULL.
  298. // That causes the frame management code to treat this like a
  299. // brand new thread and start a fresh list of chunks, beginning
  300. // with a new "root" chunk. As we make calls in this greenlet,
  301. // those chunks get added, and as calls return, they get popped.
  302. // But the frame code (pystate.c) is careful to make sure that the
  303. // root chunk never gets popped.
  304. //
  305. // Thus, when a greenlet exits for the last time, there will be at
  306. // least a single root chunk that we must be responsible for
  307. // deallocating.
  308. //
  309. // The complex part is that these chunks are allocated and freed
  310. // using ``_PyObject_VirtualAlloc``/``Free``. Those aren't public
  311. // functions, and they aren't exported for linking. It so happens
  312. // that we know they are just thin wrappers around the Arena
  313. // allocator, so we can use that directly to deallocate in a
  314. // compatible way.
  315. //
  316. // CAUTION: Check this implementation detail on every major version.
  317. //
  318. // It might be nice to be able to do this in our destructor, but
  319. // can we be sure that no one else is using that memory? Plus, as
  320. // described below, our pointers may not even be valid anymore. As
  321. // a special case, there is one time that we know we can do this,
  322. // and that's from the destructor of the associated UserGreenlet
  323. // (NOT main greenlet)
  324. PyObjectArenaAllocator alloc;
  325. _PyStackChunk* chunk = nullptr;
  326. if (tstate) {
  327. // We really did finish, we can never be switched to again.
  328. chunk = tstate->datastack_chunk;
  329. // Unfortunately, we can't do much sanity checking. Our
  330. // this->datastack_chunk pointer is out of date (evaluation may
  331. // have popped down through it already) so we can't verify that
  332. // we deallocate it. I don't think we can even check datastack_top
  333. // for the same reason.
  334. PyObject_GetArenaAllocator(&alloc);
  335. tstate->datastack_chunk = nullptr;
  336. tstate->datastack_limit = nullptr;
  337. tstate->datastack_top = nullptr;
  338. }
  339. else if (this->datastack_chunk) {
  340. // The UserGreenlet (NOT the main greenlet!) is being deallocated. If we're
  341. // still holding a stack chunk, it's garbage because we know
  342. // we can never switch back to let cPython clean it up.
  343. // Because the last time we got switched away from, and we
  344. // haven't run since then, we know our chain is valid and can
  345. // be dealloced.
  346. chunk = this->datastack_chunk;
  347. PyObject_GetArenaAllocator(&alloc);
  348. }
  349. if (alloc.free && chunk) {
  350. // In case the arena mechanism has been torn down already.
  351. while (chunk) {
  352. _PyStackChunk *prev = chunk->previous;
  353. chunk->previous = nullptr;
  354. alloc.free(alloc.ctx, chunk, chunk->size);
  355. chunk = prev;
  356. }
  357. }
  358. this->datastack_chunk = nullptr;
  359. this->datastack_limit = nullptr;
  360. this->datastack_top = nullptr;
  361. #endif
  362. }
  363. }; // namespace greenlet
  364. #endif // GREENLET_PYTHON_STATE_CPP