@@ -1755,6 +1755,8 @@ static void error_cb (rd_kafka_t *rk, int err, const char *reason, void *opaque)
1755
1755
if (result )
1756
1756
Py_DECREF (result );
1757
1757
else {
1758
+
1759
+ PyErr_Fetch (& cs -> exception_type , & cs -> exception_value , & cs -> exception_traceback );
1758
1760
crash :
1759
1761
CallState_crash (cs );
1760
1762
rd_kafka_yield (h -> rk );
@@ -1810,6 +1812,8 @@ static void throttle_cb (rd_kafka_t *rk, const char *broker_name, int32_t broker
1810
1812
goto done ;
1811
1813
}
1812
1814
1815
+ PyErr_Fetch (& cs -> exception_type , & cs -> exception_value , & cs -> exception_traceback );
1816
+
1813
1817
/**
1814
1818
* Stop callback dispatcher, return err to application
1815
1819
* 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) {
1839
1843
if (result )
1840
1844
Py_DECREF (result );
1841
1845
else {
1846
+ PyErr_Fetch (& cs -> exception_type , & cs -> exception_value , & cs -> exception_traceback );
1842
1847
CallState_crash (cs );
1843
1848
rd_kafka_yield (h -> rk );
1844
1849
}
@@ -1874,6 +1879,7 @@ static void log_cb (const rd_kafka_t *rk, int level,
1874
1879
if (result )
1875
1880
Py_DECREF (result );
1876
1881
else {
1882
+ PyErr_Fetch (& cs -> exception_type , & cs -> exception_value , & cs -> exception_traceback );
1877
1883
CallState_crash (cs );
1878
1884
rd_kafka_yield (h -> rk );
1879
1885
}
@@ -2572,6 +2578,9 @@ void CallState_begin (Handle *h, CallState *cs) {
2572
2578
cs -> thread_state = PyEval_SaveThread ();
2573
2579
assert (cs -> thread_state != NULL );
2574
2580
cs -> crashed = 0 ;
2581
+ cs -> exception_type = NULL ;
2582
+ cs -> exception_value = NULL ;
2583
+ cs -> exception_traceback = NULL ;
2575
2584
#ifdef WITH_PY_TSS
2576
2585
PyThread_tss_set (& h -> tlskey , cs );
2577
2586
#else
@@ -2592,8 +2601,19 @@ int CallState_end (Handle *h, CallState *cs) {
2592
2601
2593
2602
PyEval_RestoreThread (cs -> thread_state );
2594
2603
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
+ }
2596
2615
return 0 ;
2616
+ }
2597
2617
2598
2618
return 1 ;
2599
2619
}
0 commit comments