Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 8, 2025

When executing notebook cells containing input() functions, stdout was not being flushed consistently before prompting for user input. This caused print statements and prompts to remain buffered, making it difficult for users to see what they needed to respond to.

Problem

Consider this common interactive notebook pattern:

while True:
    text = input("Enter some text: ")
    if text == '':
        print("Goodbye")
        break
    print(f"You entered {text}")

Before this fix, the output would be inconsistent - sometimes users would see the prompt immediately, other times the output would remain buffered until after they provided input, making the notebook confusing to use.

Solution

This PR implements a startup code provider that automatically monkey-patches Python's built-in input() function when Python kernels start. The patched function:

  1. Calls sys.stdout.flush() to ensure all pending output is displayed
  2. Calls the original input() function with all arguments preserved
  3. Returns the result unchanged
  4. Handles any flush errors gracefully without interrupting functionality

The monkey patch is installed via kernel startup code that runs early in the kernel initialization process:

import builtins
import sys

def __vscode_input_with_flush(*args, **kwargs):
    try:
        sys.stdout.flush()
    except:
        pass
    return __vscode_original_input(*args, **kwargs)

__vscode_original_input = builtins.input
builtins.input = __vscode_input_with_flush

Implementation

  • InputFlushStartupCodeProvider: New startup code provider that injects the monkey patch
  • Service Registration: Registered for both Node.js and Web environments
  • Python-only: Only applies to Python kernels, no impact on other kernel types
  • Safe: Preserves all original input() functionality and handles errors gracefully
  • Automatic: No user configuration required - works transparently

Testing

  • Comprehensive unit tests verify the provider works correctly for Python kernels
  • Direct Python validation confirms the monkey patch behavior
  • Existing regression tests pass without issues
  • Manual testing with interactive notebook scenarios confirms the fix works

Now users will consistently see all print output before being prompted for input, making interactive notebooks much more usable.

Fixes #13039.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI changed the title [WIP] Calls to input() should flush stdout when executing cells Fix stdout flushing for input() calls in notebook cells Sep 8, 2025
@Copilot Copilot AI requested a review from DonJayamanne September 8, 2025 01:18
Copilot finished work on behalf of DonJayamanne September 8, 2025 01:18
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.

Calls to input() should flush stdout when executing cells
2 participants