Opened 8 years ago
Last modified 3 weeks ago
#44977 new defect (bug)
Transient fill fail delete to itself if it's timeout option is missing
| Reported by: | nlozovan | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Options, Meta APIs | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
Description (last modified by )
Just faced this issue - for some reason, the transient was not deleting itself. While checking the DB, the option with the transient was there, while the timeout option was missing (probably a glitch while saving to DB).
Now, if you check get_transient() in option.php (quick link https://core.trac.wordpress.org/browser/tags/4.9.8/src/wp-includes/option.php#L0 ) you can see that these are deleted only if both exist and both pass the test:
[...]
if ( false !== $timeout && $timeout < time() ) { DELETING TRANSIENT}
[...]
Otherwise transient will hang... forever. This should be tuned up so it also checks that these options exist, and delete transient if timeout is absolete.
Change History (6)
#2
@
8 years ago
- Component Cache API → Options, Meta APIs
- Description modified (diff)
- Summary Transient fill fail delete itself if it's timeout option is absolete → Transient fill fail delete to itself if it's timeout option is missing
#4
@
5 weeks ago
Replying to geekofshire (comment:3): I don't think get_transient() can distinguish the two cases. A transient set with $expiration = 0 never gets a _transient_timeout_ row at all, so "value row present, timeout row missing" is the normal state of every non-expiring transient, not just the result of a failed write. Treating a missing timeout as expired would delete all of those on first read.
The only way to tell them apart would be at write time, and #65863 already covers the write-ordering problem in set_transient() (timeout row first, value row second, no transaction), together with the mirrored orphan case and a cleanup pass in delete_expired_transients().
Given that, I'd suggest closing this one as a duplicate of #65863 unless there's a cheap way to detect the failed-write case that I'm missing.
#5
@
4 weeks ago
Tested against current trunk. Simulating a failed timeout INSERT causes set_transient() to return true and store the value without expiration metadata, so there is a reproducible write-side failure here.
#52345 appears to be the closer duplicate: it reports this exact behavior and already discusses handling failed timeout writes in set_transient(). The cleanup proposed in #65863 targets timeout rows whose value is missing, so it would not resolve the state reported here.
Suggest consolidating this ticket with #52345 and keeping #65863 as a related ticket.
This ticket was mentioned in PR #14743 on WordPress/wordpress-develop by @therssoftware.
3 weeks ago
#6
- Keywords has-patch has-unit-tests added; needs-patch removed
Description
When set_transient() or set_site_transient() creates or updates a transient with an expiration, it writes both the timeout row (_transient_timeout_{name} / _site_transient_timeout_{name}) and the value row (_transient_{name} / _site_transient_{name}).
Previously, the result of storing the timeout row was ignored or bypassed on certain paths:
- When adding a new transient or site transient with an expiration, if the database rejected the timeout row
INSERT, the value was still stored without an expiration timestamp. With no timeout row,get_transient()andget_site_transient()treat the transient as permanent (never expiring). - When updating an existing non-expiring transient or site transient to have an expiration, if the timeout row
INSERTfailed, the value was still updated or stored without an expiration timestamp.
Furthermore, on the read path in get_transient(), a transient without a timeout row cannot simply be deleted on read if it is not in alloptions, because legitimate non-expiring transients whose serialized size exceeds the autoload size threshold (150KB) have autoload = 'off' and legitimately have no timeout row.
Changes
set_transient()(src/wp-includes/option.php):- On the add path, verifies that
add_option( $transient_timeout, ... )succeeded, had no database error, or the timeout option already exists before storing the value row. - On the update path (adding an expiry to an existing transient without a timeout), verifies that
add_option( $transient_timeout, ... )succeeded, had no database error, or the timeout option already exists before adding the value row. - Placed
global $wpdb;immediately before first use in each branch and documented@global wpdb $wpdbin DocBlock.
- On the add path, verifies that
set_site_transient()(src/wp-includes/option.php):- On the add path, verifies that
add_site_option( $transient_timeout, ... )succeeded, had no database error, or the timeout option already exists before storing the value row. - On the update path (adding an expiry to an existing site transient without a timeout), verifies that
add_site_option( $transient_timeout, ... )succeeded, had no database error, or the timeout option already exists before updating the value row. - Placed
global $wpdb;immediately before first use in each branch and documented@global wpdb $wpdbin DocBlock.
- On the add path, verifies that
- PHPUnit Tests (
tests/phpunit/tests/option/transient.php&tests/phpunit/tests/option/siteTransient.php):test_set_transient_should_not_store_value_when_timeout_cannot_be_added: verifies new transient value is not stored when timeout INSERT fails.test_set_transient_should_not_store_value_when_timeout_cannot_be_added_to_existing_transient: verifies value is not stored when adding expiry to existing transient fails.test_set_transient_should_keep_latest_value_when_set_twice_with_same_expiry: verifies setting same transient twice with identical expiry retains the latest value.test_set_transient_should_store_value_when_orphaned_timeout_exists: verifies setting transient succeeds when orphaned timeout is already present.test_set_transient_should_store_value_when_timeout_was_added_concurrently: verifies concurrent timeout row insert succeeds.test_set_transient_should_store_value_when_timeout_was_added_concurrently_to_existing_transient: verifies concurrent timeout row insert on existing transient succeeds.test_get_transient_should_not_delete_non_autoloaded_transient_without_timeout: regression test verifyingget_transient()does not delete non-autoloaded transients that lack a timeout row.- Equivalent unit tests added to
siteTransient.phpcoveringset_site_transient()across all branches on both single-site and multisite.
Testing Instructions
- Run PHPUnit on both test files:
vendor/bin/phpunit tests/phpunit/tests/option/transient.php tests/phpunit/tests/option/siteTransient.php
- Run PHPUnit on multisite:
vendor/bin/phpunit -c tests/phpunit/multisite.xml tests/phpunit/tests/option/transient.php tests/phpunit/tests/option/siteTransient.php
- Run the full option test group:
vendor/bin/phpunit --group option
- Verify code standards with PHPCS:
vendor/bin/phpcs src/wp-includes/option.php tests/phpunit/tests/option/transient.php tests/phpunit/tests/option/siteTransient.php
How should we handle transients that are saved without a timeout due to a glitch? Should we add a check for this wherever we delete the transient?
if ( false === $timeout || ( false !== $timeout && $timeout < time() ) ) {