Fix WatchDog interrupt function hook
Hook and unhook it in runtime is not thread safe...
@@ -27,7 +27,7 @@ SWOW_API zend_class_entry *swow_watch_dog_exception_ce;
typedef void (*swow_interrupt_function_t)(zend_execute_data *execute_data);
-static swow_interrupt_function_t original_zend_interrupt_function;
+static swow_interrupt_function_t original_zend_interrupt_function = (swow_interrupt_function_t) -1;
SWOW_API void swow_watch_dog_alert_standard(cat_watch_dog_t *watch_dog)
{
@@ -176,6 +176,11 @@ static PHP_METHOD(Swow_WatchDog, run)
Z_PARAM_ZVAL(zalerter)
ZEND_PARSE_PARAMETERS_END();
+ if (original_zend_interrupt_function == (swow_interrupt_function_t) -1) {
+ original_zend_interrupt_function = zend_interrupt_function;
+ zend_interrupt_function = swow_watch_dog_interrupt_function;
+ }
+
ret = swow_watch_dog_run(quantum, threshold, zalerter);
if (UNEXPECTED(!ret)) {
@@ -244,9 +249,6 @@ int swow_watch_dog_runtime_init(INIT_FUNC_ARGS)
return FAILURE;
}
- original_zend_interrupt_function = zend_interrupt_function;
- zend_interrupt_function = swow_watch_dog_interrupt_function;
-
return SUCCESS;
}
@@ -256,9 +258,6 @@ int swow_watch_dog_runtime_shudtown(INIT_FUNC_ARGS)
cat_core_error_with_last(WATCH_DOG, "WatchDog stop failed");
}
- zend_interrupt_function = original_zend_interrupt_function;
- original_zend_interrupt_function = NULL;
-
if (!cat_watch_dog_runtime_shutdown()) {
return FAILURE;
}
