A websocket implementation for zig
0

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #78 from tadejg/fix-tls-client-regression-closing-crashes

Fix client regression: Calling client.close() on a TLS connection while readLoop is running results in a crash

authored by

Karl Seguin and committed by
GitHub
(Sep 1, 2025, 10:46 AM +0800) 5cfef4a7 59aaa71e

+12 -3
+12 -3
src/client/client.zig
··· 405 405 } 406 406 407 407 pub fn close(self: *Stream) void { 408 + const fd = self.stream.handle; 409 + const builtin = @import("builtin"); 410 + const native_os = builtin.os.tag; 411 + 408 412 if (self.tls_client) |tls_client| { 413 + // Shutdown the socket first, so readLoop() can exit, before tls_client's buffers are freed 414 + if (native_os == .windows) { 415 + _ = std.os.windows.ws2_32.shutdown(fd, std.os.windows.ws2_32.SD_BOTH); 416 + } else if (native_os == .wasi and !builtin.link_libc) { 417 + _ = std.os.wasi.sock_shutdown(fd, .{ .WR = true, .RD = true }); 418 + } else { 419 + std.posix.shutdown(fd, .both) catch {}; 420 + } 409 421 tls_client.deinit(); 410 422 } 411 423 ··· 415 427 // 416 428 // we don't want to crash on double close 417 429 418 - const fd = self.stream.handle; 419 - const builtin = @import("builtin"); 420 - const native_os = builtin.os.tag; 421 430 if (native_os == .windows) { 422 431 return std.os.windows.CloseHandle(fd); 423 432 }