TPythonState.cpp 16 KB

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