summaryrefslogtreecommitdiff
path: root/ext/socket/ipsocket.c
diff options
context:
space:
mode:
authorMisaki Shioi <31817032+shioimm@users.noreply.github.com>2024-11-29 18:49:02 +0900
committerGitHub <noreply@github.com>2024-11-29 18:49:02 +0900
commit49d2e79fb0dd57caed269c67660ff4cc68b3d729 (patch)
tree5624faad5d548283996a1d7eb530cde2e1495247 /ext/socket/ipsocket.c
parent22e1a8c478e92fafcb597d74a2d4f08c57f07c4c (diff)
Ensure to close pipes when `TCPSocket.new` finishes processing (#12181)
`TCPSocket.new` with HEv2 uses three threads. The last of these threads to exit closed pipes. However, if pipes were open at the end of the main thread, they would leak. This change avoids this by closing pipes at the end of the main thread.
Notes
Notes: Merged-By: shioimm <shioi.mm@gmail.com>
Diffstat (limited to 'ext/socket/ipsocket.c')
-rw-r--r--ext/socket/ipsocket.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/ext/socket/ipsocket.c b/ext/socket/ipsocket.c
index a3ccd9d58b..8a64a26b24 100644
--- a/ext/socket/ipsocket.c
+++ b/ext/socket/ipsocket.c
@@ -229,6 +229,7 @@ struct fast_fallback_inetsock_arg
rb_nativethread_lock_t *lock;
struct fast_fallback_getaddrinfo_entry *getaddrinfo_entries[2];
struct fast_fallback_getaddrinfo_shared *getaddrinfo_shared;
+ int wait;
int connection_attempt_fds_size;
int *connection_attempt_fds;
VALUE test_mode_settings;
@@ -313,7 +314,7 @@ cancel_fast_fallback(void *ptr)
{
arg->cancelled = true;
char notification = SELECT_CANCELLED;
- if ((write(arg->notify, &notification, 1)) < 0) {
+ if (arg->notify != -1 && (write(arg->notify, &notification, 1)) < 0) {
rb_syserr_fail(errno, "write(2)");
}
}
@@ -554,7 +555,7 @@ init_fast_fallback_inetsock_internal(VALUE v)
pthread_t threads[arg->family_size];
char resolved_type[2];
ssize_t resolved_type_size;
- int hostname_resolution_waiter = 0, hostname_resolution_notifier = 0;
+ int hostname_resolution_waiter = -1, hostname_resolution_notifier = -1;
int pipefd[2];
fd_set readfds, writefds;
@@ -587,6 +588,7 @@ init_fast_fallback_inetsock_internal(VALUE v)
if ((fcntl(hostname_resolution_waiter, F_SETFL, waiter_flags | O_NONBLOCK)) < 0) {
rb_syserr_fail(errno, "fcntl(2)");
}
+ arg->wait = hostname_resolution_waiter;
hostname_resolution_notifier = pipefd[1];
wait_arg.readfds = &readfds;
@@ -599,7 +601,6 @@ init_fast_fallback_inetsock_internal(VALUE v)
rb_nativethread_lock_initialize(arg->getaddrinfo_shared->lock);
arg->getaddrinfo_shared->notify = hostname_resolution_notifier;
- arg->getaddrinfo_shared->wait = hostname_resolution_waiter;
arg->getaddrinfo_shared->connection_attempt_fds = arg->connection_attempt_fds;
arg->getaddrinfo_shared->connection_attempt_fds_size = arg->connection_attempt_fds_size;
arg->getaddrinfo_shared->cancelled = false;
@@ -1191,6 +1192,10 @@ fast_fallback_inetsock_cleanup(VALUE v)
arg->local.res = 0;
}
+ if (arg->wait != -1) close(arg->wait);
+ if (getaddrinfo_shared->notify != -1) close(getaddrinfo_shared->notify);
+ getaddrinfo_shared->notify = -1;
+
if (getaddrinfo_shared) {
if (arg->family_size == 1) {
free_fast_fallback_getaddrinfo_shared(&getaddrinfo_shared);