Ë
    G~éj   ã                   ó"  — d Z ddlZddlZddlZddlZddlZ eej                  j                  dd«      «      Z	 e
ej                  j                  dd«      «      Zd„ Z G d„ d	«      Zd
„ Zd„ Zd„ Zedk(  r e«         ej$                  d«       yy)aT  Reproducer for the free-threaded GC crash in issue #515.

https://github.com/python-greenlet/greenlet/issues/515

Before the fix, ``set_initial_state`` copied the parent thread state's
``_PyCStackRef`` list head into each new greenlet. Those nodes sit on the
parent greenlet's C stack, so once the child starts running on its own stack
and overwrites that region their ``next`` pointers dangle. The free-threaded
collector walks ``c_stack_refs`` for every thread, so a GC on any thread ends
up chasing those dangling nodes (inside ``gc_collect_main`` ->
``gc_visit_thread_stacks``) while such a child is active, and segfaults.

The fiddly part is getting a *non-empty* list to inherit at all. The
interpreter only holds a ``_PyCStackRef`` transiently, while it is resolving an
attribute -- ``_Py_LoadAttr_StackRefSteal`` pins ``self`` across ``obj.attr()``
-- so the greenlet has to be primed from inside that window. Priming it from a
property getter does exactly that; it is also the same deep-attribute context
Playwright's sync bridge happens to start its fiber from. A churn thread then
keeps the collector busy while the dispatcher runs on its stale list.

A fixed build prints "ISSUE 515 OK"; a regressed one segfaults in about a
second. With-GIL builds are unaffected -- ordinary refcounting keeps the nodes
alive -- so there it simply prints the OK line.
é    NÚREPRO_SECONDSÚ4ÚREPRO_THREADSÚ6c                  óv   — dj                  «       j                  «       j                  «       j                  «       S )NÚabc)ÚupperÚlowerÚstripÚtitle© ó    úb/home/boxd/.venv-cloak/lib/python3.12/site-packages/greenlet/tests/fail_issue_515_freethread_gc.pyÚ_busyr   &   s+   € ð �;‰;‹=×ÑÓ ×&Ñ&Ó(×.Ñ.Ó0Ð0r   c                   ó4   — e Zd Zd„ Zd„ Zed„ «       Zd„ Zd„ Zy)ÚBridgec                 óŒ   — || _         t        j                  «       | _        t        j                  | j                  «      | _        y ©N)ÚstopÚgreenletÚ
getcurrentÚmainÚ_loopÚdisp)Úselfr   s     r   Ú__init__zBridge.__init__-   s/   € ØˆŒ	Ü×'Ñ'Ó)ˆŒ	Ü×%Ñ% d§j¡jÓ1ˆ�	r   c                 ó¬   — | j                   d   sEt        d«      D ]  }t        «        Œ | j                  j	                  «        | j                   d   sŒDy y )Nr   é2   )r   Úranger   r   Úswitch)r   Ú_s     r   r   zBridge._loop2   sB   € Ø—)‘)˜A’,Ü˜2“Yò �Ü•ðà�I‰I×ÑÔð —)‘)˜A•,r   c                 ó8   — | j                   j                  «        y r   ©r   r    ©r   s    r   Ú_primezBridge._prime8   s   € ð
 	�	‰	×ÑÔØr   c                 ó   — | j                   S r   )r%   r$   s    r   ÚstartzBridge.start@   s   € ð
 �{‰{Ðr   c                 ó8   — | j                   j                  «        y r   r#   r$   s    r   ÚresumezBridge.resumeG   s   € Ø�	‰	×ÑÕr   N)	Ú__name__Ú
__module__Ú__qualname__r   r   Úpropertyr%   r'   r)   r   r   r   r   r   ,   s*   „ ò2ò
ð ñó ðòór   r   c                 ór   — t        | «      }|j                  «        | d   s|j                  «        | d   sŒy y )Nr   )r   r'   r)   )r   Úbridges     r   Úworkerr0   K   s/   € Ü�D‹\€FØ
‡L�L„Nð �1ŠgØ�‰Œð �1�gr   c                 óÔ   — | d   s^t        d«      D �cg c]  }|d dœ‘Œ	 }}t        ||dd  «      D ]
  \  }}||d<   Œ |d   |d   d<   ~t        j                  «        | d   sŒ]y y c c}w )Nr   iô  )ÚiÚnexté   r3   éÿÿÿÿ)r   ÚzipÚgcÚcollect)r   r2   ÚnodesÚaÚbs        r   Úchurnr<   T   s|   € Ø�1Šgô 27°s³Ö<¨A�q $Ó'Ð<ˆÐ<Ü˜˜u Q R˜yÓ)ò 	‰DˆAˆqØˆAˆfŠIð	à! !™Hˆˆb‰	�&ÑØÜ
�
‰
Œð �1�gùò =s   “A%c            	      ó”  — dg} t        j                  t        | fd¬«      g}|t        t        «      D �cg c]  }t        j                  t
        | fd¬«      ‘Œ! c}z  }|D ]  }|j                  «        Œ t        j                  «       j                  t        «       d| d<   |D ]  }|j                  d¬«       Œ t        d«       y c c}w )NFT)ÚtargetÚargsÚdaemonr   é   )ÚtimeoutzISSUE 515 OK)Ú	threadingÚThreadr<   r   ÚNWORKERSr0   r'   ÚEventÚwaitÚDURATIONÚjoinÚprint)r   Úthreadsr!   Úts       r   r   r   `   s·   € Øˆ7€DÜ×Ñ¤u°D°7À4ÔHÐI€GØÜœx›ö*Øô × Ñ ¬°d°WÀTÖJò *ñ *€Gàò ˆØ	�‰�	ðä‡O�OÓ×Ñœ8Ô$Ø€Dˆ�GØò ˆØ	�‰�qˆÕðä	ˆ.Õùò*s   ´$CÚ__main__)Ú__doc__r7   ÚosÚsysrC   r   ÚfloatÚenvironÚgetrH   ÚintrE   r   r   r0   r<   r   r*   Úexitr   r   r   ú<module>rV      sŽ   ðñó0 
Û 	Û 
Û ã ñ �—‘—‘ °Ó5Ó6€Ùˆr�z‰z�~‰~˜o¨sÓ3Ó4€ò1÷ñ ò>ò	òð ˆzÒÙ„FØ€C‡H�HˆQ…Kð r   