RobertCraigie / RobertCraigie/pyright-python

Pyright won't work again after getting interrupted through Ctrl+C

Open
#80 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
277
Forks
31
Avg merge
4h 22m
Merged PRs (30d)
3

Description

It turns out that force stopping pyright with Ctrl+C during the time a loading bar is visible on the screen (presumably npx running something), some crucial cleanup work is skipped, and a folder named .pyright-random_string stays present in the node modules directory (as an example of full path for my case: /home/itsdrike/.local/share/npm/_npx/77993788984d59ab/node_modules/.pyright-WRkpBM9m) when it should have gotten removed.

Because this folder didn't get deleted, running pyright the next time leads to problems from npx, detecting that the directory it wants to make is already there, and ending with 217 error code, which doesn't give the user any description on what happened (because of the --silent flag). This is the actual error output from npx when using it manually to see the messages it produces (without silent):
image

Full traceback of the keyboard interrupt error propagation which did lead to this folder not getting removed:

Traceback (most recent call last):
  File "/home/itsdrike/.local/share/pyenv/versions/3.10.3/bin/pyright", line 8, in <module>
    sys.exit(entrypoint())
  File "/home/itsdrike/.local/share/pyenv/versions/3.10.3/lib/python3.10/site-packages/pyright/cli.py", line 81, in entrypoint
    sys.exit(main(sys.argv[1:]))
  File "/home/itsdrike/.local/share/pyenv/versions/3.10.3/lib/python3.10/site-packages/pyright/cli.py", line 21, in main
    return run(*args, **kwargs).returncode
  File "/home/itsdrike/.local/share/pyenv/versions/3.10.3/lib/python3.10/site-packages/pyright/cli.py", line 57, in run
    return node.run('npx', *pre_args, f'pyright@{version}', *args, **kwargs)
  File "/home/itsdrike/.local/share/pyenv/versions/3.10.3/lib/python3.10/site-packages/pyright/node.py", line 112, in run
    return subprocess.run(node_args, env=env, **kwargs)
  File "/home/itsdrike/.local/share/pyenv/versions/3.10.3/lib/python3.10/subprocess.py", line 503, in run
    stdout, stderr = process.communicate(input, timeout=timeout)
  File "/home/itsdrike/.local/share/pyenv/versions/3.10.3/lib/python3.10/subprocess.py", line 1141, in communicate
    self.wait()
  File "/home/itsdrike/.local/share/pyenv/versions/3.10.3/lib/python3.10/subprocess.py", line 1204, in wait
    return self._wait(timeout=timeout)
  File "/home/itsdrike/.local/share/pyenv/versions/3.10.3/lib/python3.10/subprocess.py", line 1938, in _wait
    (pid, sts) = self._try_wait(0)
  File "/home/itsdrike/.local/share/pyenv/versions/3.10.3/lib/python3.10/subprocess.py", line 1896, in _try_wait
    (pid, sts) = os.waitpid(self.pid, wait_flags)
KeyboardInterrupt

I actually already encountered this issue a good while ago, but I had no idea what was causing it and it ended up getting resolved by deleting the folder made by npx holding pyright. I mentioned this in #37, and since that, I did encounter the issue a few times again, but I blamed it on npx running poorly with Arch Linux, or something like that. However after a lot of debugging, I finally figured out why this was happening.

When running pyright with npx alone outside of using this library (running through python's subprocesses), npx is able to handle Ctrl+C occurring and gracefully exits after a while (taking it's time to remove this temporary folder, and perhaps do some other cleanup too). This can be seen from this error message:
image

However running pyright through the script this library provides only produces the python's traceback error, and leads to the main python process being stopped, which then most likely kills the npx subprocess forcefully, and prevents it's cleanup from running.

This should be handled by catching the KeyboardInterrupt while waiting on the npx process, and if it was catched, ending the process gracefully and waiting for it to close before actually exitting the python script (perhaps by re-raising the exception, or with a simple message and sys.exit call with non-zero code).

Since npx can take a while (few seconds) for it's cleanup to occur, and the script is running it with the --silent flag, it would probably be a good idea to also print out some message about this closing processes getting started immediately after the user pressed ctrl+c, to avoid them pressing it again, and causing another keyboard interrupt error during the graceful closing process, leading to this issue once again. Maybe even another handle which would that time indeed forcefully terminate the process, but also let the user know about this issue.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in pyright/node.py at run(), where npx is launched with subprocess.run, then review pyright/cli.py to understand how the return code is propagated. Reproduce an interrupt while the loading bar is visible and verify that npx receives graceful shutdown, its temporary .pyright-* directory is removed, and the wrapper exits without an unhandled traceback.

Written by the indexing model from the issue text.

Assessment

Tech stack
node.js, python
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.