Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caml implementation creates orphaned file descriptors #3422

Closed
vicuna opened this issue Jan 7, 2005 · 1 comment
Closed

Caml implementation creates orphaned file descriptors #3422

vicuna opened this issue Jan 7, 2005 · 1 comment
Labels

Comments

@vicuna
Copy link

vicuna commented Jan 7, 2005

Original bug ID: 3422
Reporter: administrator
Status: closed
Resolution: fixed
Priority: normal
Severity: minor
Category: ~DO NOT USE (was: OCaml general)

Bug description

The Mac OS X version of mldonkey has had a problem with orphaned file
descriptors. I was finally able to track down the source of the
problem and it has to do with the implementation of two of the unix
library routines "unix_accept" and "alloc_sockaddr". I've been working
the ocaml-3.08.0. Bascially the problem is that "unix_accept" calls
the unix routine "accept" which creates a file descriptor in "retcode"
(assuming no error). It then calls "alloc_sockaddr" and for some
reason, sometimes, the sa_family returned by unix "accept" does not
match any of the cases in "alloc_sockaddr". This causes "unix_error"
to be called in "alloc_sockaddr" which leave the file descriptor open
and there is no way in the user program to obtain and close it.

unix_accept is in otherlibs/unix/accept.c and is:

CAMLprim value unix_accept(value sock)
{
int retcode;
value res;
value a;
union sock_addr_union addr;
socklen_param_type addr_len;

addr_len = sizeof(addr);
enter_blocking_section();
retcode = accept(Int_val(sock), &addr.s_gen, &addr_len);
leave_blocking_section();
if (retcode == -1) uerror("accept", Nothing);
a = alloc_sockaddr(&addr, addr_len);
if (a == -1)
{
close(retcode);
unix_error(EAFNOSUPPORT, "", Nothing);
}
Begin_root (a);
res = alloc_small(2, 0);
Field(res, 0) = Val_int(retcode);
Field(res, 1) = a;
End_roots();
return res;
}

and "alloc_sockaddr" is in otherlibs/unix/socketaddr.c and is:

value alloc_sockaddr(union sock_addr_union * adr /in/,
socklen_param_type adr_len)
{
value res;
switch(adr->s_gen.sa_family) {
#ifndef _WIN32
case AF_UNIX:
{ value n = copy_string(adr->s_unix.sun_path);
Begin_root (n);
res = alloc_small(1, 0);
Field(res,0) = n;
End_roots();
break;
}
#endif
case AF_INET:
{ value a = alloc_inet_addr(&adr->s_inet.sin_addr);
Begin_root (a);
res = alloc_small(2, 1);
Field(res,0) = a;
Field(res,1) = Val_int(ntohs(adr->s_inet.sin_port));
End_roots();
break;
}
#ifdef HAS_IPV6
case AF_INET6:
{ value a = alloc_inet6_addr(&adr->s_inet6.sin6_addr);
Begin_root (a);
res = alloc_small(2, 1);
Field(res,0) = a;
Field(res,1) = Val_int(ntohs(adr->s_inet6.sin6_port));
End_roots();
break;
}
#endif
default:
res = (value) -1;
/*
unix_error(EAFNOSUPPORT, "", Nothing);
*/
}
return res;
}

The "unix_accept" routine should close the open file descriptor in
"retcode" if "alloc_sockaddr" gets an error. I tested this by moving
the "unix_error" routine out of "unix_accept" and instead returned a -1
to indicate an error. Then in each routine which called
"alloc_sockaddr" I tested the return value and called "unix_error" if
it was -1. Rebuilding mlnet (mldonkey) on Mac OS X with this change
eliminated the orphaned file descriptors. I do not know what value of
sa_family the unix "accept" command is returning to cause this error or
why it is returning a value that does not match your cases.

Since there appears to be no way for a user program that uses
"unix_accept" (ocaml Unix.accept) to obtain and close the file
descriptor opened by "unix_accept", I think that the library will have
to be changed to close "retcode" if alloc_sockaddr fails.

Thanks for your assistance.

Rand H. Childs

@vicuna
Copy link
Author

vicuna commented Jan 12, 2005

Comment author: administrator

see also #3423
fixed 2005-01-12 DD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant