Reputation: 41096
In windows, a child process can inherit handles of the parent process, given that the handle is created inheritable, and the child process is created with the "inherit handles" option.
How are those handles closed properly? I couldn't find any documentation on that.
Does the inherited handle in the child process become invalid when the parent process closes it (or terminates)? In that case, the child process would have to duplicate it. and only close the duplicate after use - but depending on how the handle value is passed to the child process, there would be a (miniscule) window in which the handle could become invalid before it is duplicated.
Or does the child process get its own handle, that it can (and should) close as needed?
That seems more reasonable to me, though I wonder how can we ensure that the child process knows of ll handles it became responsible for?
Upvotes: 0
Views: 234
Reputation: 33706
inherit handles this is duplicated handles. so they independent from parent process handles and not become invalid when the parent process closes self copy. and child process can (and should) close this handles when no more needed it. if not do this - handles will be closed when process exit.
how can we ensure that the child process knows of all handles it became responsible for?
formally possible enumerate all handles in self process (via NtQuerySystemInformation(SystemExtendedHandleInformation)
and for every handle call GetHandleInformation
and check for HANDLE_FLAG_INHERIT
. but this not have big sense and nobody do this.
need ask another question first - for what we pass handle(s) to child ? in most case we also pass information about this handles values to child. usually inside command line. child must understand format of command line and get information about handles.
how child will be use handles, close he they or it will be closed only when child exit - this already depend from child process. how it written/design
Upvotes: 1