Skip to content

Commit ed7c2b3

Browse files
committed
vine: count child_count properly
1 parent e657730 commit ed7c2b3

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

taskvine/src/worker/vine_transfer_server.c

+22-15
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,33 @@ static void vine_transfer_process(struct vine_cache *cache)
7676
while (1) {
7777
struct link *lnk = link_accept(transfer_link, time(0) + 10);
7878

79-
child_count++;
80-
pid_t p = fork();
81-
if (p == 0) {
82-
if (lnk) {
79+
if (lnk) {
80+
pid_t p = fork();
81+
if (p == 0) {
8382
vine_transfer_handler(lnk, cache);
8483
link_close(lnk);
85-
}
86-
_exit(0);
87-
} else {
88-
/* Also close the link in the parent process, otherwise the opened file descriptors will not be closed.
89-
* This caused a problem where incoming transfers were all failing due to the file descriptor limit per process being reached. */
90-
if (lnk) {
84+
_exit(0);
85+
} else if (p > 0) {
86+
/* Increment the child count when a new child is successfully forked. */
87+
child_count++;
88+
/* Also close the link in the parent process, otherwise the opened file descriptors will not be closed.
89+
* This caused a problem where incoming transfers were all failing due to the file descriptor limit per process being reached. */
9190
link_close(lnk);
92-
}
93-
if (child_count < VINE_TRANSFER_PROC_MAX_CHILD) {
94-
while (waitpid(-1, NULL, WNOHANG) > 0) {
95-
child_count--;
91+
/* If the child count is less than the maximum allowed, wait for any exited children and decrement the child count. */
92+
if (child_count < VINE_TRANSFER_PROC_MAX_CHILD) {
93+
while (waitpid(-1, NULL, WNOHANG) > 0) {
94+
child_count--;
95+
}
96+
continue;
9697
}
97-
continue;
98+
} else {
99+
/* If fork fails, log the error and close the link. */
100+
debug(D_VINE, "fork failed: %s", strerror(errno));
101+
link_close(lnk);
98102
}
103+
} else {
104+
/* If lnk is NULL, it means link_accept failed to accept a connection.
105+
* This could be due to a timeout or other transient issues. */
99106
}
100107

101108
debug(D_VINE, "Transfer Server: waiting on exited child. Reached %d", child_count);

0 commit comments

Comments
 (0)