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

KeyboardInterrupt When Running Selection/Line in Python Terminal with 'Shift+Enter' #24422

Closed
CardonaEA opened this issue Oct 4, 2024 · 44 comments · Fixed by #24418, #24542 or #24555
Closed
Assignees
Labels
area-terminal bug Issue identified by VS Code Team member as probable bug verification-needed Verification of issue is requested verified Verification succeeded
Milestone

Comments

@CardonaEA
Copy link

CardonaEA commented Oct 4, 2024

Environment:
Operating System: Windows 11
VS Code Version: 1.94.0
Python Extension Version: v2024.16.0

Steps to reproduce:

Default setting: the 'shift+enter' keyboard shortcut Runs Selection/Line in Python REPL.

  1. Ensure that the setting "python.REPL.sendToNativeREPL" is set to false. (This setting leads to the 'Shift+Enter' keyboard shortcut running the Selection/Line in the Python Terminal.)
  2. Open a Python script file (.py) in VS Code.
  3. Highlight a line or selection of code in the editor.
  4. Press Shift+Enter to run the selection or line in the Python Terminal.

Expected Behavior:
The selected line or code executes in the Python Terminal without any interruptions or errors.

Actual Behavior:
A KeyboardInterrupt is outputted before the selection or line continues executing in the Python Terminal.

Additional Information:

  • The issue occurs consistently when running a selection or line with Shift+Enter with the specified configuration.
  • The KeyboardInterrupt appears even when the execution continues after the interrupt message.
  • If the execution starts from a Python terminal, an error message is displayed, but the KeyboardInterrupt does not appear anymore.
  • The issue can be also triggered by setting a different keyboard shortcut.
Screen.Recording.2024-10-04.211253.mp4
Screen.Recording.2024-10-04.211730.mp4
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Oct 4, 2024
@CardonaEA CardonaEA changed the title Bug: KeyboardInterrupt When Running Selection/Line in Python Terminal with 'Shift+Enter Bug: KeyboardInterrupt When Running Selection/Line in Python Terminal with 'Shift+Enter' Oct 4, 2024
@CardonaEA CardonaEA changed the title Bug: KeyboardInterrupt When Running Selection/Line in Python Terminal with 'Shift+Enter' KeyboardInterrupt When Running Selection/Line in Python Terminal with 'Shift+Enter' Oct 4, 2024
@levibaguley
Copy link

levibaguley commented Oct 5, 2024

I'm seeing the same thing. I rolled back to 14.1 and it doesn't do that anymore. Click dropdown on "uninstall" to roll back and uncheck "Auto update". I'm planning on coming back here at some point to check if it's been fixed.

@CardonaEA
Copy link
Author

Yes, no issue with v2024.14.1. Thanks.

@lenarddome
Copy link

I was experiencing the same issue and can confirm that rolling back to previous versions gets rid of it.

@nbwosm
Copy link

nbwosm commented Oct 7, 2024

Thanks @levibaguley! I had the same issue and this fixed.

@breininger88
Copy link

Phew. Same issue and this worked. Thanks!

@AdrianNewbiePython
Copy link

Hi. Thank you all for your contributions.
Just as it is shown in the video of @CardonaEA , the error started me after I disabled the REPL to be executed after the keyboard shortcut SHIFT + ENTER.

However, even specifying in "Preferences: Open Workplace Settings (JSON)" the setting "python.REPL.sendToNativeREPL" to false, the KeyboardInterrupt still shows up. I don't know if it is related to the issue, but even if I still turn it to false in "Preferences: Open Workplace Settings (JSON)", in the page "Preferences: Open Default Settings (JSON)" (which I cannot edit), the setting "python.REPL.sendToNativeREPL" is still set true.

@kcerniauskas3
Copy link

Issue remains in v2024.18.0.

@jafar-the-amazing
Copy link

Rolling Back did fix the issue and as @kcerniauskas3 said ; it still remains in v2024.18.0.

@dharmatech
Copy link

dharmatech commented Nov 5, 2024

I'm seeing the same thing. I rolled back to 14.1 and it doesn't do that anymore. Click dropdown on "uninstall" to roll back and uncheck "Auto update". I'm planning on coming back here at some point to check if it's been fixed.

I see folks have rolled back to 14.1 to workaround the issue.

But is it really required? Yes, the KeyboardInterrupt appears in the console, but is it actually causing a functional issue besides that?

I'm using version v2024.18.0 and yes the KeyboardInterrupt is happening, but other than that the extension appears to work OK.

@rodo-nunez
Copy link

I'm seeing the same thing. I rolled back to 14.1 and it doesn't do that anymore. Click dropdown on "uninstall" to roll back and uncheck "Auto update". I'm planning on coming back here at some point to check if it's been fixed.

I see folks have rolled back to 14.1 to workaround the issue.

But is it really required? Yes, the KeyboardInterrupt appears in the console, but is it actually causing a functional issue besides that?

I'm using version v2024.18.0 and yes the KeyboardInterrupt is happening, but other than that the extension appears to work OK.

Yes, it is. At least for me.

That KeyboardInterrupt error was stopping my session after a real error or warning. So I was not able to debug my code without losing a lot of time and rerunning everything again and again. So I stopped using the extension and went back to another IDE not based on VSCode.

@anthonykim1
Copy link

Thanks everyone for the input here.
Is everyone on Windows? I can't seem to repro on my MacOS ARM machine.
I DO see the keyboard interrupt on my Windows machine though.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Nov 6, 2024
@anthonykim1 anthonykim1 added bug Issue identified by VS Code Team member as probable bug and removed triage-needed Needs assignment to the proper sub-team labels Nov 6, 2024
@levibaguley
Copy link

Yes, Windows 10 for me

@anthonykim1
Copy link

anthonykim1 commented Nov 6, 2024

I see, can folks also give 2024.16.1 a try and see whether it also adds the keyboard interrupt or not?
I personally don't see the keyboard interrupt on 16.1 but just wanted to make sure. Make sure to restart extension and run command

Edit: After restarting extension, and reloading with Python clear cache, I can repro this on 16.1 too

'Python: Clear Cache and Reload' via command palette (cmd/ctrl + shift + p)
Trying to narrow down the cause here :) Thanks in advance.

@rodo-nunez
Copy link

rodo-nunez commented Nov 6, 2024

it does fail for me

Image

@rodo-nunez
Copy link

v2024.15.2024092702 fails for me

Image

v2024.15.2024090406 works fine for me

Image

Probably the bug was introduced somewhere in-between

Image

@anthonykim1
Copy link

Thanks for this @rodo-nunez This is incredibly helpful!
I will go further investigate this and try to come up with the fix. Will let you all know if I have any updates/fix :)

@rodo-nunez
Copy link

rodo-nunez commented Nov 6, 2024

Using binary search:

v2024.15.2024091801 failed
v2024.15.2024091202 worked fine
v2024.15.2024091601 failed
v2024.15.2024091301 failed

So my guess is that the bug was introduced on v2024.15.2024091301.

@rodo-nunez
Copy link

@anthonykim1 also, I was reproducing that using the "Python: Run Selection/Line in Python Terminal" command. I always change it to Ctrl+Enter, to be able to freely choose between "Python: Run Selection/Line in Python Terminal" and "Python: Run Selection/Line in Python REPL" whenever I want.

Image

On v2024.15.2024091301, I don't see problems with the REPL, only with the Terminal. But I don't use the REPL that much.

Image

@anthonykim1
Copy link

Ok, thanks to @rodo-nunez I was able to figure out where this is coming from quickly.

Its from this PR: https://github.com/microsoft/vscode-python/pull/24078/files
where we switched over to use executeCommand instead of sendText internally to send over Python code for Terminal REPL.
Now will try to come up with a fix without breaking people.

For now, the workaround to not get this keyboard interrupt would be going to setting and then disabling shell integration:
Image

And Yes! the REPL you mentioned in the editor is our new native REPL. It has intellisense and syntax highlighting, autocomplete so feel free to give it a try :)

@nbwosm
Copy link

nbwosm commented Nov 6, 2024

@anthonykim1 I upgraded to v2024.18.0 and disabled shell integration and can confirm that I am not getting the KeyboardInterupt error. Thanks.

Follow-up: After having made these changes, I noticed a modest amount of latency in sending the code to the Python terminal via Shift + Enter and getting executed. I have reverted to v2024.14.1 and undone the shell disable. This was most noticeable executing matplotlib plots.

@stats9
Copy link

stats9 commented Nov 6, 2024

Image

I'm using version 2024.18.0 and I used the prerelease version, but unfortunately for every line of code I run through the script, I have this issue keyboardInterrupt.
the shortcut I defined is CTRL + Enter, but the shortcut I changed is the same.
I really don't know what idea I can implement to solve this problem.

update:

i add this line to my jason settings (workspace jason settings):
"terminal.integrated.shellIntegration.enabled": false,
and fix my problem!
i disable it shell integration actually.

@rzhao271 rzhao271 added verified Verification succeeded and removed info-needed Issue requires more information from poster labels Dec 3, 2024
@rzhao271
Copy link

rzhao271 commented Dec 3, 2024

I still see KeyboardInterrupt with a Python 3.11.9 venv on Windows and with python.terminal.shellIntegration.enabled set to true.

Python 3.11.9 (tags/v3.11.9:de54cf5, Apr  2 2024, 10:12:12) [MSC v.1938 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> 
KeyboardInterrupt
>>> ids=["test adding", "2      +V", "\n ffda"],
>>> 
KeyboardInterrupt
>>> if num % 4 == 0:
...     subprocess.run(["python", "-c", "for i in range(0, 10000): print(i)"])
... 

I do not see it when python.terminal.shellIntegration.enabled is set to false.

@rzhao271 rzhao271 added verification-found Issue verification failed and removed verified Verification succeeded labels Dec 3, 2024
@rzhao271 rzhao271 reopened this Dec 3, 2024
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Dec 3, 2024
@anthonykim1
Copy link

do you have shell integration enabled in upper shell? Image
This general terminal one @rzhao271

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Dec 4, 2024
@anthonykim1
Copy link

anthonykim1 commented Dec 4, 2024

Further fix is on the way for folks using windows in regards to preventing keyboard interrupt #24542
Note that indentation error for windows still persists as explained above but there is already PR up in cpython itself to address indentation error for windows REPL in terminal folks.

@dbaeumer
Copy link
Member

dbaeumer commented Dec 4, 2024

I still see the KeyboardInterrupt

Image

@dbaeumer
Copy link
Member

dbaeumer commented Dec 4, 2024

I guess it needs a new Python extensions.

@dbaeumer
Copy link
Member

dbaeumer commented Dec 4, 2024

@anthonykim1 I will reopen this. Please close again when ready for verification.

@dbaeumer dbaeumer reopened this Dec 4, 2024
@dbaeumer
Copy link
Member

dbaeumer commented Dec 4, 2024

My fault. Was not on latest insiders.

@dbaeumer dbaeumer closed this as completed Dec 4, 2024
@dbaeumer dbaeumer added the verified Verification succeeded label Dec 4, 2024
@dbaeumer
Copy link
Member

dbaeumer commented Dec 4, 2024

Image

@rzhao271 rzhao271 removed info-needed Issue requires more information from poster verification-found Issue verification failed triage-needed Needs assignment to the proper sub-team labels Dec 4, 2024
anthonykim1 added a commit that referenced this issue Dec 9, 2024
Further resolves:
#24422
Prevent keyboard interrupt for Mac and Linux when using Python3.13

Having Python3.13 as interpreter choice and then enabling shell
integration where it is normally supported (we disabled temporarily for
Python3.13 due to python/cpython#126131), lead
to edge case.

So although we don't override user's PS1 in Python side after checking
Python3.13 is selected, we were not aware of this in typescript side,
leading to wrongly using executeCommand inside Python terminal REPL
(Python3.13 IDLE), instead of sendText.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment