TPythonState.cpp 17 KB

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