[Twisted-Python] Windows spawnProcess - Child process inherits files and sockets (POSIX does not)

exvito here ex.vitorino at gmail.com
Tue Apr 5 15:11:19 MDT 2016


On Thu, Mar 31, 2016 at 10:27 PM, Glyph <glyph at twistedmatrix.com> wrote:

> Before submitting a patch for review, I'm looking for preliminary
> feedback, assuming you agree that the Windows vs POSIX semantics should be
> the same (if not, why?).
>
>
> After much thought: Yes.  They should be the same.  The reason they're not
> is largely ignorance of the relevant APIs and abstractions on Windows, not
> any desire that they differ.  The one place they have to differ a little
> bit is handle inheritance: we need to figure out some way to express the
> 'childFDs' mapping in terms of both file descriptors and handles.
>

Agreed. My code does not go into 'childFDs' mapping territory, though. All
it does is prevent all file- and socket-handles from being inherited by the
child process -- other than the pipes used for STDIO communication.

My patch calls a few Windows APIs via ctypes, however, as far as I can
> tell, Twisted on Windows requires pywin32 and, recently, there has been
> some discussion around dropping that dependency and moving towards
> something based on cffi.
>
>
> ctypes is dangerous and error-prone.  If you screw up the bit-width of a
> type, or the type in a header changes on some future version, ctypes gives
> you no way of finding out until some poor user's process segfaults, and
> usually not at the relevant call site.  So we'd prefer not to maintain more
> ctypes-using code.
>
> The APIs in pywin32 very closely mirror the underlying Windows API, so for
> addressing this issue, please just go ahead and use pywin32 APIs; porting
> them to a new API along with everything else should be relatively
> straightforward.
>

> If we do move forward with that change, we will probably use
> https://pypi.python.org/pypi/pywincffi and not move anything within
> Twisted.
>

Agreed with your ctypes comment -- I've been hit by such faults which
"magically" went away using cffi when coding against Windows TAPI.

pywin32, unfortunatelly, does not include two Windows APIs (out of four) my
code requires -- I just grepped the source for latest release I could find
on SourceForge, 220.

For completeness, the missing APIs are NtQuerySystemInformation [1] and
NtQueryObject [2].
The others are GetHandleInformation [3] and SetHandleInformation [4].

What would you say the way forward is? Should I submit the patch for review
> anyway? Is there any other work that needs to be done first that I may
> contribute to?
>
>
> Yes, just go ahead and write the patch.
>

Given that pywin32 does not provide two of the required APIs, maybe this
issue is somewhat blocked.

Adding to that is the fact that one particular API call in my code --
NtQuerySystemInformation [1] -- is being used with what seems to be an
undocumented option -- SystemHandleInformation (enum = 16) -- and
returning, again, an apparently undocumented data structure --
SYSTEM_HANDLE_INFORMATION. I downloaded and installed the available SDKs
and WDKs (driver dev kits) from Microsoft and could not find any reference
to those particular options or data structures.

My code was created after much investigation on how to obtain the list of
open handles for the current process.
The gist of it is:
- Call NtQuerySystemInformation with the SystemInformationClass arg set
to SystemHandleInformation.
- This returns all (!!!) of the handles in the system (no need for special
privileges).
- Filter those out by the current process PID and type, such that only
files and sockets are left.
- Use the GetHandleInformation to get the inheritance flag and clear it
with SetHandleInformation if needed.

It is mostly based on SysInternals information at
http://forum.sysinternals.com/howto-enumerate-handles_topic18892.html.
There are many other references across the web to those undocumented
options and data structures. But none of those come from formal Microsoft
documents, that I could find.

An alternative approach, which I also tried, for completeness sake, was to
try and Get/SetHandleInformation on all possible handles -- this is
completely unfeasible given that handles are at least 32 bit numbers.

After all of this -- including some frustration, I confess -- I decided to
go ahead and create a cffi ABI-mode variation of my original patch, anyway:
it passes the same tests and, much like the ctypes approach, works nicely
on my environment: Win 7 32, Win 2008R2 64, Win XP 32 (!!!), Python 2.7.11
32, Twisted 16.1, cffi 1.5.2.

Just for kicks I compared the performance of the ctypes vs cffi
implementation:
- The ctypes code runs in 0.014s - 0.016s.
- The cffi code runs in 0.03s - 0.04s.

This makes sense given that the code is mostly calling out to DLLs and,
AFAICT, cffi does the nice extra work of validating/converting types back
and forth.


Wrapping up: I'm really not sure how to more forward with this: not only
does pywin32 not provide the needed APIs, but also one of those APIs --
documented -- is being used in an undocumented fashion.

Even though I'd love to submit a patch, I don't think we're at that point
yet. However, for posterity's sake and if anyone wants to take a look at
the code, it is avalable at https://github.com/exvito/twisted in branches
win32-fix-handle-inherit-cffi and win32-fix-handle-inherit-ctypes. They add
two tests to twisted/test/test_process.py, one line to
twisted/internet/_dumbwin32proc.py and one module named
twisted/internet/_win32handleinherit.py

I'd love to hear feedback or ideas on this.
Thanks again

[1]
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724509(v=vs.85).aspx
[2] https://msdn.microsoft.com/en-us/library/bb432383(v=vs.85).aspx
[3]
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724329(v=vs.85).aspx
[4]
https://msdn.microsoft.com/en-us/library/windows/desktop/ms724935(v=vs.85).aspx
--
exvito
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://twistedmatrix.com/pipermail/twisted-python/attachments/20160405/e3eb9e53/attachment.html>


More information about the Twisted-Python mailing list