-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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; | ||
} | ||
|
||
|
@@ -334,4 +328,41 @@ 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(); | ||
stubbedHttpsRequest.returns(clientRequest); | ||
const request = createPipelineRequest({ | ||
url: "https://example.com", | ||
abortSignal: controller.signal | ||
}); | ||
const promise = client.sendRequest(request); | ||
|
||
const streamResponse = new FakeResponse(); | ||
|
||
clientRequest.destroy = function(this: FakeRequest, e: Error) { | ||
// give it some time to attach listeners and read from the stream | ||
setTimeout(() => { | ||
streamResponse.destroy(e); | ||
}, 100); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does a value of 0 accomplish the same thing? Having something that's non-zero just makes me a bit nervous because that's led to flaky tests in the past. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, 0 works. I'll go ahead and update it. The reason this is here is because Node will throw if you emit an error before any listeners are attached |
||
}; | ||
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"); | ||
} | ||
}); | ||
}); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.