Skip to content

Scons crash when using multiple jobs #3351

Closed

Description

@kayhayen
  • Version of SCons

3.0.4, but I believe 2.3.x had it already.

  • Version of Python

2.7 through 3.7

  • How you installed SCons

Inline copy of Scons in Nuitka

  • What Platform are you on? (Linux/Windows and which version)

Windows

  • How to reproduce your issue? Please include a small self contained reproducer. Likely a SConstruct should do for most issues.

This is a race, one of which is rarely seen by me, but had been reported previously by users, it happens when 2 threads from --jobs create the same config cache:

  • How you invoke scons (The command line you're using "scons --flags some_arguments")

We use --jobs <cpu_count> by default in nuitka

Our workaround is very crude code, but working:

In CacheDir.py:

-            if os.path.isdir(path) and len(os.listdir(path)) != 0:
+            # Nuitka: Fix races where there is already the "config".
+            if os.path.isdir(path) and len([f for f in os.listdir(path) if os.p
ath.basename(f) != "config"]) != 0:

Basically it makes config not count, because it could be created right after you were looking for it to not exist. I assume there could still be races, but none have been observed by Nuitka users so far, as far as I know. The fix has been in Nuitka only for a long time. I believe that Windows is more prone to his race, but the issue is definitely there.

Activity

  1. mwichmann commented on Apr 13, 2019

    @mwichmann
    Collaborator

    given the section of code already has a huge comment about a race condition that it's trying to avoid, we can certainly do more. The comment centers around "exclusive file creation". That's available in Python3 since some version or another, but as long as we have to support Python2 it is not universally available. Maybe a version-conditional bit here would be worth something? (open takes a 'x' mode for files, and os.makedirs takes an exist_ok parameter).

  2. kayhayen commented on Apr 13, 2019

    @kayhayen
    Author

    In Nuitka I do something like this:

    from threading import Lock
    ....
    @contextmanager
    def _with_file_Lock():
        # This is a singleton, pylint: disable=global-statement
        global file_lock
    
        if file_lock is None:
            file_lock = Lock()
    
        file_lock.acquire()
        yield
        if file_lock is not None:
            file_lock.release()
    
    def something():
        with _with_file_Lock():
            if not os.path.isdir(path):
                os.makedirs(path)
    
    
    

    That is working with every Python version and i not sure how exclusive file creation can solve anything like that.

    Yours,
    Kay

  3. mwichmann commented on Apr 13, 2019

    @mwichmann
    Collaborator

    I have no idea under what conditions the scons code snippet runs - comment talks about processes but that might be inaccurate wording? threading.Lock is fine within a single process but how does it help if there really are multiple processes in play?

  4. mwichmann commented on Apr 14, 2019

    @mwichmann
    Collaborator

    ... working with every Python version and i not sure how exclusive file creation can solve anything like that.

    To reply directly to this point, instead of this kind of construct:

    def something():
        with _with_file_Lock():
            if not os.path.isdir(path):
                os.makedirs(path)
    

    instead just try the makedirs operation and move on if it fails, then you can do the same with exclusive open on the file:

    def something():
        try:
            os.makedirs(path, exist_ok=True)
        except FileExistsError:
            pass
        
        config = "Some-Config"
        try:
            with open("foo", "x") as f:
                f.write(config)
        except FileExistsError:
            with open("foo", "r") as f:
                config = f.read()
    
  5. kayhayen commented on Apr 14, 2019

    @kayhayen
    Author

    @mwichmann I agree that ought to fare even better, but is Python3 only, due to its use of x mode, which is not for Python2. And without x, you would have to be able to tell when the writing of the file is done, or else you risk reading incomplete files.

    The easy solution for an application in full control to this issue could be having a EOF indicator on the file content level, so you know if you read was complete or terminated early, then loop on that.

  6. mwichmann commented on Apr 14, 2019

    @mwichmann
    Collaborator

    Yes, I'm aware this is a Python3-only answer (see earlier comment); there are some places where we have version-specific blocks and I was just speculating if that would makes sense here. Not my call...

  7. bdbaddog commented on Apr 14, 2019

    @bdbaddog
    Contributor

    Sounds like a python3 specific fix would be simpler and better?

    @kayhayen - Are you using Py3 or py2 or both? Can you live with running just py3? (if SCons dropped py27 support would it impact you?)

  8. kayhayen commented on Apr 15, 2019

    @kayhayen
    Author

    @bdbaddog While Nuitka does 2.6-2.7, 3.3-3.7, I do not need Scons to do that. In fact, right now, there is a gap already for early Python3 versions. But on Windows, there is an easy way to find installed Pythons, and Nuitka has used that method, when Scons didn't do Python3 yet. And now does it for the 3.x versions you don't support.

    There are even 2 inline copies, due to Python 2.6 (a patched 2.3.x), after you dropped support for it, only recently did I switch Python 2.7 to the 3.0.4 inline copy. For RHEL they are in the RPM too, I think, and might get used, because you know, RHEL contains no software.

    On Debian with packages, I depend on scons from that distribution, and generally on Linux, I am using scons as from the system if it, and often it is installed, or else an inline copy. These are often very old, therefore I have typically workarounds. For instance, the gcc version detection should use --dumpversion which only outputs the version, and there I am monkey patching the gcc tool already to use that, because e.g. CondaCC will give crap there otherwise, and so might some MinGW64, but I forgot about that.

    On these systems, issues affect me, but I am e.g. going to keep the temp file workaround with overriding LINKCOM even if 3.0.5 or later will contain a fix, just because it also fixes the bug for systems you are no longer supporting.

    https://github.com/Nuitka/Nuitka/blob/412188e3780c054831600f1b9bd7e51ed9384a1b/nuitka/build/SingleExe.scons#L344

    You can see many other workaround, e.g. the spawn function is overloaded for Windows, I am not calling del via shell to delete files, I am blacklisting a ton of tools and their detection, as I am not using them, but that sometimes spawns processes and more.

    So, long story short, I don't need fixes for the issues from Scons. I just wanted to report them, so they are known to you. And Python 3.5 or higher would be perfectly fine I think, but you make your own decisions.

    Yours,
    Kay

  9. bdbaddog commented on Apr 16, 2019

    @bdbaddog
    Contributor

    Any reason you don't run SCons only with supported python versions?
    Then whatever other processing you do can be run with whatever other python versions you need.
    This would allow you to use newest(er) SCons on all systems.
    And/or or you can vendor scons-local distribution into your repo and update/patch as needed.

  10. acmorrow commented on Apr 16, 2019

    @acmorrow
    Contributor

    We at MongoDB have also had occasional situations where it appears that two separate python processes running on different machines race on setting up a new CacheDir on an NFS mount. A python3 only fix would be fine for us.

  11. mwichmann commented on Apr 16, 2019

    @mwichmann
    Collaborator

    I can push something for this to experiment with... I assume, as it usually is, this fail is not reproducible, just happens "sometimes"?

  12. added a commit that references this issue on Apr 16, 2019
  13. added a commit that references this issue on Apr 28, 2019
  14. bdbaddog commented on Dec 29, 2019

    @bdbaddog
    Contributor

    Any reproduction of the issue post #3353 being merged?

  15. kayhayen commented on Dec 29, 2019

    @kayhayen
    Author

    No, this is perfect now, closing the issue. Nuitka has been released for months with nothing like it being reported anymore.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions