-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
http/server: ERR_CONNECTION_RESET when responding without request body consumed #21209
Comments
Hi @jespertheend, this is likely because the async handler(req: Request): Promise<Response> {
// Consumes `req.body` without really using it.
await req.body?.cancel();
return new Response("OK"):
} |
Hmm ok that makes sense. |
I’ll test this out on my machine and see how I go. Also, the |
Yes, I'm assuming this happens once a file reaches a certain size, because I tried it with a smaller file of a couple kb and I didn't see this issue. |
Seems like using |
Hi @jespertheend, are you still encountering this issue? If so, what's the minimal reproducible snippet? |
Yes I can still reproduce with the snippet from the first comment. I can reproduce it on both macOS and Ubuntu. It seems a bit flaky though, about 1 in ten times it works fine, but the other 90% it fails with ERR_CONNECTION_RESET |
Frankly, I don't think this is an issue with the server. Instead, I think the code snippet is flawed due to its hackiness. If you're trying to simply make a POST request, you can do so with just HTML. A tutorial on how to do so can be found here. |
That example is missing If you wish to achieve this using only html, you can use this reduced test case: import { serve } from "https://deno.land/std@0.165.0/http/server.ts";
serve(async (req: Request) => {
const url = new URL(req.url);
if (url.pathname == "/") {
return new Response(
`
<!DOCTYPE html>
<html>
<body>
<form action="/post" method="post" enctype="multipart/form-data">
<input type="file" id="file" name="file">
<input type="submit">
</form>
</body>
</html>`,
{
headers: {
"content-type": "text/html",
},
},
);
} else if (url.pathname == "/post") {
console.log("/post received");
// Uncomment this to fix the issue:
// const buffer = await req.arrayBuffer();
return new Response("OK");
}
return new Response("Not found", {
status: 404,
});
}); In this case the issue is arguably worse, because now the user gets redirected to an empty page with a full screen error, rather than only an error being printed in the console. |
Hi @jespertheend, it's been a year since the last update. Is this still an issue for you? |
I can still reproduce this but with New code snippet
async function post() {
const fileInput = document.getElementById("fileInput") as HTMLInputElement;
if (!fileInput.files || fileInput.files.length != 1) {
alert("Please select a file");
return;
}
const file = fileInput.files[0];
const response = await fetch("/post", {
method: "POST",
headers: {
"content-type": "application/zip"
},
body: file,
});
alert(await response.text());
}
Deno.serve(async (req: Request) => {
const url = new URL(req.url);
if (url.pathname == "/") {
return new Response(`
<!DOCTYPE html>
<html>
<body>
<script>
${post.toString()}
</script>
<input type="file" id="fileInput" />
<button onclick="post()">POST</button>
</body>
</html>`, {
headers: {
"content-type": "text/html"
}
})
} else if (url.pathname == "/post") {
console.log("/post received");
// Uncomment this to fix the issue:
// const buffer = await req.arrayBuffer();
return new Response("OK");
}
return new Response("Not found", {
status: 404,
})
}) But it's not a big issue for me so I'd be fine with closing it as well. |
@bartlomieju, are you able to transfer this to the runtime repo? |
curl client also shows the error like the below (though the response itself looks returned correctly): $ curl -X POST localhost:8000/post -d @$HOME/Downloads/dist.zip
curl: (55) Send failure: Broken pipe
OK I updated the original example to the updated one. |
I am going to classify this is as "working as intended". The server is closing the connection after the response body has been entirely sent, to prevent resource leaks. You did not read the entire request body in the server, which means that the client may not have sent the entire request body and thus is still trying to write it. While the client is doing this, the server closed the connection because the request is done. The client notices this and errors out because it was unable to fully send it's request body. |
Describe the bug
Steps to Reproduce
main.ts
:deno run --allow-net main.ts
http://localhost:8000/
Expected behavior
The request responds with 'ok'
Actual behavior
The request fails with ERR_CONNECTION_RESET.
Note that the deno console doesn't show any errors and the
console.log("/post received");
line is being run.If you uncomment the
await req.arrayBuffer()
line, the issue is fixed.Environment
1.38.1
The text was updated successfully, but these errors were encountered: