fix: correct manifest decoding logic #202
Merged
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.
Refactor manifest decoding to store intermediate JSON data.
What is this change
It appears that sometimes we encounter the following error:
This is specific to a Cloudflare environment and would most likely not happen on a local one. The reason is that the
await result.json()
is done through the function call (most likely). It is safer to store it in a variable and then passing it so that cloudflare can close that handle directly, avoiding this issue. Reference in cloudflare doc:https://developers.cloudflare.com/workers/observability/errors/#cannot-perform-io-on-behalf-of-a-different-request
This is theoretical so I don't promise that it will solve our issues, but it's worth a try.
I asked Claude about this and this is the answer:
In JavaScript/TypeScript, awaiting a Promise immediately versus passing the Promise to another function that awaits it doesn't fundamentally change the variable lifespan. However, in the context of Cloudflare Workers and I/O operations, the timing of when you await and resolve I/O-bound objects is crucial.
Let's compare:
The key differences are:
fetch()
and its associated I/O context are being passed intodecodeManifest
While both might work in regular JavaScript environments, in Cloudflare Workers, it's better to resolve I/O operations as early as possible to ensure the I/O context is properly closed and doesn't leak into other functions or get stored in ways that might cross request boundaries.
Think of it like this:
fetch()
is like an open file handleresult.json()
is like reading and closing that fileSo while the variable lifespan itself doesn't change, the timing of when you resolve the I/O context does matter in the Workers environment.