aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Ciro Spaciari <ciro.spaciari@gmail.com> 2023-04-10 19:05:16 -0300
committerGravatar GitHub <noreply@github.com> 2023-04-10 15:05:16 -0700
commitf91dc8c0d308f2b21a725aeccc58c3cfb78c1339 (patch)
tree9d143e807de20c78103fa54049da58a6c280b199
parentf4ab79d6bb5042b06b82731ad37a5fd956cf4826 (diff)
downloadbun-f91dc8c0d308f2b21a725aeccc58c3cfb78c1339.tar.gz
bun-f91dc8c0d308f2b21a725aeccc58c3cfb78c1339.tar.zst
bun-f91dc8c0d308f2b21a725aeccc58c3cfb78c1339.zip
always safe deinit socket context (#2611)
-rw-r--r--src/bun.js/api/bun/socket.zig12
-rw-r--r--src/deps/uws.zig20
2 files changed, 26 insertions, 6 deletions
diff --git a/src/bun.js/api/bun/socket.zig b/src/bun.js/api/bun/socket.zig
index 3fa022907..8af1d895b 100644
--- a/src/bun.js/api/bun/socket.zig
+++ b/src/bun.js/api/bun/socket.zig
@@ -694,7 +694,6 @@ pub const Listener = struct {
std.debug.assert(this.handlers.active_connections == 0);
if (this.socket_context) |ctx| {
- ctx.close(this.ssl);
ctx.deinit(this.ssl);
}
@@ -1049,14 +1048,17 @@ fn NewSocket(comptime ssl: bool) type {
pub fn markInactive(this: *This) void {
if (this.reffer.has) {
- var vm = this.handlers.vm;
- this.reffer.unref(vm);
-
// we have to close the socket before the socket context is closed
// otherwise we will get a segfault
// uSockets will defer closing the TCP socket until the next tick
- if (!this.socket.isClosed())
+ if (!this.socket.isClosed()) {
this.socket.close(0, null);
+ // onClose will call markInactive again
+ return;
+ }
+
+ var vm = this.handlers.vm;
+ this.reffer.unref(vm);
this.handlers.markInactive(ssl, this.socket.context());
this.poll_ref.unref(vm);
diff --git a/src/deps/uws.zig b/src/deps/uws.zig
index e87e3280f..2713f8760 100644
--- a/src/deps/uws.zig
+++ b/src/deps/uws.zig
@@ -394,13 +394,31 @@ pub const Timer = opaque {
return @ptrCast(*?Type, @alignCast(@alignOf(Type), us_timer_ext(this))).*.?;
}
};
+
pub const SocketContext = opaque {
pub fn getNativeHandle(this: *SocketContext, comptime ssl: bool) *anyopaque {
return us_socket_context_get_native_handle(comptime @as(i32, @boolToInt(ssl)), this).?;
}
+ fn _deinit_ssl(this: *SocketContext) void {
+ us_socket_context_free(@as(i32, 1), this);
+ }
+
+ fn _deinit(this: *SocketContext) void {
+ us_socket_context_free(@as(i32, 0), this);
+ }
+
+ /// closes and deinit the SocketContexts
pub fn deinit(this: *SocketContext, ssl: bool) void {
- us_socket_context_free(@as(i32, @boolToInt(ssl)), this);
+ this.close(ssl);
+ //always deinit in next iteration
+ if (Loop.get()) |loop| {
+ if (ssl) {
+ loop.nextTick(*SocketContext, this, SocketContext._deinit_ssl);
+ } else {
+ loop.nextTick(*SocketContext, this, SocketContext._deinit);
+ }
+ }
}
pub fn close(this: *SocketContext, ssl: bool) void {