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

fix(http2) fix aborted behavior #11226

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/bun.js/api/bun/h2_frame_parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1726,7 +1726,7 @@ pub const H2FrameParser = struct {
if (stream.signal) |_signal| {
return JSC.JSValue.jsBoolean(_signal.aborted());
}
return JSC.JSValue.jsBoolean(true);
return JSC.JSValue.jsBoolean(stream.rstCode == @intFromEnum(ErrorCode.CANCEL));
}
pub fn getStreamState(this: *H2FrameParser, globalObject: *JSC.JSGlobalObject, callframe: *JSC.CallFrame) callconv(.C) JSValue {
JSC.markBinding(@src());
Expand Down
41 changes: 22 additions & 19 deletions src/js/node/http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,31 +654,33 @@ function emitStreamNT(self, streams, streamId) {

function emitStreamErrorNT(self, streams, streamId, error, destroy) {
const stream = streams.get(streamId);
const error_instance = streamErrorFromCode(error);
if (stream) {
if (!stream[bunHTTP2Closed]) {
stream[bunHTTP2Closed] = true;
}
stream.rstCode = error;
stream.emit("error", error_instance);
if (destroy) stream.destroy(error_instance);
if (constants.NGHTTP2_CANCEL != error && !stream.aborted) {
const error_instance = streamErrorFromCode(error);
stream.emit("error", error_instance);
if (destroy) stream.destroy(error_instance, error);
} else {
if (destroy) stream.destroy();
}
}
self.emit("streamError", error_instance);
}

function emitAbortedNT(self, streams, streamId, error) {
const stream = streams.get(streamId);
const error_instance = streamErrorFromCode(constants.NGHTTP2_CANCEL);
if (stream) {
if (!stream[bunHTTP2Closed]) {
stream[bunHTTP2Closed] = true;
}

stream.rstCode = constants.NGHTTP2_CANCEL;
stream.emit("aborted", error);
stream.emit("error", error_instance);
stream.emit("aborted");
stream.destroy();
stream.emit("end");
stream.emit("close");
}
self.emit("streamError", error_instance);
}
class ClientHttp2Session extends Http2Session {
/// close indicates that we called closed
Expand Down Expand Up @@ -720,13 +722,14 @@ class ClientHttp2Session extends Http2Session {
if (!self) return;
var stream = self.#streams.get(streamId);
if (stream) {
const error_instance = streamErrorFromCode(error);
if (!stream[bunHTTP2Closed]) {
stream[bunHTTP2Closed] = true;
}
stream.rstCode = error;
stream.emit("error", error_instance);
self.emit("streamError", error_instance);
if (error !== constants.NGHTTP2_CANCEL && !stream.aborted) {
stream.rstCode = error;
const error_instance = streamErrorFromCode(error);
stream.emit("error", error_instance);
}
} else {
process.nextTick(emitStreamErrorNT, self, self.#streams, streamId, error);
}
Expand Down Expand Up @@ -834,15 +837,15 @@ class ClientHttp2Session extends Http2Session {
if (!self) return;
var stream = self.#streams.get(streamId);
if (stream) {
const error_instance = streamErrorFromCode(constants.NGHTTP2_CANCEL);
if (!stream[bunHTTP2Closed]) {
stream[bunHTTP2Closed] = true;
}

stream.rstCode = constants.NGHTTP2_CANCEL;
stream.emit("aborted", error);
stream.emit("error", error_instance);
self.emit("streamError", error_instance);
stream.emit("aborted");
Copy link
Collaborator

Choose a reason for hiding this comment

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

i don't think this is right

i think it should be done inside .._destroy

stream.destroy();
stream.emit("end");
stream.emit("close");
} else {
process.nextTick(emitAbortedNT, self, self.#streams, streamId, error);
}
Expand All @@ -863,7 +866,7 @@ class ClientHttp2Session extends Http2Session {
if (errorCode !== 0) {
for (let [_, stream] of self.#streams) {
stream.rstCode = errorCode;
stream.destroy(sessionErrorFromCode(errorCode));
stream.destroy(sessionErrorFromCode(errorCode), errorCode);
}
}
self[bunHTTP2Socket]?.end();
Expand Down Expand Up @@ -1155,7 +1158,7 @@ class ClientHttp2Session extends Http2Session {
this[bunHTTP2Socket] = null;
// this should not be needed since RST + GOAWAY should be sent
for (let [_, stream] of this.#streams) {
if (error) {
if (error && code != constants.NGHTTP2_CANCEL) {
stream.emit("error", error);
}
stream.destroy();
Expand Down
22 changes: 10 additions & 12 deletions test/js/node/http2/node-http2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function doHttp2Request(url, headers, payload, options, request_options) {
data += chunk;
});
req.on("error", reject);
req.on("aborted", reject);
req.on("end", () => {
resolve({ data, headers: response_headers });
client.close();
Expand Down Expand Up @@ -636,8 +637,8 @@ describe("Client Basics", () => {
await promise;
expect("unreachable").toBe(true);
} catch (err) {
expect(err.code).toBe("ERR_HTTP2_STREAM_ERROR");
expect(err.message).toBe("Stream closed with error code 8");
// aborted event should not pass any arguments
expect(err).toBeUndefined();
}
});
it("aborted event should work with abortController", async () => {
Expand All @@ -647,16 +648,16 @@ describe("Client Basics", () => {
client.on("error", reject);
const req = client.request({ ":path": "/" }, { signal: abortController.signal });
req.on("aborted", resolve);
// error should not be called when aborted
req.on("error", reject);
req.on("end", () => {
resolve();
client.close();
});
abortController.abort();
const result = await promise;
expect(result).toBeDefined();
expect(result.name).toBe("AbortError");
expect(result.message).toBe("The operation was aborted.");
expect(result.code).toBe(20);
// https://nodejs.org/api/http2.html#event-aborted
// aborted event will not pass any arguments
await promise;
expect(req.aborted).toBeTrue();
expect(req.rstCode).toBe(8);
});
Expand All @@ -666,15 +667,12 @@ describe("Client Basics", () => {
client.on("error", reject);
const req = client.request({ ":path": "/" }, { signal: AbortSignal.abort() });
req.on("aborted", resolve);
req.on("error", reject);
req.on("end", () => {
resolve();
client.close();
});
const result = await promise;
expect(result).toBeDefined();
expect(result.name).toBe("AbortError");
expect(result.message).toBe("The operation was aborted.");
expect(result.code).toBe(20);
await promise;
expect(req.rstCode).toBe(8);
expect(req.aborted).toBeTrue();
});
Expand Down
Loading