Windows handles can be set to be either inheritable or not, to control whether child processes will receive them (when bInheritHandles
in CreateProcess
is TRUE). However, using SetHandleInformation
to mark a SOCKET non-inheritable does not always work. In particular, when certain Layered Service Providers (LSPs) are installed, the child process inherits the handle anyway. This is particularly likely to cause bugs with listening sockets. (But, because of another issue, if the child were to try using the socket, it would not be able! A real catch-22!)
Steps to reproduce
- Create eg a listening socket. Mark it non-inheritable using
SetHandleInformation
.
- Spawn a child, with
bInheritHandles
true.
- Close the socket in the parent, and attempt to re-bind to the port.
When a (non-IFS) LSP is installed, eg. PCTools Internet Security, the listening socket will be open in the child (visible in netstat
), despite SetHandleInformation
being called on the socket to disable inheritance before creating the child.
For an alternative scenario, see the (brief) steps in KB2398202.
What workarounds are available?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…