Mypy: Stdout hijacking in api.py is not thread safe

Created on 2 Jan 2019  路  7Comments  路  Source: python/mypy

See https://github.com/tomv564/pyls-mypy/issues/19, AFAIU mypy api.py relying on sys.stdout overriding is not thread-safe and causes issues when sys.stout and sys.stderr are used concurrently.

bug good-first-issue priority-1-normal

Most helpful comment

PR #6750 opened.

All 7 comments

This is a good catch. It may be a bit tricky to fix, but we鈥檙e open to PRs. There probably aren鈥檛 too many places where stdout or street are used, but passing something in may require a bunch of messy signature changes.

It would be better to explicitly pass the stdout and stderr objects instead of modifying them in sys. Would you like to contribute a fix? This should be a pretty easy one.

Here's a summary of what's involved for anybody interested in helping with this:

  • Don't modify sys.stdout and sys.stderr in mypy.api.
  • Instead, pass relevant StringIO objects to mypy.main.main as arguments. These can default to sys.stdin and sys.stderr in main.
  • Use these arguments when we generate error messages.

    • No need to update dmypy, stubgen and logging to use these, since they aren't relevant when using api.py.

Erm... Well it seems Github happily shows any reference of an issue from any fork whatsoever... that's good to know.

I wanted to wait for tomorrow so my colleague could test it on his setup since he was more "lucky" than me in getting the race conditions to "work" on his machine(tm). I guess nothing's wrong in putting the PR up for review now.

@elkhadiy, take the time you need to make a good change set. Don't worry we won't jump on you :)

What remains to be done about this issue? It causes a lot of problems on my setup (using pyls-mypy and spacemacs) and I'd be willing to do some work to fix it but the changes @elkhadiy made on their fork seem to already cover the needed changes.

@AnOctopus I believe the fork was committed to but a PR was never opened. If you would like to fix this, please feel free to open a PR! (Or perhaps @elkhadiy is still interested?)

PR #6750 opened.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dmoisset picture dmoisset  路  49Comments

MarkCBell picture MarkCBell  路  25Comments

tjltjl picture tjltjl  路  35Comments

gvanrossum picture gvanrossum  路  26Comments

glyph picture glyph  路  26Comments