microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.26k stars 1.15k forks source link

Code will no longer run after error in terminal #23607

Open Nostang3 opened 1 month ago

Nostang3 commented 1 month ago

Type: Bug

I wrote code to was using asycrio to capture stdout of a subprocess shell. The decoder received a character that it couldn't handle. The deoder was set to utf8. It caused an exception. And from that point on when hitting the run button does nothing.

VS Code version: Code 1.90.0 (89de5a8d4d6205e5b11647eb6a74844ca23d2573, 2024-06-04T19:33:54.889Z) OS version: Windows_NT x64 10.0.22631 Modes:

System Info |Item|Value| |---|---| |CPUs|AMD Ryzen 7 5800X 8-Core Processor (16 x 3800)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: enabled_on
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled| |Load (avg)|undefined| |Memory (System)|31.92GB (14.55GB free)| |Process Argv|--crash-reporter-id 30c69368-395a-476c-b6ce-c9033ab2e049| |Screen Reader|no| |VM|0%|
Extensions (12) Extension|Author (truncated)|Version ---|---|--- remotehub|Git|0.62.0 vsc-python-indent|Kev|1.18.0 debugpy|ms-|2024.6.0 python|ms-|2024.8.0 vscode-pylance|ms-|2024.6.1 remote-ssh|ms-|0.110.1 remote-ssh-edit|ms-|0.86.0 remote-wsl|ms-|0.88.2 powershell|ms-|2024.2.2 remote-explorer|ms-|0.4.3 remote-repositories|ms-|0.40.0 vscode-yaml|red|1.15.0
A/B Experiments ``` vsliv368:30146709 vspor879:30202332 vspor708:30202333 vspor363:30204092 vscoreces:30445986 vscod805:30301674 binariesv615:30325510 vsaa593cf:30376535 py29gd2263:31024239 c4g48928:30535728 azure-dev_surveyone:30548225 962ge761:30959799 pythongtdpath:30769146 welcomedialogc:30910334 pythonidxpt:30866567 pythonnoceb:30805159 asynctok:30898717 pythontestfixt:30902429 pythonregdiag2:30936856 pythonmypyd1:30879173 h48ei257:31000450 pythontbext0:30879054 accentitlementsc:30995553 dsvsc016:30899300 dsvsc017:30899301 dsvsc018:30899302 cppperfnew:31000557 dsvsc020:30976470 pythonait:31006305 jchc7451:31067544 chatpanelc:31048052 dsvsc021:30996838 9c06g630:31013171 pythoncenvpt:31062603 a69g1124:31058053 dvdeprecation:31068756 pythonprt:31056678 dwnewjupyter:31046869 2f103344:31071589 26j00206:31048877 ```
karthiknadig commented 1 month ago

Can you share the code that is causing this issue?

Nostang3 commented 1 month ago

Can you share the code that is causing this issue?

I've made a few changes since then but this is the gist of it. This was a proof of concept code to make sure I could capture all the stdout of a process opened by create_subprocess_shell. Streamlink offers a unique situation were the output eventually stops outputting new lines and just updates the same line over and over again as it updates size and Mbps of the capture which was the hard part for me. I"m not that great at the more trickier python coding, asyncio is very new to me, so please feel free to point out mistakes or better way to do this if you feel inclined. That in and of itself could be the cause of the error.

Made a comment in the code for the area of error and how I basically ignored the error to make it work.

import asyncio

async def run_command_live_output(command):
    # Create a subprocess
    process = await asyncio.create_subprocess_shell(
        command,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE,
    )

    async def read_stream(stream, callback):
        while True:
            line = await stream.read(65)
            if line:
                try:
                    callback(line.decode(encoding='utf8', errors='ignore'))  # ,<=== I didn't have the ignore option before but had to use it to remove the errors. 
                except UnicodeDecodeError as uniE:
                    print(f"UnicodeDecodeError: {uniE}")
            else:
                break

    await asyncio.gather(
        read_stream(process.stdout, lambda x: print(x, end='', flush=True)),
        read_stream(process.stderr, lambda x: print(x, end='', flush=True))
    )

    return_code = await process.wait()
    if return_code != 0:
        print(f"Command failed with return code {return_code}")
    else:
        print("Command completed successfully")

async def main():
    streamer = "bobross"
    command = f"streamlink twitch.tv/{streamer} best --stdout --progress force -o {streamer}.mp4"
    await run_command_live_output(command )

try:
    asyncio.run(main())
except KeyboardInterrupt:
    print("\nStopped by Interrupt")