Skip to content

Occasional python crash in multiprocessing\forking.py on Windows. #718

Closed

Description

@SigmundVik

With the latest version of Emscripten from the incoming branch, I get this crash maybe one out of three times when compiling with emcc:

Exception WindowsError: WindowsError(5, 'Access is denied') in <Finalize object, dead> ignored
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
  File "C:\tools\Python26\lib\atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "C:\tools\Python26\lib\multiprocessing\util.py", line 277, in _exit_function
    p._popen.terminate()
  File "C:\tools\Python26\lib\multiprocessing\forking.py", line 274, in terminate
    _subprocess.TerminateProcess(int(self._handle), TERMINATE)
WindowsError: [Error 5] Access is denied
Error in sys.exitfunc:
Traceback (most recent call last):
  File "C:\tools\Python26\lib\atexit.py", line 24, in _run_exitfuncs
    func(*targs, **kargs)
  File "C:\tools\Python26\lib\multiprocessing\util.py", line 277, in _exit_function
    p._popen.terminate()
  File "C:\tools\Python26\lib\multiprocessing\forking.py", line 274, in terminate
    _subprocess.TerminateProcess(int(self._handle), TERMINATE)
WindowsError: [Error 5] Access is denied

Activity

  1. SigmundVik commented on Nov 18, 2012

    @SigmundVik
    ContributorAuthor

    I should mention that this happens when I compile my own code, I have not seen it while running tests.

  2. kripken commented on Nov 20, 2012

    @kripken
    Member

    @juj , have you seen something like this on the test bots?

  3. juj commented on Nov 21, 2012

    @juj
    Collaborator

    No, that's an error message I haven't seen on the bot output. It runs on Windows Vista. I do Windows builds on a Windows 7, on the current stable branch, and haven't gotten this issue either.

    Are there some options to control how emscripten behaves with multithreading?

  4. juj commented on Nov 23, 2012

    @juj
    Collaborator

    Sigh.. this seems to be the same old "Handle is Invalid" problem in disguise.

    1. There's this problem: http://bugs.python.org/issue3905
    2. I have now started getting the following crash, and I am unable to build on incoming: http://pastebin.com/VaPg9qXG
      • Interestingly, if buildbot invokes the build, this crash does not happen (!!!) It is perhaps exactly due to that buildbot does not spawn a visible shell window, and the bug is exactly related to behavior with shell windows.
    3. Googling around, other people are getting "Handle is Invalid"/"Access is denied" on various Python projects on Windows as well, e.g. http://osdir.com/ml/python-pyinstaller/2011-10/msg00031.html

    Don't yet know for a workaround for this case.

  5. juj commented on Nov 23, 2012

    @juj
    Collaborator

    Well well.. just random trial-and-effort mucking around sometimes does help. I managed to work around the problem (and now incoming builds successfully) by redirecting stdin in the vs-tool wrapper exes. See the commit that added the workaround: juj/vs-tool@0cbf44d

    Are you using the vs-tool wrappers @SiggyBar? If not and you have another external build tool invoke emscripten, it can exhibit the same problem. Perhaps try if going through vs-tool helps, if it's possible in your setup?

    (This is assuming that as indicated by my previous googling that our issues are caused by the same root problem)

  6. SigmundVik commented on Nov 27, 2012

    @SigmundVik
    ContributorAuthor

    Thank you for your feedback, @juj. I am not using the vs-tool wrappers, I invoke em++ from scons.

    I see that there already is a fix for http://bugs.python.org/issue3905 in tools\shared.py, is it not possible to add the redirection fix from juj/vs-tool@0cbf44d there?

  7. juj commented on Nov 27, 2012

    @juj
    Collaborator

    This fix juj/vs-tool@0cbf44d does not "complete" or "improve" on top of this fix https://github.com/kripken/emscripten/blob/master/tools/shared.py#L8 . They aim to do the exact same thing. The problem with the bug in this issue you reported, as it seems, is that it occurs from a different codepath what this fix https://github.com/kripken/emscripten/blob/master/tools/shared.py#L8 has access to. The codepath goes somewhere inside the multiprocessing library in python, and therefore there is no straightforward way to apply https://github.com/kripken/emscripten/blob/master/tools/shared.py#L8 to it. In my case, I noticed that using juj/vs-tool@0cbf44d avoids the issue also in multiprocessing, and hopefully throughout the whole emscripten process. It might actually make https://github.com/kripken/emscripten/blob/master/tools/shared.py#L8 obsolete, but only if emscripten is run via juj/vs-tool exe wrappers.

    So, in short, I don't know how to get "inside" python multiprocessing lib in any other way except via the method I used in juj/vs-tool.

    Did that clear or confuse even more? o_O

    If you have the time, try testing whether juj/vs-tool wrappers even help avoid the problem in your case, to confirm 100% if we are talking about the same source issue, since our error messages did differ a bit (Handle is Invalid vs Access is Denied). Installation instructions for that are here: https://github.com/kripken/emscripten/wiki/Using-Emscripten-from-Visual-Studio-2010

  8. SigmundVik commented on Nov 28, 2012

    @SigmundVik
    ContributorAuthor

    Maybe we can patch the python multiprocessing lib in a similar way to how spawnve() was patched for Windows here:
    http://scons.tigris.org/ds/viewMessage.do?dsForumId=1272&dsMessageId=2956316

    (Btw, we occasionally got that scons crash that this post talks about, and the patch fixed it.)

    Regarding the verification of the source issue, I don't understand how I can verify that we are indeed talking about the same problem unless I can make a fix that works with scons.

  9. juj commented on Nov 28, 2012

    @juj
    Collaborator

    The emscripten wrappers in vs-tool repository don't have anything to do with Visual Studio itself. You do not need Visual Studio to use them. They are just .exe files to allow calling 'emcc.exe' on the command line instead of having to call 'python emcc', which cmake and Visual Studio neither can easily understand.

  10. SigmundVik commented on Nov 28, 2012

    @SigmundVik
    ContributorAuthor

    Thanks, now I get it :)

    On a side note I noticed that your vs-tool implements response file parsing. I had to do the same thing to get emscripten to work with scons on Windows. Maybe it would be better if emcc would do the response file parsing itself?

    Here is the command line I use to call emcc (em++) from scons:

    env["CXXCOM"] = "cmdFwd.py python %s $CCFLAGS $_CPPINCFLAGS -o $TARGET $SOURCE" % emxxPath

    with cmdFwd.py implemented like this:

    import shlex
    import subprocess
    import sys
    
    def _ParseWithResponseFile(args):
        '''
        Parses the given list of arguments and (recursively) replaces all args
        that are filenames with a leading '@' sign with their content.
        '''
        newArgs = []
    
        for arg in args:
            if arg[0] == '@':
                try:
                    with open(arg[1:]) as file:
                        for line in file.readlines():
                            # Setting posix to True gives wrong paths, but with
                            # it set to False we need to unquote ourselves...
                            tokens = shlex.split(line, comments=True, posix=False)
                            tokens = [t[1:-1] if t[0] == '"' and t[-1] == '"'
                                else t for t in tokens]
                            newArgs += _ParseWithResponseFile(tokens)
                except IOError:
                    # If we could not open the file, we simply add the
                    # argument unchanged.
                    newArgs.append(arg)
            else:
                newArgs.append(arg)
    
        return newArgs
    
    if __name__ == '__main__':
        exit(subprocess.call(_ParseWithResponseFile(sys.argv[1:])))
  11. juj commented on Jan 13, 2013

    @juj
    Collaborator

    I am now seeing this same bug on my newly installed Win8 computer. Don't yet know how to resolve.

    �[0m�[31m�[1mLinking CXX executable ../bin/ClearScreen.html
    �[0mem++ running.. please wait.
    em++ running.. please wait.
    em++ running.. please wait.
    em++ running.. please wait.
    em++ running.. please wait.
    em++ running.. please wait.
    em++ running.. please wait.
    em++ running.. please wait.
    em++ running.. please wait.
    
    G:\Emscripten\tools\eliminator\node_modules\uglify-js\lib\process.js:1355
            return str.replace(/[\u0080-\uffff]/g, function(ch) {
                       ^
    RangeError: Maximum call stack size exceeded
    Warning: .ll contains floating-point values with more than 64 bits. Faking values for them. If they are used, this will almost certainly break horribly!
    
    
    
    =======================================
    
    bootstrapping relooper...
    
      bootstrap phase 1
    
      bootstrap phase 2
    
    bootstrapping relooper succeeded
    
    =======================================
    
    Traceback (most recent call last):
    
      File "c:\python27\lib\multiprocessing\util.py", line 261, in _run_finalizers
    
        finalizer()
    
      File "c:\python27\lib\multiprocessing\util.py", line 200, in __call__
    
        res = self._callback(*self._args, **self._kwargs)
    
      File "c:\python27\lib\multiprocessing\pool.py", line 476, in _terminate_pool
    
        p.terminate()
    
      File "c:\python27\lib\multiprocessing\process.py", line 137, in terminate
    
        self._popen.terminate()
    
      File "c:\python27\lib\multiprocessing\forking.py", line 306, in terminate
    
        _subprocess.TerminateProcess(int(self._handle), TERMINATE)
    
    WindowsError: [Error 5] Access is denied
    
    Error in atexit._run_exitfuncs:
    
    Traceback (most recent call last):
    
      File "c:\python27\lib\atexit.py", line 24, in _run_exitfuncs
    
        func(*targs, **kargs)
    
      File "c:\python27\lib\multiprocessing\util.py", line 291, in _exit_function
    
        p._popen.terminate()
    
      File "c:\python27\lib\multiprocessing\forking.py", line 306, in terminate
    
        _subprocess.TerminateProcess(int(self._handle), TERMINATE)
    
    WindowsError: [Error 5] Access is denied
    
    Error in sys.exitfunc:
    
    Traceback (most recent call last):
    
      File "c:\python27\lib\atexit.py", line 24, in _run_exitfuncs
    
        func(*targs, **kargs)
    
      File "c:\python27\lib\multiprocessing\util.py", line 291, in _exit_function
    
        p._popen.terminate()
    
      File "c:\python27\lib\multiprocessing\forking.py", line 306, in terminate
    
        _subprocess.TerminateProcess(int(self._handle), TERMINATE)
    
    WindowsError: [Error 5] Access is denied
    
    
    Traceback (most recent call last):
      File "G:\Emscripten\emcc", line 1257, in <module>
        flush_js_optimizer_queue()
      File "G:\Emscripten\emcc", line 1208, in flush_js_optimizer_queue
        final = shared.Building.js_optimizer(final, js_optimizer_queue, jcache)
      File "G:\Emscripten\tools\shared.py", line 1038, in js_optimizer
        return js_optimizer.run(filename, passes, NODE_JS, jcache)
      File "G:\Emscripten\tools\js_optimizer.py", line 177, in run
        return temp_files.run_and_clean(lambda: run_on_js(filename, passes, js_engine, jcache))
      File "G:\Emscripten\tools\shared.py", line 435, in run_and_clean
        return func()
      File "G:\Emscripten\tools\js_optimizer.py", line 177, in <lambda>
        return temp_files.run_and_clean(lambda: run_on_js(filename, passes, js_engine, jcache))
      File "G:\Emscripten\tools\js_optimizer.py", line 140, in run_on_js
        filenames = pool.map(run_on_chunk, commands, chunksize=1)
      File "c:\python27\lib\multiprocessing\pool.py", line 227, in map
        return self.map_async(func, iterable, chunksize).get()
      File "c:\python27\lib\multiprocessing\pool.py", line 528, in get
        raise self._value
    AssertionError: Error in js optimizer: 
    Press any key to continue . . . 
    mingw32-make[2]: *** [bin/ClearScreen.html] Error 1
    mingw32-make[1]: *** [ClearScreen/CMakeFiles/ClearScreen.dir/all] Error 2
    mingw32-make: *** [all] Error 2
    program finished with exit code 2
    elapsedTime=218.040000
    
  12. juj commented on Jan 13, 2013

    @juj
    Collaborator

    So far I've only seen this in release mode builds, never in debug, and it seems to occur systematically on release builds.

    On another note, the error

    G:\Emscripten\tools\eliminator\node_modules\uglify-js\lib\process.js:1355
            return str.replace(/[\u0080-\uffff]/g, function(ch) {
                       ^
    RangeError: Maximum call stack size exceeded
    

    seems to be related to something completely different.

  13. kripken commented on Jan 13, 2013

    @kripken
    Member

    The call stack error might be due to the llvm switch issue we are investigating in the other bug. Other testcases would be useful (as usual, the files from EMCC_DEBUG).

  14. juj commented on Jan 13, 2013

    @juj
    Collaborator

    Yeah, I now know that this call stack error was caused exactly by that long GLenum->string switch-case function, so no need to go into that further here. It did not however resolve this problem with multiprocessing "access is denied", which is the next one to be solved.

  15. juj commented on Jan 14, 2013

    @juj
    Collaborator

    If I brutishly go into File "c:\python27\lib\multiprocessing\pool.py", line 476, in _terminate_pool

    and replace the line

    p.terminate()
    

    with

    try:
        p.terminate()
    except WindowsError:
        pass
    

    I get past the error, and the Emscripten builds succeed fine, and I get proper runnable .html files. The issue seems to be somehow caused by when Python tries to tear down the multiprocessing worker pool at process exit time. I don't know how big of a hack this is at the moment.

  16. juj commented on Jan 14, 2013

    @juj
    Collaborator
  17. juj commented on Jan 14, 2013

    @juj
    Collaborator

    I settled to patching Python27\lib\multiprocessing\pool.py locally with the following:

    45a46
    > import sys
    476c477,481
    <                     p.terminate()
    ---
    >                     try:
    >                         p.terminate()
    >                     except WindowsError, e:
    >                         print >> sys.stderr, 'Warning: WindowsError exception received when terminating worker pool thread: %s' % str(e)
    >                         pass
    

    That's good enough for me, I don't know any more about the details of what's going on, but since emscripten is building fine, I'm happy with this. Cross-checking with the stack provided by @SiggyBar in first post, the call stacks for us seem to be a bit different. But might this patch help in your case still?

  18. kripken commented on Jan 15, 2013

    @kripken
    Member

    Separately from confirming its the same issue for @SiggyBar, it looks like we should push that terminate except patch.

  19. juj commented on Jan 15, 2013

    @juj
    Collaborator

    The Windows build bot (which was switched to run on Windows 8 Pro 64-bit) now also runs the patched code, and it does report several of these errors during the run: http://clb.demon.fi:8112/builders/win-emcc-incoming-tests/builds/169/steps/Test%20emscripten%20incoming/logs/stdio

    My Windows 7 laptop does not give these exceptions.

  20. self-assigned this
    on Jul 28, 2014
  21. juj commented on Jun 29, 2016

    @juj
    Collaborator

    This should have been fixed some time ago, and also another call site with the same kind of issue should also be fixed (#4404), so we should no longer be seeing Access is Denied errors on Windows (when tearing down multiprocessing pools).

  22. pemryan commented on Sep 22, 2020

    @pemryan

    still happening occasionally on Python 3.8.5 @ Windows10

  23. jeffRTC commented on Jan 15, 2021

    @jeffRTC

    @juj I hit this problem on Python 3.9

    Since you guys removed msi installer, I had to restore manual jargon :((

  24. sbc100 commented on Jan 15, 2021

    @sbc100
    Collaborator

    You mean an MSI installer for emsdk? I don't remember emsdk ever having such a thing but it could just be before my time (in which case we stopping a long time ago). Or perhaps there was an MSI build that was maintained outside the core project?
    If you like windows installers maybe you will like recent chocolaty package that was recently added.

  25. kripken commented on Jan 15, 2021

    @kripken
    Member

    I think @juj maintained an MSI installer many years ago, but I'm not aware of anyone supporting one currently.

    Chocolatey as mentioned is an option, and also the normal download instructions.

  26. jeffRTC commented on Jan 15, 2021

    @jeffRTC

    @sbc100 @kripken MSI installer was on your Docs on 2020

    It had green logo on left side when launched.

  27. jeffRTC commented on Jan 15, 2021

    @jeffRTC

    I'm 99% sure I didn't time travel

  28. kripken commented on Jan 15, 2021

    @kripken
    Member

    It's possible the docs were outdated, sorry about that. They look accurate now at least.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions