1212 *
1313 * Tested only on gcc, linux, x86_64.
1414 *
15- * Copyright (C) 2014-2015
15+ * Copyright (C) 2014-2017
1616 * Antonio Cuni - anto.cuni@gmail.com
1717 * Maciej Fijalkowski - fijall@gmail.com
1818 * Armin Rigo - arigo@tunes.org
19+ * Richard Plangger - planrichi@gmail.com
1920 *
2021 */
2122
@@ -145,6 +146,35 @@ int _vmprof_sample_stack(struct profbuf_s *p, PY_THREAD_STATE_T * tstate, uconte
145146 return 1 ;
146147}
147148
149+ #ifndef RPYTHON_VMPROF
150+ static PY_THREAD_STATE_T * _get_pystate_for_this_thread (void ) {
151+ // see issue 116 on github.com/vmprof/vmprof-python.
152+ // PyGILState_GetThisThreadState(); can hang forever
153+ //
154+ PyInterpreterState * istate ;
155+ PyThreadState * state ;
156+ long mythread_id ;
157+
158+ istate = PyInterpreterState_Head ();
159+ if (istate == NULL ) {
160+ return NULL ;
161+ }
162+ mythread_id = PyThread_get_thread_ident ();
163+ // fish fish fish, it will NOT lock the keymutex in pythread
164+ do {
165+ state = PyInterpreterState_ThreadHead (istate );
166+ do {
167+ if (state -> thread_id == mythread_id ) {
168+ return state ;
169+ }
170+ } while ((state = PyThreadState_Next (state )) != NULL );
171+ } while ((istate = PyInterpreterState_Next (istate )) != NULL );
172+
173+ // uh? not found?
174+ return NULL ;
175+ }
176+ #endif
177+
148178static void sigprof_handler (int sig_nr , siginfo_t * info , void * ucontext )
149179{
150180 int commit ;
@@ -167,7 +197,7 @@ static void sigprof_handler(int sig_nr, siginfo_t* info, void *ucontext)
167197 int fault_code = setjmp (restore_point );
168198 if (fault_code == 0 ) {
169199 pthread_self ();
170- tstate = PyGILState_GetThisThreadState ();
200+ tstate = _get_pystate_for_this_thread ();
171201 } else {
172202 signal (SIGSEGV , prevhandler );
173203 __sync_lock_release (& spinlock );
0 commit comments