Skip to content

Instantly share code, notes, and snippets.

@andrewrk
Created July 1, 2024 21:23
Show Gist options
  • Save andrewrk/bf46cff35d34692558563f75bc8d0e62 to your computer and use it in GitHub Desktop.
Save andrewrk/bf46cff35d34692558563f75bc8d0e62 to your computer and use it in GitHub Desktop.
connect() hanging even after shutdown() is called
const std = @import("std");
pub fn main() !void {
const addr = try std.net.Address.initUnix("/home/andy/dev/zig/.zig-cache/tmp/b8f2350f14c91bbf");
std.log.debug("socket", .{});
const sockfd = try std.posix.socket(
std.posix.AF.UNIX,
std.posix.SOCK.STREAM | std.posix.SOCK.CLOEXEC,
0,
);
const thread = try std.Thread.spawn(.{}, run, .{sockfd});
defer thread.join();
std.log.debug("connect", .{});
try std.posix.connect(sockfd, &addr.any, addr.getOsSockLen());
std.log.debug("read", .{});
var trash_buf: [1]u8 = undefined;
const n = try std.posix.read(sockfd, &trash_buf);
std.log.debug("read {d} byte: {d}", .{ n, trash_buf[0] });
}
fn run(sockfd: std.posix.fd_t) !void {
std.log.debug("thread sleep", .{});
std.time.sleep(std.time.ns_per_ms * 500);
std.log.debug("thread shutdown", .{});
try std.posix.shutdown(sockfd, .both);
std.log.debug("thread exit", .{});
}
$ uname -a
Linux bark 6.9.7 #1-NixOS SMP PREEMPT_DYNAMIC Thu Jun 27 11:52:32 UTC 2024 x86_64 GNU/Linux
$ ./test
debug: socket
debug: connect
debug: thread sleep
debug: thread shutdown
debug: thread exit
^C
$ strace -f ./test
execve("./test", ["./test"], 0x7fffb9984958 /* 163 vars */) = 0
arch_prctl(ARCH_SET_FS, 0x10e4010) = 0
prlimit64(0, RLIMIT_STACK, NULL, {rlim_cur=8192*1024, rlim_max=RLIM64_INFINITY}) = 0
prlimit64(0, RLIMIT_STACK, {rlim_cur=16384*1024, rlim_max=RLIM64_INFINITY}, NULL) = 0
rt_sigaction(SIGSEGV, {sa_handler=0x1069640, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART|SA_RESETHAND|SA_SIGINFO, sa_restorer=0x10795f0}, NULL, 8) = 0
rt_sigaction(SIGILL, {sa_handler=0x1069640, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART|SA_RESETHAND|SA_SIGINFO, sa_restorer=0x10795f0}, NULL, 8) = 0
rt_sigaction(SIGBUS, {sa_handler=0x1069640, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART|SA_RESETHAND|SA_SIGINFO, sa_restorer=0x10795f0}, NULL, 8) = 0
rt_sigaction(SIGFPE, {sa_handler=0x1069640, sa_mask=[], sa_flags=SA_RESTORER|SA_RESTART|SA_RESETHAND|SA_SIGINFO, sa_restorer=0x10795f0}, NULL, 8) = 0
rt_sigaction(SIGPIPE, {sa_handler=0x103a730, sa_mask=[], sa_flags=SA_RESTORER, sa_restorer=0x10795f0}, NULL, 8) = 0
gettid() = 46123
write(2, "debug: socket\n", 14debug: socket
) = 14
socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC, 0) = 3
mmap(NULL, 16785408, PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fcdef200000
mprotect(0x7fcdef201000, 16781312, PROT_READ|PROT_WRITE) = 0
clone(child_stack=0x7fcdf0200ff8, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID|0x400000, parent_tid=[46124], tls=0x7fcdf0201010, child_tidptr=0x7fcdf0201040) = 46124
strace: Process 46124 attached
[pid 46123] write(2, "debug: connect\n", 15debug: connect
) = 15
[pid 46123] connect(3, {sa_family=AF_UNIX, sun_path="/home/andy/dev/zig/.zig-cache/tmp/b8f2350f14c91bbf"}, 110 <unfinished ...>
[pid 46124] gettid() = 46124
[pid 46124] write(2, "debug: thread sleep\n", 20debug: thread sleep
) = 20
[pid 46124] nanosleep({tv_sec=0, tv_nsec=500000000}, 0x7fcdf0200da0) = 0
[pid 46124] write(2, "debug: thread shutdown\n", 23debug: thread shutdown
) = 23
[pid 46124] shutdown(3, SHUT_RDWR) = 0
[pid 46124] write(2, "debug: thread exit\n", 19debug: thread exit
) = 19
[pid 46124] exit(0) = ?
[pid 46124] +++ exited with 0 +++
^Cstrace: Process 46123 detached
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment