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

[core-rest-pipeline] Fix race condition in request #17956

Merged
merged 5 commits into from
Sep 30, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions sdk/core/core-rest-pipeline/src/nodeHttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,9 @@ class NodeHttpClient implements HttpClient {
});

abortController.signal.addEventListener("abort", () => {
req.abort();
reject(new AbortError("The operation was aborted."));
const abortError = new AbortError("The operation was aborted.");
req.destroy(abortError);
reject(abortError);
});
if (body && isReadableStream(body)) {
body.pipe(req);
Expand All @@ -229,7 +230,7 @@ class NodeHttpClient implements HttpClient {
req.end(ArrayBuffer.isView(body) ? Buffer.from(body.buffer) : Buffer.from(body));
} else {
logger.error("Unrecognized body type", body);
throw new RestError("Unrecognized body type");
reject(new RestError("Unrecognized body type"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, we used to run into this in event hubs/service bus a lot too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one shouldn't actually hurt us because throwing from inside the promise constructor (when sync) is handled correctly, but it didn't look as nice.

}
} else {
// streams don't like "undefined" being passed as data
Expand Down Expand Up @@ -313,11 +314,15 @@ function streamToText(stream: NodeJS.ReadableStream): Promise<string> {
resolve(Buffer.concat(buffer).toString("utf8"));
});
stream.on("error", (e) => {
reject(
new RestError(`Error reading response as text: ${e.message}`, {
code: RestError.PARSE_ERROR
})
);
if (e && e?.name === "AbortError") {
reject(e);
} else {
reject(
new RestError(`Error reading response as text: ${e.message}`, {
code: RestError.PARSE_ERROR
})
);
}
});
});
}
Expand Down
44 changes: 37 additions & 7 deletions sdk/core/core-rest-pipeline/test/node/nodeHttpClient.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ class FakeResponse extends PassThrough {
public headers?: IncomingHttpHeaders;
}

class FakeRequest extends PassThrough {
public finished?: boolean;
public abort(): void {
this.finished = true;
}
}
class FakeRequest extends PassThrough {}

/**
* Generic NodeJS streams accept typed arrays just fine,
Expand Down Expand Up @@ -53,7 +48,6 @@ function createResponse(statusCode: number, body = ""): IncomingMessage {

function createRequest(): ClientRequest {
const request = new FakeRequest();
request.finished = false;
return (request as unknown) as ClientRequest;
}

Expand Down Expand Up @@ -334,4 +328,40 @@ describe("NodeHttpClient", function() {
const response = await promise;
assert.strictEqual(response.status, 200);
});

it("should return an AbortError when aborted while reading the HTTP response", async function() {
clock.restore();
const client = createDefaultHttpClient();
const controller = new AbortController();

const clientRequest = createRequest();
clientRequest.destroy = function(this: FakeRequest, e: Error) {
// give it some time to attach listeners and read from the stream
setTimeout(() => {
streamResponse.destroy(e);
}, 100);
};
stubbedHttpsRequest.returns(clientRequest);
const request = createPipelineRequest({
url: "https://example.com",
abortSignal: controller.signal
});
const promise = client.sendRequest(request);

const streamResponse = new FakeResponse();
streamResponse.headers = {};
streamResponse.statusCode = 200;
const buffer = Buffer.from("The start of an HTTP body");
streamResponse.write(buffer);
stubbedHttpsRequest.yield(streamResponse);
controller.abort();

try {
await promise;
assert.fail("Expected await to throw");
} catch (e) {
console.log(e);
assert.strictEqual(e.name, "AbortError");
}
});
});