Skip to content

Commit

Permalink
make sure everything is cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cirospaciari committed Oct 16, 2024
1 parent dc4b0c2 commit 6a6d160
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 45 deletions.
39 changes: 26 additions & 13 deletions src/bun.js/api/bun/h2_frame_parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3882,17 +3882,15 @@ pub const H2FrameParser = struct {
}
return this;
}

pub fn deinit(this: *H2FrameParser) void {
log("deinit", .{});

defer {
if (ENABLE_ALLOCATOR_POOL) {
H2FrameParser.pool.?.put(this);
} else {
this.destroy();
}
}
pub fn detachFromJS(this: *H2FrameParser, _: *JSC.JSGlobalObject, _: *JSC.CallFrame) JSValue {
JSC.markBinding(@src());
this.detach(false);
return .undefined;
}
/// be careful when calling detach be sure that the socket is closed and the parser not accesible anymore
/// this function can be called multiple times
pub fn detach(this: *H2FrameParser, comptime finalizing: bool) void {
this.flushCorked();
this.detachNativeSocket();
this.strong_ctx.deinit();
this.handlers.deinit();
Expand All @@ -3909,9 +3907,24 @@ pub const H2FrameParser = struct {
}
var it = this.streams.valueIterator();
while (it.next()) |stream| {
stream.freeResources(this, true);
stream.freeResources(this, finalizing);
}
var streams = this.streams;
defer streams.deinit();
this.streams = bun.U32HashMap(Stream).init(bun.default_allocator);
}

pub fn deinit(this: *H2FrameParser) void {
log("deinit", .{});

defer {
if (ENABLE_ALLOCATOR_POOL) {
H2FrameParser.pool.?.put(this);
} else {
this.destroy();
}
}
this.streams.deinit();
this.detach(true);
}

pub fn finalize(
Expand Down
4 changes: 4 additions & 0 deletions src/bun.js/api/h2.classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export default [
fn: "flushFromJS",
length: 0,
},
detach: {
fn: "detachFromJS",
length: 0,
},
rstStream: {
fn: "rstStream",
length: 1,
Expand Down
65 changes: 33 additions & 32 deletions src/js/node/http2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2344,10 +2344,7 @@ class ServerHttp2Session extends Http2Session {
error(self: ServerHttp2Session, errorCode: number, lastStreamId: number, opaqueData: Buffer) {
if (!self) return;
const error_instance = sessionErrorFromCode(errorCode);
self.emit("error", error_instance);
self[bunHTTP2Socket]?.end();
self.#parser = null;
this[bunHTTP2Socket] = null;
self.destroy(error_instance);
},
wantTrailers(self: ServerHttp2Session, stream: ServerHttp2Stream) {
if (!self || typeof stream !== "object") return;
Expand All @@ -2368,16 +2365,12 @@ class ServerHttp2Session extends Http2Session {
if (errorCode !== 0) {
self.#parser.emitErrorToAllStreams(errorCode);
}

self[bunHTTP2Socket]?.end();
self.#parser = null;
this[bunHTTP2Socket] = null;
const error_instance = sessionErrorFromCode(errorCode);
self.destroy(error_instance);
},
end(self: ServerHttp2Session, errorCode: number, lastStreamId: number, opaqueData: Buffer) {
if (!self) return;
self[bunHTTP2Socket]?.end();
self.#parser = null;
this[bunHTTP2Socket] = null;
self.destroy();
},
write(self: ServerHttp2Session, buffer: Buffer) {
if (!self) return -1;
Expand All @@ -2395,14 +2388,16 @@ class ServerHttp2Session extends Http2Session {
}

#onClose() {
this[bunHTTP2Socket] = null;
this.#parser?.emitAbortToAllStreams();
this.#parser = null;
const parser = this.#parser;
if (parser) {
parser.emitAbortToAllStreams();
parser.detach();
this.#parser = null;
}
this.close();
}

#onError(error: Error) {
this[bunHTTP2Socket] = null;
this.destroy(error);
}

Expand Down Expand Up @@ -2609,8 +2604,12 @@ class ServerHttp2Session extends Http2Session {
this.goaway(code || constants.NGHTTP2_NO_ERROR, 0, Buffer.alloc(0));
socket.end();
}
this.#parser?.emitErrorToAllStreams(code || constants.NGHTTP2_NO_ERROR);
this.#parser = null;
const parser = this.#parser;
if (parser) {
parser.emitErrorToAllStreams(code || constants.NGHTTP2_NO_ERROR);
parser.detach();
this.#parser = null;
}
this[bunHTTP2Socket] = null;

if (error) {
Expand Down Expand Up @@ -2761,10 +2760,7 @@ class ClientHttp2Session extends Http2Session {
error(self: ClientHttp2Session, errorCode: number, lastStreamId: number, opaqueData: Buffer) {
if (!self) return;
const error_instance = sessionErrorFromCode(errorCode);
self.emit("error", error_instance);
self[bunHTTP2Socket]?.destroy();
self.#parser = null;
this[bunHTTP2Socket] = null;
self.destroy(error_instance);
},

wantTrailers(self: ClientHttp2Session, stream: ClientHttp2Stream) {
Expand All @@ -2784,15 +2780,12 @@ class ClientHttp2Session extends Http2Session {
if (errorCode !== 0) {
self.#parser.emitErrorToAllStreams(errorCode);
}
self[bunHTTP2Socket]?.end();
self.#parser = null;
this[bunHTTP2Socket] = null;
const error_instance = sessionErrorFromCode(errorCode);
self.destroy(error_instance);
},
end(self: ClientHttp2Session, errorCode: number, lastStreamId: number, opaqueData: Buffer) {
if (!self) return;
self[bunHTTP2Socket]?.end();
self.#parser = null;
this[bunHTTP2Socket] = null;
self.destroy();
},
write(self: ClientHttp2Session, buffer: Buffer) {
if (!self) return -1;
Expand Down Expand Up @@ -2846,10 +2839,14 @@ class ClientHttp2Session extends Http2Session {
}

#onClose() {
this[bunHTTP2Socket] = null;
this.#parser?.emitAbortToAllStreams();
this.#parser = null;
const parser = this.#parser;
if (parser) {
parser.emitAbortToAllStreams();
parser.detach();
this.#parser = null;
}
this.close();
this[bunHTTP2Socket] = null;
}
#onError(error: Error) {
this[bunHTTP2Socket] = null;
Expand Down Expand Up @@ -3069,9 +3066,13 @@ class ClientHttp2Session extends Http2Session {
this.goaway(code || constants.NGHTTP2_NO_ERROR, 0, Buffer.alloc(0));
socket.end();
}
this.#parser?.emitErrorToAllStreams(code || constants.NGHTTP2_NO_ERROR);
this[bunHTTP2Socket] = null;
const parser = this.#parser;
if (parser) {
parser.emitErrorToAllStreams(code || constants.NGHTTP2_NO_ERROR);
parser.detach();
}
this.#parser = null;
this[bunHTTP2Socket] = null;

if (error) {
this.emit("error", error);
Expand Down

0 comments on commit 6a6d160

Please sign in to comment.