aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-12-29 02:32:57 -0800
committerGravatar Jarred Sumner <jarred@jarredsumner.com> 2021-12-29 02:32:57 -0800
commitfe17d51b03cf3fcbbf50621afbb47d56eaeaa5c6 (patch)
treefae310415b0d334823be70c59a851b7f90bf9ecb
parent565cf23d9263ea153b8cb7d6f67a6c534b40ceeb (diff)
downloadbun-fe17d51b03cf3fcbbf50621afbb47d56eaeaa5c6.tar.gz
bun-fe17d51b03cf3fcbbf50621afbb47d56eaeaa5c6.tar.zst
bun-fe17d51b03cf3fcbbf50621afbb47d56eaeaa5c6.zip
FIx bug in http client
-rw-r--r--src/http_client_async.zig15
-rw-r--r--src/install/install.zig26
-rw-r--r--src/thread_pool.zig4
3 files changed, 31 insertions, 14 deletions
diff --git a/src/http_client_async.zig b/src/http_client_async.zig
index 57b24e841..9415f7767 100644
--- a/src/http_client_async.zig
+++ b/src/http_client_async.zig
@@ -385,9 +385,9 @@ pub const AsyncHTTP = struct {
var timer = std.time.Timer.start() catch @panic("Timer failure");
defer this.elapsed = timer.read();
_ = active_requests_count.fetchAdd(1, .Monotonic);
- defer _ = active_requests_count.fetchSub(1, .Monotonic);
this.response = await this.client.sendAsync(this.request_body.list.items, this.response_buffer) catch |err| {
+ _ = active_requests_count.fetchSub(1, .Monotonic);
this.state.store(.fail, .Monotonic);
this.err = err;
@@ -402,6 +402,7 @@ pub const AsyncHTTP = struct {
this.redirect_count = @intCast(u32, @maximum(127 - this.client.remaining_redirect_count, 0));
this.state.store(.success, .Monotonic);
this.gzip_elapsed = this.client.gzip_elapsed;
+ _ = active_requests_count.fetchSub(1, .Monotonic);
}
if (sender.http.callback) |callback| {
@@ -536,7 +537,7 @@ pub const AsyncMessage = struct {
pub fn release(self: *AsyncMessage) void {
self.used = 0;
self.sent = 0;
- std.debug.assert(!self.released);
+ if (self.released) return;
self.released = true;
if (self.pooled != null) {
@@ -1462,16 +1463,6 @@ pub fn connect(
client.setReadBufferSize(BufferPool.len) catch {};
client.setQuickACK(true) catch {};
- if (comptime Environment.isMac) {
- // Don't crash if the server disconnects.
- std.os.setsockopt(
- client.socket.fd,
- std.os.IPPROTO_TCP,
- std.os.SO_NOSIGPIPE,
- std.mem.asBytes(&@as(u32, @boolToInt(true))),
- ) catch {};
- }
-
this.tcp_client = client;
if (this.timeout > 0) {
client.setReadTimeout(@truncate(u32, this.timeout / std.time.ns_per_ms)) catch {};
diff --git a/src/install/install.zig b/src/install/install.zig
index cb449b2bb..3f88f2e62 100644
--- a/src/install/install.zig
+++ b/src/install/install.zig
@@ -4846,6 +4846,32 @@ pub const PackageManager = struct {
// this.enable.deduplicate_packages = false;
// }
+ if (env_loader.map.get("BUN_CONFIG_MAX_HTTP_REQUESTS")) |max_http_requests| {
+ load: {
+ AsyncHTTP.max_simultaneous_requests = std.fmt.parseInt(u16, max_http_requests, 10) catch |err| {
+ log.addErrorFmt(
+ null,
+ logger.Loc.Empty,
+ allocator,
+ "BUN_CONFIG_MAX_HTTP_REQUESTS value \"{s}\" is not a valid integer between 1 and 65535",
+ .{max_http_requests},
+ ) catch unreachable;
+ break :load;
+ };
+
+ if (AsyncHTTP.max_simultaneous_requests == 0) {
+ log.addWarningFmt(
+ null,
+ logger.Loc.Empty,
+ allocator,
+ "BUN_CONFIG_MAX_HTTP_REQUESTS value must be a number between 1 and 65535",
+ .{},
+ ) catch unreachable;
+ AsyncHTTP.max_simultaneous_requests = 255;
+ }
+ }
+ }
+
this.do.save_lockfile = strings.eqlComptime((env_loader.map.get("BUN_CONFIG_SKIP_SAVE_LOCKFILE") orelse "0"), "0");
this.do.load_lockfile = strings.eqlComptime((env_loader.map.get("BUN_CONFIG_SKIP_LOAD_LOCKFILE") orelse "0"), "0");
this.do.install_packages = strings.eqlComptime((env_loader.map.get("BUN_CONFIG_SKIP_INSTALL_PACKAGES") orelse "0"), "0");
diff --git a/src/thread_pool.zig b/src/thread_pool.zig
index 4df199006..1b39f2606 100644
--- a/src/thread_pool.zig
+++ b/src/thread_pool.zig
@@ -248,8 +248,8 @@ noinline fn wait(self: *ThreadPool, _is_waking: bool) error{Shutdown}!bool {
} else {
if (self.io) |io| {
const HTTP = @import("http");
- io.run_for_ns(std.time.ns_per_us * 100) catch {};
- while (HTTP.AsyncHTTP.active_requests_count.load(.Monotonic) > 255) {
+ io.run_for_ns(std.time.ns_per_us * 10) catch {};
+ while (HTTP.AsyncHTTP.active_requests_count.load(.Monotonic) > HTTP.AsyncHTTP.max_simultaneous_requests) {
io.tick() catch {};
}
sync = @bitCast(Sync, self.sync.load(.Monotonic));