From c5e23534470e118cdfc866d7a9bf0925ce764f29 Mon Sep 17 00:00:00 2001 From: Shish Date: Sun, 25 Jun 2023 14:19:02 +0100 Subject: [PATCH] manual timeouts --- core/send_event.php | 22 +++++++++++++++++++++- core/timeout.php | 32 -------------------------------- ext/admin/main.php | 4 ++-- ext/bulk_add/main.php | 4 ++-- ext/bulk_add_csv/main.php | 4 ++-- ext/cron_uploader/main.php | 2 +- ext/source_history/main.php | 3 +-- ext/tag_history/main.php | 4 +--- index.php | 2 -- 9 files changed, 30 insertions(+), 47 deletions(-) delete mode 100644 core/timeout.php diff --git a/core/send_event.php b/core/send_event.php index 9eacf5cc..c00e06f0 100644 --- a/core/send_event.php +++ b/core/send_event.php @@ -4,6 +4,11 @@ declare(strict_types=1); namespace Shimmie2; +class TimeoutException extends \RuntimeException +{ +} + + /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ * Event API * \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ @@ -93,6 +98,18 @@ function _dump_event_listeners(array $event_listeners, string $path): void /** @private */ global $_shm_event_count; $_shm_event_count = 0; +$_shm_timeout = null; + +function shm_set_timeout(?int $timeout=null): void +{ + global $_shm_timeout; + $_shm_timeout = ftime() + $timeout; + set_time_limit(is_null($timeout) ? 0 : $timeout); +} + +if(ini_get('max_execution_time')) { + shm_set_timeout((int)ini_get('max_execution_time') - 3); +} /** * Send an event to all registered Extensions. @@ -105,7 +122,7 @@ function send_event(Event $event): Event { global $tracer_enabled; - global $_shm_event_listeners, $_shm_event_count, $_tracer; + global $_shm_event_listeners, $_shm_event_count, $_tracer, $_shm_timeout; $event_name = _namespaced_class_name(get_class($event)); if (!isset($_shm_event_listeners[$event_name])) { return $event; @@ -122,6 +139,9 @@ function send_event(Event $event): Event ksort($my_event_listeners); foreach ($my_event_listeners as $listener) { + if($_shm_timeout && ftime() > $_shm_timeout) { + throw new TimeoutException(); + } if ($tracer_enabled) { $_tracer->begin(get_class($listener)); } diff --git a/core/timeout.php b/core/timeout.php deleted file mode 100644 index f54ee264..00000000 --- a/core/timeout.php +++ /dev/null @@ -1,32 +0,0 @@ -active = true; - // declare(ticks = 1); - // pcntl_signal(SIGALRM, [$this, 'handle'], true); - // pcntl_alarm($seconds); - set_time_limit($seconds + 5); - } - - public function clear() - { - set_time_limit(0); - $this->active = false; - } - - public function handle($signal) - { - if ($this->active) { - throw new TimeoutException(); - } - } -} diff --git a/ext/admin/main.php b/ext/admin/main.php index 95648585..1269fcd8 100644 --- a/ext/admin/main.php +++ b/ext/admin/main.php @@ -37,7 +37,7 @@ class AdminPage extends Extension public function onPageRequest(PageRequestEvent $event) { - global $database, $page, $user, $_shm_timeout; + global $database, $page, $user; if ($event->page_matches("admin")) { if (!$user->can(Permissions::MANAGE_ADMINTOOLS)) { @@ -51,7 +51,7 @@ class AdminPage extends Extension if ($user->check_auth_token()) { log_info("admin", "Util: $action"); - $_shm_timeout->clear(); + shm_set_timeout(null); $database->set_timeout(null); send_event($aae); } diff --git a/ext/bulk_add/main.php b/ext/bulk_add/main.php index 325cca1b..54946f7b 100644 --- a/ext/bulk_add/main.php +++ b/ext/bulk_add/main.php @@ -24,10 +24,10 @@ class BulkAdd extends Extension public function onPageRequest(PageRequestEvent $event) { - global $page, $user, $_shm_timeout; + global $page, $user; if ($event->page_matches("bulk_add")) { if ($user->can(Permissions::BULK_ADD) && $user->check_auth_token() && isset($_POST['dir'])) { - $_shm_timeout->clear(); + shm_set_timeout(null); $bae = send_event(new BulkAddEvent($_POST['dir'])); foreach ($bae->results as $result) { $this->theme->add_status("Adding files", $result); diff --git a/ext/bulk_add_csv/main.php b/ext/bulk_add_csv/main.php index 4c00c251..93089737 100644 --- a/ext/bulk_add_csv/main.php +++ b/ext/bulk_add_csv/main.php @@ -11,10 +11,10 @@ class BulkAddCSV extends Extension public function onPageRequest(PageRequestEvent $event) { - global $page, $user, $_shm_timeout; + global $page, $user; if ($event->page_matches("bulk_add_csv")) { if ($user->can(Permissions::BULK_ADD) && $user->check_auth_token() && isset($_POST['csv'])) { - $_shm_timeout->clear(); + shm_set_timeout(null); $this->add_csv($_POST['csv']); $this->theme->display_upload_results($page); } diff --git a/ext/cron_uploader/main.php b/ext/cron_uploader/main.php index 286d5e9a..e548fc85 100644 --- a/ext/cron_uploader/main.php +++ b/ext/cron_uploader/main.php @@ -350,7 +350,7 @@ class CronUploader extends Extension self::$IMPORT_RUNNING = true; try { - //$_shm_timeout->clear(); + //shm_set_timeout(null); $output_subdir = date('Ymd-His', time()); $image_queue = $this->generate_image_queue(); diff --git a/ext/source_history/main.php b/ext/source_history/main.php index 0048d7fd..8b41ea35 100644 --- a/ext/source_history/main.php +++ b/ext/source_history/main.php @@ -180,7 +180,6 @@ class SourceHistory extends Extension protected function process_bulk_revert_request() { - global $_shm_timeout; if (isset($_POST['revert_name']) && !empty($_POST['revert_name'])) { $revert_name = $_POST['revert_name']; } else { @@ -210,7 +209,7 @@ class SourceHistory extends Extension $revert_date = null; } - $_shm_timeout->clear(); // reverting changes can take a long time, disable php's timelimit if possible. + shm_set_timeout(null); // reverting changes can take a long time, disable php's timelimit if possible. // Call the revert function. $this->process_revert_all_changes($revert_name, $revert_ip, $revert_date); diff --git a/ext/tag_history/main.php b/ext/tag_history/main.php index 07bc4bba..f7050cdd 100644 --- a/ext/tag_history/main.php +++ b/ext/tag_history/main.php @@ -178,8 +178,6 @@ class TagHistory extends Extension protected function process_bulk_revert_request() { - global $_shm_timeout; - if (isset($_POST['revert_name']) && !empty($_POST['revert_name'])) { $revert_name = $_POST['revert_name']; } else { @@ -209,7 +207,7 @@ class TagHistory extends Extension $revert_date = null; } - $_shm_timeout->clear(); // reverting changes can take a long time, disable php's timelimit if possible. + shm_set_timeout(null); // reverting changes can take a long time, disable php's timelimit if possible. // Call the revert function. $this->process_revert_all_changes($revert_name, $revert_ip, $revert_date); diff --git a/index.php b/index.php index f0b559a3..c80883b6 100644 --- a/index.php +++ b/index.php @@ -46,8 +46,6 @@ _set_up_shimmie_environment(); $_tracer = new \EventTracer(); $_tracer->begin("Bootstrap"); _load_core_files(); -$_shm_timeout = new \Timeout(); -$_shm_timeout->set(((int)ini_get('max_execution_time')) || 30); $cache = loadCache(CACHE_DSN); $database = new Database(DATABASE_DSN); $config = new DatabaseConfig($database);