Skip to content

Fix sync loop flagging PDFs and other files without dimensions - #1401

Open
gabrielcld2 wants to merge 4 commits into
developfrom
fix/1397-non-image-sync-loop
Open

gabrielcld2 wants to merge 4 commits into
developfrom
fix/1397-non-image-sync-loop

Conversation

@gabrielcld2

@gabrielcld2 gabrielcld2 commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #1397

Approach

Since 3.3.9, files without width and height (PDF, SVG, documents, spreadsheets, CSV, archives, audio) uploaded in the admin get the sync error Sync loop detected: the "delivery" sync state could not be saved. and never sync to Cloudinary.

Preparing the sync of such a file asks for its URL, because no sized URL exists for it. In the admin, the attachment URL filter calls back into Media::cloudinary_id(), which prepared the sync again. That nesting used to stop because Utils::is_saving_metadata() checked did_action(). #1303 changed it to doing_action(), so the nesting continued until the loop guard from #1328 stopped it and saved the error.

Two guards, each with its own PHPUnit test:

  • Delivery::create_delivery() now gets the local path from Media::local_url(), which pauses the plugin's URL filter, the same way get_sized() already does for images. This removes the re-entry at its source. It also stops a Cloudinary URL from being stored as the local path, when the attachment already has a Cloudinary ID in the same request.
  • Media::cloudinary_id() caches a placeholder for the attachment before it prepares the sync. A nested call during preparation, reached any other way, gets the local URL instead of preparing the sync again. The outer call then replaces the placeholder with the real ID.
  • On the first upgrade to a version with this fix, Sync::clear_delivery_loop_errors() removes that exact error (in English and the current locale) if the site ran 3.3.9 or any later release without the fix, so the affected files sync again. It runs once, recorded in the _cloudinary_delivery_loop_errors_cleared option, so loop errors flagged after the fix stay visible. Cleared files aren't re-queued: they sync the next time they're shown in the admin, or through bulk sync. It doesn't touch other sync errors, or loop errors on other sync types.
  • The loop error message moved to Sync::get_loop_error_message(), so the guard and the cleanup use the same string.

QA notes

  1. With auto sync and SVG support on, upload a PDF from Media > Add New.
  2. Confirm the attachment shows no sync error and syncs to Cloudinary, with a res.cloudinary.com URL.
  3. Repeat with an SVG, an MP3 and a DOCX file.
  4. Upgrade path: on a 3.3.9 site with an errored PDF, update to this build. The error clears and the PDF syncs.

Automated:

  • New PHPUnit tests in test-media-cloudinary-id-reentry.php (9 tests). Removing either guard fails its own test, and removing both fails the original scenario as well. The cleanup tests cover the version range and the run-once behaviour. The full suite passes (129 tests).
  • New e2e spec cloudinary-upload-sync.spec.js uploads a PDF, SVG (with SVG support on), DOCX, XLSX, CSV, ZIP and MP3 through the Media Library, plus a JPG and an MP4 to check files with dimensions still sync. Together they cover Cloudinary's image, video and raw resource types. The other specs upload through REST, which skips the sync preparation, so they can't catch this.
    • Without the fix, the 7 files without dimensions fail with the error the customer reported. The JPG and MP4 pass.
    • All files go up in one batch and the test waits for the auto-sync queue, so it takes about 35s. The full e2e suite passes locally.

Formats checked by hand on the tests site, one upload each, with and without the fix: SVG with only a viewBox, AVIF, WebP, GIF, PNG, BMP, PPTX, TXT and WAV all behave the same way. TIFF doesn't sync either way, with no error shown, so that isn't from this bug. It needs a separate look.

Files without width and height (PDF, audio, documents) got the error
'Sync loop detected: the "delivery" sync state could not be saved.'
when uploaded in the admin. Preparing their sync asks for the
attachment URL, and the admin URL filter called back into
Media::cloudinary_id(), which prepared the sync again until the loop
guard tripped.

Answer that nested call with the local URL instead. On upgrade from
3.3.9, clear the delivery loop errors it flagged so those files sync.

Adds PHPUnit regression tests and an e2e test that uploads a PDF
through the Media Library.

Fixes #1397
The sync loop hit every file without dimensions, not just PDFs: SVG,
documents, spreadsheets, CSV, archives and audio. Upload one of each
through the Media Library, with SVG support on, plus an image and a
video to check files with dimensions still sync. Together they cover
Cloudinary's image, video and raw resource types.

All files go up in one batch, and their state is read in one call, so
the test only takes about 15 seconds longer than the PDF-only one.

Wait for the auto-sync queue to push the files instead of syncing them
from the test, which raced with the queue and flagged files with
"Asset in sync loop." once several were uploaded.
For a file without dimensions, Delivery::create_delivery() got the
local path from wp_get_attachment_url(). In the admin, the plugin
filters that URL, which is how preparing the sync called back into
itself. If the attachment already had a Cloudinary ID in the request,
it also stored the Cloudinary URL as the local path. Use
Media::local_url() instead, as get_sized() already does for images.

The guard in Media::cloudinary_id() stays, for any other way a sync
preparation can reach it. Each guard now has its own PHPUnit test.

Also:
- Give the upload sync e2e test a 150s timeout, above its 90s sync
  poll, so a slow sync fails with the file that didn't sync rather
  than a generic test timeout.
- Drop the duplicate placeholder in cloudinary_id(), add the @uses
  tag to the upgrade cleanup, and fix a test comment.

@utkarshcloudinary utkarshcloudinary left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! ✅

Comment thread php/class-sync.php Outdated
*
* In 3.3.9, files without dimensions (PDF, audio, documents) got this error on upload, from a loop between
* preparing their sync and getting their URL. Now that the loop is fixed, they can sync without anyone clearing the
* error by hand. Only this exact error is cleared, and only when upgrading from 3.3.9: other sync errors, and

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The docblock says "only when upgrading from 3.3.9". Update it together with the version range.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 8b73930, along with the new run-once behaviour.

Comment thread php/class-sync.php Outdated
* @uses action:cloudinary_version_upgrade
*/
public function clear_delivery_loop_errors( $old_version ) {
if ( version_compare( $old_version, '3.3.9', '<' ) || version_compare( $old_version, '3.3.10', '>=' ) ) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: The cleanup only runs when the old version is exactly 3.3.9 (3.3.9 <= old < 3.3.10). The lower bound is right, as the loop error was added in 3.3.9. The hard-coded upper bound assumes the next release after 3.3.9 is the one that contains this fix.

This fix is not in the current release branch (uat, PR #1388). If any release without this fix ships after 3.3.9, a site that installs it before this fix reports that release as its old version, so the cleanup skips it and its files keep the error. That release still has the loop, so files uploaded on it get the same error, and those are not cleared either.

The same condition, with example versions:

upgrade from 3.3.8   -> skipped
upgrade from 3.3.9   -> cleared
upgrade from 3.3.10+ -> skipped

Suggestion: avoid tying this to a release number that isn't decided yet. For example, run the cleanup once for any upgrade from 3.3.9 or later, and record that it ran (a one-time option, or a step in Utils::get_upgrade_sequence() keyed on db_version). Loops flagged after the fix still stay visible.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. #1388 is a 3.4.0 release without this fix, so the upper bound was wrong. Fixed in 8b73930: the cleanup now runs once, on the first upgrade to a version with the fix. The version upgraded from is then always the last one without it, so it clears the error when that version is 3.3.9 or later, with no upper bound. An option (_cloudinary_delivery_loop_errors_cleared) records that it ran, so loops flagged after the fix stay visible, including on sites that installed a version with the fix. I used an option rather than a get_upgrade_sequence() step because those ranges are hard-coded versions too.

Comment thread php/class-sync.php Outdated
'Sync loop detected: the "delivery" sync state could not be saved.',
self::get_loop_error_message( 'delivery' ),
);
foreach ( get_available_languages() as $locale ) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: The plugin has no translations to match. WordPress.org has no language packs for it (api.wordpress.org/translations/plugins/1.0/ returns 0), the repo only ships languages/cloudinary.pot, and nothing calls load_plugin_textdomain(). So in practice the loop error is saved in English, and switching through every installed language finds nothing new.

The switching isn't free either. It runs in the request that fires cloudinary_version_upgrade (during init), which can be the first front-end visit after the update. In WP_Locale_Switcher, each switch_to_locale() and each restore_previous_locale() calls change_locale(), which:

  • reloads the core text domain for that locale (load_default_textdomain()),
  • unloads and reloads every other loaded text domain,
  • builds a new $wp_locale,
  • fires change_locale (plus switch_locale / restore_previous_locale).

That's two full locale changes per installed language other than the current one (switch_to_locale() returns false for the current locale), and every plugin hooked to those actions runs each time.

Suggestion: delete only the English string (plus the current locale's, if you want a safety net), and drop the locale switching.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, removed in 8b73930. It now clears the English message and the current locale's, with no locale switching.

*
* @return void
*/
public function test_the_nested_attachment_url_is_the_local_one() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warning: This test still passes with both fixes reverted. I copied the PR into the tests container and restored php/class-media.php and php/class-delivery.php from develop:

PR code:             OK (8 tests)
both fixes reverted: 4 failures, but test_the_nested_attachment_url_is_the_local_one passes (53 assertions)

The PDF has no public_id in this test, so no Cloudinary URL can show up on either side of the comparison. With one added line that sets it, the test fails on the reverted code and passes on the PR code:

$media->update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], 'sample' );
both fixes reverted, public_id set:
  Failed asserting that two strings are identical.
  -'https://res.cloudinary.com/test-cloud/images/v1/sample/sample.?_i=AA'
  +'http://localhost:8899/wp-content/uploads/2026/12/sample-28.pdf'
PR code, public_id set: OK

In that failure, $local_url (expected) is the Cloudinary URL and the nested URL (actual) is the local file. I haven't traced why local_url() returns the Cloudinary URL there, so it's worth checking what the test should assert.

Suggestion: set Sync::META_KEYS['public_id'] after creating the attachment, as the tests at lines 216 and 309 already do, and also assert that no nested URL contains res.cloudinary.com. The two guards are each covered elsewhere: with only the class-media.php change reverted, test_a_nested_call_does_not_prepare_the_sync_again fails; with only the class-delivery.php change reverted, test_delivery_saves_the_local_path_of_a_file_without_dimensions fails.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking this. Fixed in 8b73930: the test now sets the public ID and asserts that no nested URL contains res.cloudinary.com. It fails with both fixes reverted and passes on the PR code.

About local_url() returning the Cloudinary URL: that only happens in PHPUnit. Tests don't run as admin requests, so Delivery::ensure_relation (frontend only) is registered on wp_get_attachment_url as well. Inside local_url(), it runs get_sync_type(), which calls local_url() again, and the nested call resets in_downsize to false before the outer one reaches Media::attachment_url(). On a real site, the two filters are never registered together. The test now removes ensure_relation to match an admin request, and builds the expected URL from the upload dir instead of calling local_url(), as test_delivery_saves_the_local_path_of_a_file_without_dimensions does.

$sync = $this->get_sync();
$attachment_id = $this->create_errored_attachment( Sync::get_loop_error_message( 'delivery' ) );

foreach ( array( '3.3.8', '3.3.10', '3.4.0' ) as $old_version ) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: This test asserts that upgrades from 3.3.10 and 3.4.0 keep the error. Any release that ships after 3.3.9 without this fix has the same bug, so these versions may need to move into the "clears" test, depending on how the range in class-sync.php is changed.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 8b73930: 3.3.10 and 3.4.0 are now in the "clears" test, the "keeps" test covers 3.3.8 and a new install (2.0.1), and a new test checks the cleanup runs only once.

The cleanup only ran on upgrades from 3.3.9, which assumed the next
release would contain the fix. A release without it, such as 3.4.0 in
uat, has the same loop, so sites upgrading from it kept their errors.

Run it on the first upgrade to a version with the fix instead: the
version upgraded from is then the last one without it, so clear the
errors when it is 3.3.9 or later. Record that it ran, so later
upgrades keep genuine loop errors visible, including on sites that
installed a version with the fix.

Drop the switch through every installed language: the plugin ships no
translations, and each switch reloads every text domain. Clear the
English message and the current locale's.

Set a public ID in the nested attachment URL test, so a Cloudinary URL
could show up and the test fails without the fixes, and remove the
frontend-only filter that PHPUnit registers.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants