subprocess "handle is invalid" error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Grant Edwards

    #1

    subprocess "handle is invalid" error

    I'm trying to use the py-gnuplot module on windows, and have
    been unable to get it to work reliably under Win2K and WinXP.

    By default, it uses popen(gnuplotcm d,'w'), but in some
    situations that consistently gets an "invalid operand" IOError
    when write() is called on the pipe.

    So I switched to subprocess. It works fine when executed
    "normally" (e.g. "python progname.py"), but when bundled by
    py2exe, it always does this:

    Traceback (most recent call last):
    File "surfedit.p y", line 28, in ?
    File "Gnuplot\_Gnupl ot.pyc", line 178, in __init__
    File "Gnuplot\gp_win 32.pyc", line 117, in __init__
    File "subprocess.pyc ", line 533, in __init__
    File "subprocess.pyc ", line 607, in _get_handles
    File "subprocess.pyc ", line 634, in _make_inheritab le
    WindowsError: [Errno 6] The handle is invalid

    How does one troubleshoot errors that happen three layers deep
    in the subprocess module?

    --
    Grant Edwards grante Yow! I'm a fuschia bowling
    at ball somewhere in Brittany
    visi.com
  • Erik Johnson

    #2
    Re: subprocess "handle is invalid" error


    "Grant Edwards" <grante@visi.co mwrote in message
    news:132cdrpqsm nvpa7@corp.supe rnews.com...

    <snip stuff about gnu-plot and py2exe problems>
    How does one troubleshoot errors that happen three layers deep
    in the subprocess module?
    I think the problem is more likely in how your py2exe is getting
    bundled, rather than any real problem with the subprocess module. I have
    been on the py2exe email list but not really paying much attention... If I
    recall correctly, there are some special considerations when using gnuplot
    with py2exe? I would suggest digging through the py2exe email archives to
    see if you can locate emails on that subject (one copy of which I believe
    can be found at:
    http://sourceforge.net/mailarchive/f...=py2exe-users), and
    if that doesn't work, to direct your question to the py2exe users list at:
    py2exe-users@lists.sou rceforge.net

    (I believe the current home page of the py2exe project is at:
    http://sourceforge.net/projects/py2exe/)

    Hope that helps,
    -ej


    Comment

    • Grant Edwards

      #3
      Re: subprocess &quot;handle is invalid&quot; error

      On 2007-04-18, Erik Johnson <nobody@invalid .comwrote:
      >
      "Grant Edwards" <grante@visi.co mwrote in message
      news:132cdrpqsm nvpa7@corp.supe rnews.com...
      >
      ><snip stuff about gnu-plot and py2exe problems>
      >How does one troubleshoot errors that happen three layers deep
      >in the subprocess module?
      >
      I think the problem is more likely in how your py2exe is
      getting bundled, rather than any real problem with the
      subprocess module.
      I've switched back to os.popen() since I seem to get the same
      problems with either. The odd thing is that it only seems to
      fail when the current directory is mounted via the network
      _and_ when it's being started by clicking on a file whose
      filetype is associated with the application.

      Running the program from the command line (cmd.exe or bash)
      always works for any current directory. Running the program by
      cliking on a shortcut icon on the desktop always works for any
      'start in' directory.

      The one case that doesn't work is when its run by clicking on
      an associated file on a network drive.

      --
      Grant Edwards grante Yow! Now I'm concentrating
      at on a specific tank battle
      visi.com toward the end of World
      War II!

      Comment

      • Thomas Heller

        #4
        Re: subprocess &quot;handle is invalid&quot; error

        Grant Edwards schrieb:
        I'm trying to use the py-gnuplot module on windows, and have
        been unable to get it to work reliably under Win2K and WinXP.
        >
        By default, it uses popen(gnuplotcm d,'w'), but in some
        situations that consistently gets an "invalid operand" IOError
        when write() is called on the pipe.
        >
        So I switched to subprocess. It works fine when executed
        "normally" (e.g. "python progname.py"), but when bundled by
        py2exe, it always does this:
        >
        Traceback (most recent call last):
        File "surfedit.p y", line 28, in ?
        File "Gnuplot\_Gnupl ot.pyc", line 178, in __init__
        File "Gnuplot\gp_win 32.pyc", line 117, in __init__
        File "subprocess.pyc ", line 533, in __init__
        File "subprocess.pyc ", line 607, in _get_handles
        File "subprocess.pyc ", line 634, in _make_inheritab le
        WindowsError: [Errno 6] The handle is invalid
        >
        How does one troubleshoot errors that happen three layers deep
        in the subprocess module?
        >
        I think this is a subprocess bug. It is often attributed to py2exe because
        usually developers do never run the script in pythonW.exe instead of python.exe,
        and later build a *windows* program with py2exe (the *windows* program has no
        console, and that triggers the bug).

        Consider ths little script:

        """
        import os, sys, subprocess

        sys.stderr = open("errors.tx t", "w")

        if os.path.exists( "output.txt "):
        os.remove("outp ut.txt")

        proc = subprocess.Pope n("dir", shell=True,
        stdout=subproce ss.PIPE,
        ## stderr=subproce ss.PIPE,
        ## stdin=subproces s.PIPE,
        )
        ##proc.stderr.c lose()
        ##proc.stdin.cl ose()
        data = proc.stdout.rea d()

        open("output.tx t", "w").write(data )
        """

        It calls 'dir' in the current directory, and writes the output
        to the file 'output.txt'. Any errors are written to 'errors.txt'.

        When you run this script with python.exe, everything works.
        If the script is run with pythonW.exe, nothing works and 'errors.txt'
        contains this:

        c:\svn\theller> type errors.txt
        Traceback (most recent call last):
        File "test_subproc.p y", line 9, in <module>
        stdout=subproce ss.PIPE,
        File "c:\python25\li b\subprocess.py ", line 586, in __init__
        errread, errwrite) = self._get_handl es(stdin, stdout, stderr)
        File "c:\python25\li b\subprocess.py ", line 699, in _get_handles
        p2cread = self._make_inhe ritable(p2cread )
        File "c:\python25\li b\subprocess.py ", line 744, in _make_inheritab le
        DUPLICATE_SAME_ ACCESS)
        WindowsError: [Error 6] Das Handle ist ungültig
        c:\svn\theller>

        The error message, translated to english, is 'the handle is invalid'.

        The script can be made to work correctly even with pythonW.exe (and also
        as py2exe'd windows program, I just checked it out) when the 4 commented out
        lines are uncommented. subprocess cannot inherit the standard handles when
        the process has no console, you have to create pipes for all 3 channels, and
        close those that are not needed.

        I thought that this bug was fixed in Python2.5.1 (the release candidate),
        but it seems it wasn't. The bug is at
        http://sourceforge.net/tracker/index...70&atid=105470

        If all this is correct, I hope that someone adds a section to the py2exe wiki;
        and reopens the above bug report.

        Thomas

        Comment

        • Grant Edwards

          #5
          Re: subprocess &quot;handle is invalid&quot; error

          On 2007-04-18, Thomas Heller <theller@ctypes .orgwrote:
          I think this is a subprocess bug. It is often attributed to
          py2exe because usually developers do never run the script in
          pythonW.exe instead of python.exe, and later build a *windows*
          program with py2exe (the *windows* program has no console, and
          that triggers the bug).
          [...]

          Ah, excellent analysis. It does indeed to appear to be a
          subprocess bug. Switching back to os.popen() makes things
          "work" again. There's a different problem when the program is
          started by double-clicking an associated file in a networked
          directory (works fine in a local directory). That was the
          initial problem I was trying to troubleshoot by switching
          trying subprocess.Pope n().
          The script can be made to work correctly even with pythonW.exe
          (and also as py2exe'd windows program, I just checked it out)
          when the 4 commented out lines are uncommented. subprocess
          cannot inherit the standard handles when the process has no
          console, you have to create pipes for all 3 channels, and
          close those that are not needed.
          Perhaps I'll try switching back to subprocess.Pope n() and
          creating all three pipes.
          I thought that this bug was fixed in Python2.5.1 (the release candidate),
          but it seems it wasn't. The bug is at
          http://sourceforge.net/tracker/index...70&atid=105470
          >
          >If all this is correct, I hope that someone adds a section to the py2exe wiki;
          It had been documented at the py2exe wiki -- though it mentions a
          different exception. I added the bug-tracker link and also
          that it throws the "invalid handle" exception.


          and reopens the above bug report.
          Since I'm still using 2.4.3, I don't think it would be
          appropriate for me to do so.

          --
          Grant Edwards grante Yow! It don't mean a
          at THING if you ain't got
          visi.com that SWING!!

          Comment

          • Daniel Serodio

            #6
            Re: subprocess &quot;handle is invalid&quot; error



            Thomas Heller-2 wrote:
            >
            Grant Edwards schrieb:
            >
            [snip]
            >
            >>
            > Traceback (most recent call last):
            > File "surfedit.p y", line 28, in ?
            > File "Gnuplot\_Gnupl ot.pyc", line 178, in __init__
            > File "Gnuplot\gp_win 32.pyc", line 117, in __init__
            > File "subprocess.pyc ", line 533, in __init__
            > File "subprocess.pyc ", line 607, in _get_handles
            > File "subprocess.pyc ", line 634, in _make_inheritab le
            > WindowsError: [Errno 6] The handle is invalid
            >>
            >How does one troubleshoot errors that happen three layers deep
            >in the subprocess module?
            >>
            >
            I think this is a subprocess bug. It is often attributed to py2exe
            because
            usually developers do never run the script in pythonW.exe instead of
            python.exe,
            and later build a *windows* program with py2exe (the *windows* program has
            no
            console, and that triggers the bug).
            >
            [snip]
            >
            I thought that this bug was fixed in Python2.5.1 (the release candidate),
            but it seems it wasn't. The bug is at
            http://sourceforge.net/tracker/index...70&atid=105470
            >
            If all this is correct, I hope that someone adds a section to the py2exe
            wiki;
            and reopens the above bug report.
            >
            Thomas
            >
            I've been bitten by this same bug in 2.5.1, and the bug you linked to (which
            is now @ http://bugs.python.org/issue1124861) is still marked as fixed.

            I've tried to reopen it but it seems only the reporter (or some "admin"
            user) is able to do it.

            --
            View this message in context: http://www.nabble.com/subprocess-%22...p14740517.html
            Sent from the Python - python-list mailing list archive at Nabble.com.

            Comment

            Working...