Skip to content

Axios freezes or does not clean connection properly when is input stream destroyed or throws error #3780

@misos1

Description

@misos1

Describe the bug

  1. When the input stream is destroyed in the middle of sending data (premature close) then axios never ends sending request.
  2. When input stream emits error then axios will throw it as expected but application never ends probably because connection with server is never resolved (maybe leak).

To Reproduce

There are 3 variations which can be switched using the variation variable:

Variation 0: After one second stream sends some data and ends gracefully
Variation 1: After one second is stream destroyed
Variation 2: After one second is stream destroyed with error

let variation = 0;

let { PassThrough } = require("stream");
let http = require("http");
let axios = require("axios");

let server = http.createServer(async function(req, res)
{
	for await (let chunk of req);
	res.end("ok");
});

(async function()
{
	await new Promise(resolve => server.listen(resolve));
	let stream = new PassThrough();
	setTimeout(function()
	{
		if(variation == 0) stream.end("abc");
		if(variation == 1) stream.destroy();
		if(variation == 2) stream.destroy(new Error("abc"));
	}, 1000);
	await axios.post("http://127.0.0.1:" + server.address().port, stream)
		.then(res => console.log("received", res.data))
		.catch(err => console.log("error", err));
	console.log("request finished");
	server.close();
}());

Variation 0 proceeds normally:

received ok
request finished

Variation 1 never prints anything (axios is stuck in sending the request).

Variation 2 proceeds with error but application never terminates:

error Error: abc
...
request finished
(running forever)

Expected behavior

Variation 1 should print something like "premature close error" (ERR_STREAM_PREMATURE_CLOSE) as does require("stream").finished in such case (axios probably needs to listen on "close" event).
Variation 2 should gracefully end.

Environment

  • Axios Version 0.21.1
  • Adapter HTTP
  • Node.js Version v15.12.0

Additional context/Screenshots

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions