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

Fwd: Caml implementation creates orphaned file descriptors #3423

Closed
vicuna opened this issue Jan 7, 2005 · 3 comments
Closed

Fwd: Caml implementation creates orphaned file descriptors #3423

vicuna opened this issue Jan 7, 2005 · 3 comments
Labels

Comments

@vicuna
Copy link

vicuna commented Jan 7, 2005

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

Bug description

Sorry, I sent you my modified code instead of the original code for
"unix_accept" and "alloc_sockaddr". I've put the original source
back as it was in this forwarded copy of my original message.

Rand

Begin forwarded message:

From: Rand randchilds@mac.com
Date: January 7, 2005 10:52:44 AM CST
To: caml-bugs@caml.inria.fr
Subject: Caml implementation creates orphaned file descriptors

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);
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:
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

Thanks for letting me know Damien.

Rand

On Jan 12, 2005, at 9:10 AM, Damien Doligez wrote:

From: Rand randchilds@mac.com

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.

Thanks for the well-delimited bug report. This is now fixed in the CVS
version.

-- Damien

@vicuna
Copy link
Author

vicuna commented Jan 12, 2005

Comment author: administrator

From: Rand randchilds@mac.com

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.

Thanks for the well-delimited bug report. This is now fixed in the CVS
version.

-- Damien

@vicuna
Copy link
Author

vicuna commented Jan 12, 2005

Comment author: administrator

see also #3422
fixed 2005-01-12 DD

@vicuna vicuna closed this as completed Jan 12, 2005
@vicuna vicuna added the bug label Mar 19, 2019
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