diff options
author | 2023-06-27 19:20:50 -0400 | |
---|---|---|
committer | 2023-06-29 23:40:28 -0400 | |
commit | 9e2e3732127ea77803e87cc8768c9040d1717ca7 (patch) | |
tree | 860ef38884bbfaff72680cb8d0c24ca39e9d3409 /src/bun.js/api/server.zig | |
parent | 4b27aaaac78de307f65a2e3359a0598363c25ad1 (diff) | |
download | bun-9e2e3732127ea77803e87cc8768c9040d1717ca7.tar.gz bun-9e2e3732127ea77803e87cc8768c9040d1717ca7.tar.zst bun-9e2e3732127ea77803e87cc8768c9040d1717ca7.zip |
free request context on abort
Diffstat (limited to 'src/bun.js/api/server.zig')
-rw-r--r-- | src/bun.js/api/server.zig | 99 |
1 files changed, 53 insertions, 46 deletions
diff --git a/src/bun.js/api/server.zig b/src/bun.js/api/server.zig index 7d2a5641d..e35759245 100644 --- a/src/bun.js/api/server.zig +++ b/src/bun.js/api/server.zig @@ -1432,9 +1432,12 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp _ = signal.unref(); } - //if have sink, call onAborted on sink + // if have sink, call onAborted on sink + // this request was already finalized if (this.sink) |wrapper| { wrapper.sink.abort(); + this.markComplete(); + this.deinit(); return; } @@ -1443,46 +1446,47 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp this.finalizeWithoutDeinit(); this.markComplete(); this.deinit(); - } else { - this.pending_promises_for_abort = 0; - - // if we cannot, we have to reject pending promises - // first, we reject the request body promise - if (this.request_body) |body| { - // User called .blob(), .json(), text(), or .arrayBuffer() on the Request object - // but we received nothing or the connection was aborted - - if (body.value == .Locked) { - // the promise is pending - if (body.value.Locked.action != .none or body.value.Locked.promise != null) { - this.pending_promises_for_abort += 1; - } else if (body.value.Locked.readable != null) { - body.value.Locked.readable.?.abort(this.server.globalThis); - body.value.Locked.readable = null; - } - body.value.toErrorInstance(JSC.toTypeError(.ABORT_ERR, "Request aborted", .{}, this.server.globalThis), this.server.globalThis); + return; + } + + this.pending_promises_for_abort = 0; + + // if we cannot, we have to reject pending promises + // first, we reject the request body promise + if (this.request_body) |body| { + // User called .blob(), .json(), text(), or .arrayBuffer() on the Request object + // but we received nothing or the connection was aborted + + if (body.value == .Locked) { + // the promise is pending + if (body.value.Locked.action != .none or body.value.Locked.promise != null) { + this.pending_promises_for_abort += 1; + } else if (body.value.Locked.readable != null) { + body.value.Locked.readable.?.abort(this.server.globalThis); + body.value.Locked.readable = null; } + body.value.toErrorInstance(JSC.toTypeError(.ABORT_ERR, "Request aborted", .{}, this.server.globalThis), this.server.globalThis); } + } - if (this.response_ptr) |response| { - if (response.body.value == .Locked) { - if (response.body.value.Locked.readable) |*readable| { - response.body.value.Locked.readable = null; - readable.abort(this.server.globalThis); - } + if (this.response_ptr) |response| { + if (response.body.value == .Locked) { + if (response.body.value.Locked.readable) |*readable| { + response.body.value.Locked.readable = null; + readable.abort(this.server.globalThis); } } + } - // then, we reject the response promise - if (this.promise) |promise| { - this.pending_promises_for_abort += 1; - this.promise = null; - promise.asAnyPromise().?.reject(this.server.globalThis, JSC.toTypeError(.ABORT_ERR, "Request aborted", .{}, this.server.globalThis)); - } + // then, we reject the response promise + if (this.promise) |promise| { + this.pending_promises_for_abort += 1; + this.promise = null; + promise.asAnyPromise().?.reject(this.server.globalThis, JSC.toTypeError(.ABORT_ERR, "Request aborted", .{}, this.server.globalThis)); + } - if (this.pending_promises_for_abort > 0) { - this.server.vm.tick(); - } + if (this.pending_promises_for_abort > 0) { + this.server.vm.tick(); } } @@ -2089,18 +2093,21 @@ fn NewRequestContext(comptime ssl_enabled: bool, comptime debug_mode: bool, comp stream.value.ensureStillAlive(); - const is_in_progress = response_stream.sink.has_backpressure or !(response_stream.sink.wrote == 0 and - response_stream.sink.buffer.len == 0); - - if (!stream.isLocked(this.server.globalThis) and !is_in_progress) { - if (JSC.WebCore.ReadableStream.fromJS(stream.value, this.server.globalThis)) |comparator| { - if (std.meta.activeTag(comparator.ptr) == std.meta.activeTag(stream.ptr)) { - streamLog("is not locked", .{}); - this.renderMissing(); - return; - } - } - } + // This is commented out for now because new ReadableStream({ type: 'direct' }) + // that doesn't write immediately will cause the request to exit early. + + // const is_in_progress = response_stream.sink.has_backpressure or !(response_stream.sink.wrote == 0 and + // response_stream.sink.buffer.len == 0); + + // if (!stream.isLocked(this.server.globalThis) and !is_in_progress) { + // if (JSC.WebCore.ReadableStream.fromJS(stream.value, this.server.globalThis)) |comparator| { + // if (std.meta.activeTag(comparator.ptr) == std.meta.activeTag(stream.ptr)) { + // streamLog("is not locked", .{}); + // this.renderMissing(); + // return; + // } + // } + // } this.setAbortHandler(); streamLog("is in progress, but did not return a Promise. Finalizing request context", .{}); |