Skip to content

fix(plugin-cloud-storage): don't leave skipCloudStorage on a reused context - #17700

Closed
luantaraschi wants to merge 1 commit into
payloadcms:mainfrom
luantaraschi:fix/cloud-storage-context-leak
Closed

luantaraschi wants to merge 1 commit into
payloadcms:mainfrom
luantaraschi:fix/cloud-storage-context-leak

Conversation

@luantaraschi

@luantaraschi luantaraschi commented Aug 6, 2026 •

Copy link
Copy Markdown

Fixes #17546.

What

Passing one context object across several Local API create() calls on an upload collection uploads only the first file. Every later create succeeds, writes a fully-populated document (filename, mimeType, sizes, url), and puts nothing in storage, with no error and no log line.

@edrpls traced the mechanism precisely in the issue; this confirms it and fixes the last link.

The chain

  1. createLocalReq assigns the caller's context to req.context by reference when the fresh req has none. getRequestContext returns context unchanged in that branch.
  2. The hook sets req.context.skipCloudStorage = true, so it is now set on the caller's object.
  3. The nested payload.update({ ..., req }) runs createLocalReq again on that same req. req.context is non-empty now, so line 139 reassigns req.context = { ...req.context, ...context }, which is a new object.
  4. finally { delete req.context.skipCloudStorage } deletes it from that new object.
  5. The caller's object keeps the flag. The next create() binds it to req.context again, the hook returns early at its first line, and no upload happens.

Step 3 is the one that's easy to miss: the reassignment happens on the same req, so req.context silently stops pointing at what step 2 mutated.

The fix

Capture the reference the flag is set on, and clear it from that:

+ const contextWithFlag = req.context
+ contextWithFlag.skipCloudStorage = true
  ...
  } finally {
-   delete req.context.skipCloudStorage
+   delete contextWithFlag.skipCloudStorage
  }

The recursion guard still works, because the nested createLocalReq copies req.context including the flag, so the nested hook still sees it and returns early. Only the cleanup changes.

I deliberately did not touch getRequestContext. Handing the caller's object over by reference is what lets hooks write back into a caller's context, and changing that would be a much broader behavioural change than this bug warrants.

Verified by running it

Added uploads every file when one context object is reused across creates to test/plugin-cloud-storage/int.spec.ts. Three sequential creates sharing one context, then HeadObject against the bucket for every file and every generated size.

Run against LocalStack + MongoDB in Docker:

Result
With the fix Tests 1 passed
Reverting only afterChange.ts Tests 1 failed

The control failure is the reported damage, not a proxy for it:

Error verifying uploads: [ 'image-1-400x400.png', 'image-1-900x450.png', 'image-1.png' ] NotFound

The first file (image.png) verifies fine; the second one and its sizes are absent from the bucket while its document row exists. Reverting the finally line alone is enough to reproduce.

Full suite after the change: Test Files 2 passed · Tests 35 passed | 3 todo (38).

Note on the assertion order

The test checks the bucket before asserting that sharedContext is clean. I had it the other way round first, and the control then failed on the context assertion. That is technically correct, but it would have let a future regression report the internal symptom instead of the missing object. The bucket check is the one that describes what users lose.

What I did not verify

I ran the plugin-cloud-storage suite only, on MongoDB with the S3 adapter against LocalStack. I did not run the other storage-* suites (azure, gcs, r2, vercel-blob). The change is in the shared hook, so they exercise the same path, but I'm flagging it rather than implying I checked. Same for a real S3/R2 endpoint; the reporter covered R2.

Checklist

  • Tests added
  • Documentation, no API change

…ontext

The afterChange hook sets `skipCloudStorage` on `req.context`, then calls a
nested `payload.update()`. That runs `createLocalReq`, which reassigns
`req.context` to a fresh copy — so the `finally` cleared the flag off the copy
and left it on the original object.

When a caller passes one `context` object across several Local API creates,
`createLocalReq` binds that same object to `req.context`, so the leftover flag
makes every later upload return early. The documents are written with correct
filenames and sizes, but no bytes reach storage and nothing is logged.
@luantaraschi
luantaraschi requested a review from denolfe as a code owner August 6, 2026 22:49
@cbratschi

Copy link
Copy Markdown
Contributor

Thanks for adding an integration test that verifies the actual stored objects. This directly covers the silent data-loss behavior from #17546.

There is still one context reference left polluted after the fix. The nested payload.update({ req }) runs createLocalReq, which rebinds req.context to a shallow copy containing skipCloudStorage. Deleting the flag only from contextWithFlag cleans the caller-provided object, but leaves it on the current request context.

This can remain visible to afterOperation hooks and causes later operations that reuse the same req to skip cloud storage. PR #17558 handles both references:

delete contextWithFlag.skipCloudStorage

if (req.context !== contextWithFlag) {
  delete req.context.skipCloudStorage
}

It would be useful to combine that cleanup with this PR's integration test and add an assertion covering the rebound request context.

Separately, _payloadCloudStorage is never cleared and can retain the first upload's buffers on a shared context. That probably deserves a focused follow-up because its lifecycle is more involved than the recursion flag.

@luantaraschi

Copy link
Copy Markdown
Author

You are right about the rebound context, and #17558 got there first.

getRequestContext returns { ...req.context, ...context } when the context is non-empty, and createLocalReq assigns it back at line 139. So once the hook sets the flag the context stops being empty, the nested update rebinds req.context to a copy carrying it, and my finally only cleans the original. Exactly as you describe.

Reading #17558 properly: opened 29 July against my 6 August, same captured reference (hookContext where I wrote contextWithFlag), and it also clears the rebound one. My source change is a subset of it. I should have found it before opening this, and searching the open PRs touching that file would have shown it.

So I would rather not have two PRs racing on the same file. Close this one in favour of #17558 whenever it suits you.

The one thing here that #17558 does not have is the integration test. It drives three real payload.create calls through a shared context and asserts on the stored objects rather than on mocks, which is what makes the missing bytes visible. Happy for it to be picked up into #17558, or I can send it on its own after that lands, whichever you prefer.

On _payloadCloudStorage: I have not measured that one, so I will not claim anything about it here.

@cbratschi

Copy link
Copy Markdown
Contributor

Thanks for checking this so carefully. Agreed: #17558 should be the implementation and #17700 can close in its favour.

The integration test is worth carrying over before merge because it catches the actual missing-byte regression through real creates, which complements #17558’s unit coverage.

No need to make a claim about _payloadCloudStorage without a separate reproduction.

@luantaraschi

Copy link
Copy Markdown
Author

Closing in favour of #17558, which was open first and clears the flag off both the captured context and the rebound one, where this only did the first. Two PRs racing on the same file is not worth it.

The integration test here does not exist over there, so I have pointed at it from that PR. If it lands without it I will send it on its own.

Thanks @cbratschi for reading it that carefully.

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

Labels

None yet

Projects

None yet

2 participants