Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some sort of race condition in packaging.requirement #104

Closed
alex opened this issue Apr 7, 2017 · 6 comments
Closed

Some sort of race condition in packaging.requirement #104

alex opened this issue Apr 7, 2017 · 6 comments

Comments

@alex
Copy link
Member

alex commented Apr 7, 2017

import sys
import threading

from packaging.requirements import Requirement


N_THREADS = 32

def target():
    Requirement("x[]")

def main(argv):
    threads = []
    for i in range(N_THREADS):
        threads.append(threading.Thread(target=target))
    for t in threads:
        t.start()
    for t in threads:
        t.join()
    print("Ok")

if __name__ == "__main__":
    main(sys.argv)

This fails every once in a while. Put in a loop in your terminal for best results.

@alex
Copy link
Member Author

alex commented Apr 7, 2017

Oh, where are my manners, here's the traceback:

Exception in thread Thread-3:
Traceback (most recent call last):
  File "/Users/alex_gaynor/.pyenv/versions/2.7.13/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/Users/alex_gaynor/.pyenv/versions/2.7.13/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "t.py", line 10, in target
    Requirement("x[]")
  File "/Users/alex_gaynor/.virtualenvs/tempenv-420c1556839f5/lib/python2.7/site-packages/packaging/requirements.py", line 94, in __init__
    requirement_string[e.loc:e.loc + 8]))
InvalidRequirement: Invalid requirement, parse error at "''"

alex added a commit to alex/packaging that referenced this issue Apr 7, 2017
@dstufft dstufft closed this as completed in 17c9099 Apr 7, 2017
@dsethi
Copy link

dsethi commented Apr 7, 2017

Alex - many thanks for closing this -- I was able to use your code to replicate and test that this is indeed fixed. One followup question -- will this be part of an official release soon (so that I can get it with pip upgrade instead of doing a manual compile/install?) Thanks again!

kormat added a commit to kormat/scion that referenced this issue Sep 14, 2018
packaging 16.8 had a latent race-condition that is fixed in later
versions: pypa/packaging#104
kormat added a commit to scionproto/scion that referenced this issue Sep 14, 2018
packaging 16.8 had a latent race-condition that is fixed in later
versions: pypa/packaging#104
@fofoni
Copy link

fofoni commented Oct 25, 2022

This is still biting me with packaging==21.3. I understand that the PR (#105) tried two fixes (and went with the second):

  • Wrapping the parsing in a lock. I tested, and this actually works for me.
  • Initializing the pyparsing engine at import time, but no synchronization at parse time. I still get pyparsing errors when I run this inside a lot of threads. Indeed, the pyparsing folks recommend never getting inside pyparsing's codebase from more than one thread at the same time (Is PyParsing thread safe / enables multithreading? pyparsing/pyparsing#89 (comment)).

I also understand that packaging 22.0 will have a tailor-made parser instead of depending on pyparsing. Will this parser have global state which must be protected by a lock, or can I assume that calling packaging.requirements.Requirement will be thread-safe?

Thanks!

@pradyunsg
Copy link
Member

pradyunsg commented Oct 25, 2022

We no longer use pyparsing in main, so those race conditions aren't an issue.

@fofoni
Copy link

fofoni commented Oct 25, 2022

I'll assume the race conditions are no longer an issue? Thanks!

@pradyunsg
Copy link
Member

Indeed, typo fixed. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants