Skip to content

Conversation

@elkhadiy
Copy link

@elkhadiy elkhadiy commented Jan 2, 2019

Overriding sys.stdout and sys.stderr in mypy.api is not threadsafe.
This causes problems sometimes when using the api in pyls for example.

I am struggling a little with getting the right type for f, was expecting something like Union[Callable[[TextIO, TextIO], None], Callable[[], None]] to work.

@elkhadiy elkhadiy force-pushed the fix_stdout_hijacking branch 2 times, most recently from 7975e09 to 00a803c Compare January 2, 2019 20:25
Overriding sys.stdout and sys.stderr in mypy.api is not threadsafe.
This causes problems sometimes when using the api in pyls for example.
@elkhadiy elkhadiy force-pushed the fix_stdout_hijacking branch from 00a803c to c465807 Compare January 2, 2019 21:04
Copy link
Collaborator

@msullivan msullivan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! I left some comments, and additionally:

There are some things this misses:

  • There are a handful of error message prints in build.py
  • In errors.py, there are a bunch of prints in report_internal_error for displaying a traceback. (I think that a case /could/ be made for having those bypass the API capture, though we'd probably also want to arrange for the failure to bypass that as well? By using a MypyInternalError exception that we turn into sys.exit(2) in __main__.py but let propagate in the api?)
  • The argparse.ArgumentParser argument processing will print to sys.stderr. To avoid this I think we need to create a subclass that overrides the error method.
  • Possibly others?

This is all pretty doable, but it makes me nervous about missing things.

It makes me wonder if the stable API ought to just use subprocess and invoke mypy in a fresh interpreter? It would be slower, but easier to make correct.
(Of course, I added mypy.api.run_dmypy specifically to avoid the performance penalty associated with interpreter startup, but we could make that an unstable API and document its thread safety limitations.)

def run_dmypy(args: List[str]) -> Tuple[str, str, int]:
from mypy.dmypy import main
return _run(lambda: main(args))
return _run(lambda stdout, stderr: main(args))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually do need to worry about mypy.dmypy, since it does have this API (which has a Dropbox internal client).

If you don't want to fix mypy.dmypy now, it is fine to leave run_dmypy API thread-safety as a TODO, but in that case we need to continue hijacking sys.stdout and sys.stderr in run_dmypy.


def main(script_path: Optional[str], args: Optional[List[str]] = None) -> None:
def main(script_path: Optional[str], args: Optional[List[str]] = None,
stdout: TextIO = sys.stdout,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't use sys.stdout as a default argument value, because it is evaluated at module load time, not when the function is called. This means that if something else hijacks stdout, we will ignore that and write to the original one.

In cases where it needs to have a default value, have it be an Optional[TextIO] defaulting to None, and do something along the lines of stdout = stdout or sys.stdout. I think a bunch of functions can just be changed to have it not have a default value.


def process_options(args: List[str],
stdout: TextIO = sys.stdout,
stderr: TextIO = sys.stderr,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd drop the stdout argument to this function and all the functions it call that don't need it.

@elkhadiy
Copy link
Author

elkhadiy commented Jan 3, 2019

  • There are a handful of error message prints in build.py

Thanks I'll check that and try and look around to see if we missed anything else.

  • In errors.py, there are a bunch of prints in report_internal_error for displaying a traceback. (I think that a case /could/ be made for having those bypass the API capture, though we'd probably also want to arrange for the failure to bypass that as well? By using a MypyInternalError exception that we turn into sys.exit(2) in __main__.py but let propagate in the api?)

Propagating exceptions through the api sounds like a wise decision. For example in mypy/api.py:1036:

def fail(msg: str, stderr: TextIO) -> None:
    stderr.write('%s\n' % msg)
    sys.exit(1)

Would it be better to raise an exception instead of sys.exit(1)?

  • The argparse.ArgumentParser argument processing will print to sys.stderr. To avoid this I think we need to create a subclass that overrides the error method.

Ah yes this little guy https://docs.python.org/dev/library/argparse.html#argparse.ArgumentParser.error needs to change.

It makes me wonder if the stable API ought to just use subprocess and invoke mypy in a fresh interpreter? It would be slower, but easier to make correct.

This is actually what we were first planning to do in https://github.com/tomv564/pyls-mypy/blob/master/pyls_mypy/plugin.py#L56, with something like:

res = subprocess.run(
    ["python", "-m", "mypy", *args],
    stdout=subprocess.PIPE, stderr=subprocess.PIPE
    )
report = res.stdout
errors = res.stderr

(Of course, I added mypy.api.run_dmypy specifically to avoid the performance penalty associated with interpreter startup, but we could make that an unstable API and document its thread safety limitations.)

This could also be a solution, I like it! Have the normal mypy.api.run be threadsafe and potentially slower and propose mypy.api.run_dmypy as an unstable alternative were we continue hijacking stdout and stderr while potentially ironing out missing things.

Let me first work with your comments and we can go from there.

@elkhadiy elkhadiy changed the title fix: fixed sys.stdout overriding in mypy.api (#6125) Fix sys.stdout overriding in mypy.api (#6125) Jan 3, 2019
@gvanrossum
Copy link
Member

If you’re just going to use subprocess, why bother with mypy.api at all?

@mr-c
Copy link
Contributor

mr-c commented Jun 22, 2019

Should this be closed in light of #6750 being merged?

@gvanrossum
Copy link
Member

Superseded by #6750.

@gvanrossum gvanrossum closed this Jun 22, 2019
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

Successfully merging this pull request may close these issues.

4 participants