Skip to content

VS Code Buffering Issue when Printing Outputs in Jupyter Notebooks #13565

@diftimieMSFT

Description

@diftimieMSFT

Does this issue occur when all extensions are disabled?: Not sure (need extensions to use Jupyter)

  • VS Code Version: 1.78.2
  • OS Version: Windows 11 Enterprise 22H2
  • Using Jupyter Notebooks in VS Code
  • Launching VS Code from WSL2 Ubuntu 18.04 terminal
  • Connecting Jupyter Notebook with conda environment (although issue seems to repro without conda)
  • Jupyter extension v2023.4.10112418018

Steps to Reproduce:

  1. Open VS code from WSL2 with code . (where conda environments are already initialized)
  2. Create a new Jupyter Notebook in VS Code
  3. Connect the Jupyter Notebook to a conda environment as the python kernel (select Kernel >> Python Environments >> conda environment)
  4. Populate the first python cell of the jupyter notebook with the following code:
    # Prompt the user 10 times (0-9 times) and ask them to enter the answer number
    # this will then print the question number and answer number together
    for question_num in range(0, 10):
        answer_num = input("Enter the Answer Number:")
        print(f"Question Num = #{question_num}; Answer Num = #{answer_num}")
  5. Execute the cell and enter your answers as 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 (one at a time). Note that the answer numbers always remain in sync with the question numbers, but the bug shows up when the question-answer pairs aren't always being displayed to the console after providing the answer number. For example:

Here I'm about to enter 3 as an answer after having entered "0", "1", and "2"; Only "0" and "1" have flushed:
image

Here I just entered 3 and am about to enter 4 and all 4 previous response ("0", "1", "2", "3") have flushed:
image

Note that this issue doesn't occur on IDEs such as JupyterLab and Jupyter Notebooks, only on VS Code Jupyter Notebooks.

Solution/Workaround:
The solution/workaround was to add a sys.stdout.flush() before prompting the next time around, as shown below:
image

It would be good however for VS Code to not do this buffering, especially since JupyterLab and Jupyter Notebook don't run into this issue.

Metadata

Metadata

Assignees

Labels

bugIssue identified by VS Code Team member as probable bugnotebook-output

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions