@@ -76,26 +76,33 @@ static void vine_transfer_process(struct vine_cache *cache)
76
76
while (1 ) {
77
77
struct link * lnk = link_accept (transfer_link , time (0 ) + 10 );
78
78
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 ) {
83
82
vine_transfer_handler (lnk , cache );
84
83
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. */
91
90
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 ;
96
97
}
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 );
98
102
}
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. */
99
106
}
100
107
101
108
debug (D_VINE , "Transfer Server: waiting on exited child. Reached %d" , child_count );
0 commit comments