:`. A request that died holding it is taken over * after STALE seconds. It is reentrant within a request (a replacement's roll-back, a restore inside * a write), and released at shutdown too, so a request killed by a time or memory limit doesn't keep * the page locked. Callers check the user may write the target before they lock it. */ final class Lock { const PREFIX = '_adibridge_lock_'; /** Seconds a write waits for another write to the same page before giving up. */ const WAIT = 8; /** Seconds after which a lock is treated as left behind by a request that died. */ const STALE = 120; /** @var array option name => owner, for locks this request holds. */ private static $held = array(); /** Run $fn while holding the write lock for $target (a post id or "options:"). */ public static function with( $target, callable $fn ) { $name = self::name( $target ); if ( isset( self::$held[ $name ] ) ) { return $fn(); } $owner = wp_generate_password( 20, false ); $deadline = microtime( true ) + (float) apply_filters( 'adibridge_lock_wait', self::WAIT ); while ( ! self::acquire( $name, $owner ) ) { if ( microtime( true ) > $deadline ) { throw new Tool_Error( 'Another change to this page is still being saved. Nothing was changed; try again in a moment.' ); } usleep( 50000 + wp_rand( 0, 50000 ) ); } self::$held[ $name ] = $owner; static $shutdown = false; if ( ! $shutdown ) { $shutdown = true; register_shutdown_function( array( self::class, 'release_all' ) ); // WordPress's own fatal-error handler runs first (registered before plugins load) and ends // with wp_die(), which stops the remaining shutdown functions. wp_php_error_args is applied // for every fatal it handles, just before that wp_die(): release there too. add_filter( 'wp_php_error_args', array( self::class, 'release_on_fatal' ), 0 ); } try { return $fn(); } finally { unset( self::$held[ $name ] ); self::release( $name, $owner ); } } /** wp_php_error_args: release before WordPress's fatal-error handler shows its page and dies. */ public static function release_on_fatal( $args ) { self::release_all(); return $args; } /** Shutdown: whatever this request still holds (a fatal error skips `finally`). */ public static function release_all() { foreach ( self::$held as $name => $owner ) { self::release( $name, $owner ); } self::$held = array(); } private static function name( $target ) { $key = is_string( $target ) && ( 0 === strpos( $target, 'options:' ) || 0 === strpos( $target, 'site:' ) ) ? $target : 'post:' . (int) $target; return self::PREFIX . md5( $key ); } private static function acquire( $name, $owner ) { global $wpdb; $value = time() . ':' . $owner; $done = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->options} (option_name, option_value, autoload) VALUES (%s, %s, 'no')", $name, $value ) ); if ( 1 === (int) $done ) { return true; } $current = $wpdb->get_var( $wpdb->prepare( "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s", $name ) ); if ( null === $current ) { return false; // released in between: the next round takes it } $since = (int) strtok( (string) $current, ':' ); if ( $since && $since < time() - self::STALE ) { // Left behind by a request that died: remove exactly that lock (never one a live writer just took). $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name = %s AND option_value = %s", $name, $current ) ); Log::line( 'took over a write lock left for ' . ( time() - $since ) . 's' ); } return false; } private static function release( $name, $owner ) { global $wpdb; $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->options} WHERE option_name = %s AND option_value LIKE %s", $name, '%:' . $wpdb->esc_like( $owner ) ) ); } }