Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

etag middleware causes request to hang with Miniflare >=2.8 #520

Closed
Cherry opened this issue Sep 12, 2022 · 1 comment
Closed

etag middleware causes request to hang with Miniflare >=2.8 #520

Cherry opened this issue Sep 12, 2022 · 1 comment

Comments

@Cherry
Copy link
Contributor

Cherry commented Sep 12, 2022

With the following code, requests will seemingly hang for ~5s when requesting an asset from KV.

// src/index.ts
import {Hono} from 'hono'
import {etag} from 'hono/etag'
import {getAssetFromKV, NotFoundError} from '@cloudflare/kv-asset-handler';

import manifestJSON from '__STATIC_CONTENT_MANIFEST';

const assetManifest = JSON.parse(manifestJSON);

async function assetMiddleware(context, next){
	try{
		const asset = await getAssetFromKV({
			request: context.req,
			waitUntil(promise){
				return context.executionCtx.waitUntil(promise);
			},
		}, {
			ASSET_NAMESPACE: context.env.__STATIC_CONTENT,
			ASSET_MANIFEST: assetManifest,
		});
		return asset;
	}catch(error){
		if(error instanceof NotFoundError){
			context.res = new Response('404', {status: 404});
			return;
		}
		if(context.env.NODE_ENV !== 'production'){
			throw error;
		}
		context.res = new Response('error', {status: 500});
	}
}


const app = new Hono()
app.use('*', etag());
app.get('/', (c) => c.text('Hello! Hono!'))

app.use('*', assetMiddleware);

export default app

A full reproduction can be found at https://github.com/Cherry/hono-miniflare-slowdown including reproduction steps.

It's possible this might be caused by cloudflare/miniflare@5ab7cb3.

@Cherry Cherry changed the title etag middleware causes request to hang with Miniflare >2.8 etag middleware causes request to hang with Miniflare >=2.8 Sep 12, 2022
@Cherry
Copy link
Contributor Author

Cherry commented Sep 12, 2022

After some debugging (thanks @mrbbot), it seems like this is due to a miniflare update populating the asset manifest now for kv-asset-handler, which results in this code chain being executed: cloudflare/kv-asset-handler#295, and then the content-length ends up as undefined, and then NaN.

image

Unfortunately that PR hasn't landed in a release yet, but the core fix here would be in kv-asset-handler. I wonder if there's something miniflare or hono could do to hot-patch this temporarily, since it might be a while until we see a release there. 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant