[Mono-dev] close on exec

Kornél Pál kornelpal at gmail.com
Tue Sep 30 14:18:09 EDT 2008


Hi,

MS .NET creates new processes with handle inheritance enabled but on 
Windows only handles maked as inheriable are inherited to the new process.

When using System.Diagnostics.Process.Start() mono closes file 
descriptors in the forked process.

But if you call fork() it is your responsibility to close handles. 
Handles can only be closed safely right after fork() so if you don't 
have access to the source code of the library you can't fix it.

You can however create a subprocess using Process.Start() that will not 
contain the socket handle because Mono properly closes those file 
descriptors. But there still will be some leaked file descriptors from 
the new Mono process so you should create a native wrapper executable to 
minimalize the leakage.

Kornél

Jacob Gladish wrote:
> I'm using p/invoke in my code and the library I'm calling into may call fork()/exec(), at which point the new process has dup'd copies of some of my open files. Sadly, one was a tcp listener, which means if I restart my application the bind() fails when I create an instance of a TcpListener. I'm curious if anyone can confirm the windows/.net behavior for this situation. As far as I know win32 handle inheritance is off unless specified, which leads me to believe that all fd's should be set close on exec by the mono runtime on unix variants. 


More information about the Mono-devel-list mailing list