1
0
mirror of https://github.com/php/php-src.git synced 2026-04-27 10:16:41 +02:00

Fixed bug #68583 Crash in timeout thread

This replaces the GUI element used for execution timeout handling
on Windows. Instead a timer queue technique is used, which is indeed
a thread pool. A timer queue timer is a lightweight object handled
but that thread pool and the timer thread spends most of the time
sleeping and waiting for an alert.

Please note also that this introduces neither binary nor source
breach. The custom timeout thread functions are deleted, however
they was not exported throug DLL, so couldn't be used by any
external code. As well they couldn't be used anywhere in the core
except in executor api, because those custom timeout thread
functions they used to operate on static variables which would
be overwritten (and that would blow).

So instead a relatively modern technique is used for the timeout
handling. It's still not perfect because the executor still has to
check EX(timed_out). This can be a topic for an improvement in
master. But brobably can be tricky as currently it seems to be not
possible to signal an individual thread. Also note another issue
that static variables aren't thread safe, but the current timer
implementation is.
This commit is contained in:
Anatol Belski
2014-12-12 10:43:31 +01:00
parent c5f7dcd1dc
commit 3affc0e8a2
4 changed files with 42 additions and 128 deletions
+1
View File
@@ -12,6 +12,7 @@ PHP NEWS
. Fixed bug #65769 (localeconv() broken in TS builds). (Anatol)
. Fixed bug #65230 (setting locale randomly broken). (Anatol)
. Fixed bug #68545 (NULL pointer dereference in unserialize.c). (Anatol)
. Fixed bug #68583 (Crash in timeout thread). (Anatol)
- cURL:
. Fixed bug #67643 (curl_multi_getcontent returns '' when
-3
View File
@@ -809,9 +809,6 @@ void zend_shutdown(TSRMLS_D) /* {{{ */
{
#ifdef ZEND_SIGNALS
zend_signal_shutdown(TSRMLS_C);
#endif
#ifdef ZEND_WIN32
zend_shutdown_timeout_thread();
#endif
zend_destroy_rsrc_list(&EG(persistent_list) TSRMLS_CC);
-7
View File
@@ -352,13 +352,6 @@ ZEND_API zend_class_entry *zend_fetch_class(const char *class_name, uint class_n
ZEND_API zend_class_entry *zend_fetch_class_by_name(const char *class_name, uint class_name_len, const zend_literal *key, int fetch_type TSRMLS_DC);
void zend_verify_abstract_class(zend_class_entry *ce TSRMLS_DC);
#ifdef ZEND_WIN32
void zend_init_timeout_thread(void);
void zend_shutdown_timeout_thread(void);
#define WM_REGISTER_ZEND_TIMEOUT (WM_USER+1)
#define WM_UNREGISTER_ZEND_TIMEOUT (WM_USER+2)
#endif
#define zendi_zval_copy_ctor(p) zval_copy_ctor(&(p))
#define zendi_zval_dtor(p) zval_dtor(&(p))
+41 -118
View File
@@ -46,13 +46,10 @@ ZEND_API const zend_fcall_info empty_fcall_info = { 0, NULL, NULL, NULL, NULL, 0
ZEND_API const zend_fcall_info_cache empty_fcall_info_cache = { 0, NULL, NULL, NULL, NULL };
#ifdef ZEND_WIN32
#include <process.h>
static WNDCLASS wc;
static HWND timeout_window;
static HANDLE timeout_thread_event;
static HANDLE timeout_thread_handle;
static DWORD timeout_thread_id;
static int timeout_thread_initialized=0;
#ifdef ZTS
__declspec(thread)
#endif
HANDLE tq_timer = NULL;
#endif
#if 0&&ZEND_DEBUG
@@ -1339,115 +1336,19 @@ ZEND_API void zend_timeout(int dummy) /* {{{ */
/* }}} */
#ifdef ZEND_WIN32
static LRESULT CALLBACK zend_timeout_WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) /* {{{ */
VOID CALLBACK tq_timer_cb(PVOID arg, BOOLEAN timed_out)
{
#ifdef ZTS
THREAD_T thread_id = (THREAD_T)wParam;
#endif
zend_bool *php_timed_out;
switch (message) {
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_REGISTER_ZEND_TIMEOUT:
/* wParam is the thread id pointer, lParam is the timeout amount in seconds */
if (lParam == 0) {
KillTimer(timeout_window, wParam);
} else {
#ifdef ZTS
void ***tsrm_ls;
#endif
SetTimer(timeout_window, wParam, lParam*1000, NULL);
#ifdef ZTS
tsrm_ls = ts_resource_ex(0, &thread_id);
if (!tsrm_ls) {
/* shouldn't normally happen */
break;
}
#endif
EG(timed_out) = 0;
}
break;
case WM_UNREGISTER_ZEND_TIMEOUT:
/* wParam is the thread id pointer */
KillTimer(timeout_window, wParam);
break;
case WM_TIMER: {
#ifdef ZTS
void ***tsrm_ls;
tsrm_ls = ts_resource_ex(0, &thread_id);
if (!tsrm_ls) {
/* Thread died before receiving its timeout? */
break;
}
#endif
KillTimer(timeout_window, wParam);
EG(timed_out) = 1;
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
/* }}} */
static unsigned __stdcall timeout_thread_proc(void *pArgs) /* {{{ */
{
MSG message;
wc.style=0;
wc.lpfnWndProc = zend_timeout_WndProc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hInstance=NULL;
wc.hIcon=NULL;
wc.hCursor=NULL;
wc.hbrBackground=(HBRUSH)(COLOR_BACKGROUND + 5);
wc.lpszMenuName=NULL;
wc.lpszClassName = "Zend Timeout Window";
if (!RegisterClass(&wc)) {
return -1;
}
timeout_window = CreateWindow(wc.lpszClassName, wc.lpszClassName, 0, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, NULL, NULL);
SetEvent(timeout_thread_event);
while (GetMessage(&message, NULL, 0, 0)) {
SendMessage(timeout_window, message.message, message.wParam, message.lParam);
if (message.message == WM_QUIT) {
break;
}
}
DestroyWindow(timeout_window);
UnregisterClass(wc.lpszClassName, NULL);
SetEvent(timeout_thread_handle);
return 0;
}
/* }}} */
void zend_init_timeout_thread(void) /* {{{ */
{
timeout_thread_event = CreateEvent(NULL, FALSE, FALSE, NULL);
timeout_thread_handle = CreateEvent(NULL, FALSE, FALSE, NULL);
_beginthreadex(NULL, 0, timeout_thread_proc, NULL, 0, &timeout_thread_id);
WaitForSingleObject(timeout_thread_event, INFINITE);
}
/* }}} */
void zend_shutdown_timeout_thread(void) /* {{{ */
{
if (!timeout_thread_initialized) {
/* The doc states it'll be always true, however it theoretically
could be FALSE when the thread was signaled. */
if (!timed_out) {
return;
}
PostThreadMessage(timeout_thread_id, WM_QUIT, 0, 0);
/* Wait for thread termination */
WaitForSingleObject(timeout_thread_handle, 5000);
CloseHandle(timeout_thread_handle);
timeout_thread_initialized = 0;
php_timed_out = (zend_bool *)arg;
*php_timed_out = 1;
}
/* }}} */
#endif
/* This one doesn't exists on QNX */
@@ -1465,13 +1366,28 @@ void zend_set_timeout(long seconds, int reset_signals) /* {{{ */
if(!seconds) {
return;
}
if (timeout_thread_initialized == 0 && InterlockedIncrement(&timeout_thread_initialized) == 1) {
/* We start up this process-wide thread here and not in zend_startup(), because if Zend
* is initialized inside a DllMain(), you're not supposed to start threads from it.
*/
zend_init_timeout_thread();
/* Don't use ChangeTimerQueueTimer() as it will not restart an expired
timer, so we could end up with just an ignored timeout. Instead
delete and recreate. */
if (NULL != tq_timer) {
if (!DeleteTimerQueueTimer(NULL, tq_timer, NULL)) {
EG(timed_out) = 0;
tq_timer = NULL;
zend_error(E_ERROR, "Could not delete queued timer");
return;
}
tq_timer = NULL;
}
PostThreadMessage(timeout_thread_id, WM_REGISTER_ZEND_TIMEOUT, (WPARAM) GetCurrentThreadId(), (LPARAM) seconds);
/* XXX passing NULL means the default timer queue provided by the system is used */
if (!CreateTimerQueueTimer(&tq_timer, NULL, (WAITORTIMERCALLBACK)tq_timer_cb, (VOID*)&EG(timed_out), seconds*1000, 0, WT_EXECUTEONLYONCE)) {
EG(timed_out) = 0;
tq_timer = NULL;
zend_error(E_ERROR, "Could not queue new timer");
return;
}
EG(timed_out) = 0;
#else
# ifdef HAVE_SETITIMER
{
@@ -1513,9 +1429,16 @@ void zend_set_timeout(long seconds, int reset_signals) /* {{{ */
void zend_unset_timeout(TSRMLS_D) /* {{{ */
{
#ifdef ZEND_WIN32
if(timeout_thread_initialized) {
PostThreadMessage(timeout_thread_id, WM_UNREGISTER_ZEND_TIMEOUT, (WPARAM) GetCurrentThreadId(), (LPARAM) 0);
if (NULL != tq_timer) {
if (!DeleteTimerQueueTimer(NULL, tq_timer, NULL)) {
EG(timed_out) = 0;
tq_timer = NULL;
zend_error(E_ERROR, "Could not delete queued timer");
return;
}
tq_timer = NULL;
}
EG(timed_out) = 0;
#else
# ifdef HAVE_SETITIMER
if (EG(timeout_seconds)) {