@@ -1755,6 +1755,8 @@ static void error_cb (rd_kafka_t *rk, int err, const char *reason, void *opaque)
17551755 if (result )
17561756 Py_DECREF (result );
17571757 else {
1758+
1759+ PyErr_Fetch (& cs -> exception_type , & cs -> exception_value , & cs -> exception_traceback );
17581760 crash :
17591761 CallState_crash (cs );
17601762 rd_kafka_yield (h -> rk );
@@ -1810,6 +1812,8 @@ static void throttle_cb (rd_kafka_t *rk, const char *broker_name, int32_t broker
18101812 goto done ;
18111813 }
18121814
1815+ PyErr_Fetch (& cs -> exception_type , & cs -> exception_value , & cs -> exception_traceback );
1816+
18131817 /**
18141818 * Stop callback dispatcher, return err to application
18151819 * fall-through to unlock GIL
@@ -1839,6 +1843,7 @@ static int stats_cb(rd_kafka_t *rk, char *json, size_t json_len, void *opaque) {
18391843 if (result )
18401844 Py_DECREF (result );
18411845 else {
1846+ PyErr_Fetch (& cs -> exception_type , & cs -> exception_value , & cs -> exception_traceback );
18421847 CallState_crash (cs );
18431848 rd_kafka_yield (h -> rk );
18441849 }
@@ -1874,6 +1879,7 @@ static void log_cb (const rd_kafka_t *rk, int level,
18741879 if (result )
18751880 Py_DECREF (result );
18761881 else {
1882+ PyErr_Fetch (& cs -> exception_type , & cs -> exception_value , & cs -> exception_traceback );
18771883 CallState_crash (cs );
18781884 rd_kafka_yield (h -> rk );
18791885 }
@@ -2572,6 +2578,9 @@ void CallState_begin (Handle *h, CallState *cs) {
25722578 cs -> thread_state = PyEval_SaveThread ();
25732579 assert (cs -> thread_state != NULL );
25742580 cs -> crashed = 0 ;
2581+ cs -> exception_type = NULL ;
2582+ cs -> exception_value = NULL ;
2583+ cs -> exception_traceback = NULL ;
25752584#ifdef WITH_PY_TSS
25762585 PyThread_tss_set (& h -> tlskey , cs );
25772586#else
@@ -2592,8 +2601,19 @@ int CallState_end (Handle *h, CallState *cs) {
25922601
25932602 PyEval_RestoreThread (cs -> thread_state );
25942603
2595- if (PyErr_CheckSignals () == -1 || cs -> crashed )
2604+ if (PyErr_CheckSignals () == -1 )
2605+ return 0 ;
2606+
2607+ if (cs -> crashed ) {
2608+ /* Restore the saved exception if we have one */
2609+ if (cs -> exception_type ) {
2610+ PyErr_Restore (cs -> exception_type , cs -> exception_value , cs -> exception_traceback );
2611+ cs -> exception_type = NULL ;
2612+ cs -> exception_value = NULL ;
2613+ cs -> exception_traceback = NULL ;
2614+ }
25962615 return 0 ;
2616+ }
25972617
25982618 return 1 ;
25992619}
0 commit comments