Fix: always restore error handler in object cache Redis _connect() (finally + \Throwable)#1014
Open
vbakkenes wants to merge 1 commit into
Open
Conversation
_connect() sets litespeed_exception_handler around the Redis connection but only caught \Exception/\ErrorException and called restore_error_handler() after the try/catch. A \Error escaping the connect path (e.g. a TypeError on PHP 8.x) skipped both catches and the restore, leaking the handler for the rest of the request. It then rethrew every later unrelated PHP deprecation (Gravity Forms dirname(null), Wordfence strrpos(null), ...) as a fatal ErrorException, white-screening the whole site even with WP_DEBUG off. Catch \Throwable (also removes the unreachable \ErrorException catch, since \ErrorException extends \Exception) and move restore_error_handler() into a finally so the handler is always restored. Fixes litespeedtech#1013 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Object_Cache::_connect()registersset_error_handler( 'litespeed_exception_handler' )around the Redis connection, but only catches\Exception/\ErrorExceptionand callsrestore_error_handler()after thetry/catch. If a\Error(e.g. aTypeError/ValueErroron PHP 8.x) escapes the connect path, bothcatchblocks are skipped,restore_error_handler()never runs, andlitespeed_exception_handlerstays registered for the rest of the request.Since that handler rethrows every PHP error as an
ErrorException, any later unrelated PHP 8.1+ deprecation — e.g. Gravity Formsdirname(null)or Wordfencestrrpos(null)— becomes a fatalUncaught ErrorException, white-screening the whole site even withWP_DEBUGoff. The stack trace points at the other plugin, so it's very hard to trace back to the object cache.Also, the
catch ( \ErrorException $e )aftercatch ( \Exception $e )was unreachable —\ErrorExceptionextends\Exception.Fix
Catch
\Throwable(covers\Errortoo, and folds in the previously-unreachable\ErrorExceptionbranch) and moverestore_error_handler()into afinallyso the handler is always restored.Notes
\Throwable/finallyare PHP 7.0+, within the plugin'sRequires PHP: 7.2.$failed = trueand fall back as before.Fixes #1013