Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: c9b3224543579416e7805b4e2cae76f18e66dab6ed39b553cf9cfca59b04b8d9
Ticket: 1d0618000674abdf701021d69eaa858cbccf14a4
channel reads prematurely from stdin on Windows
User & Date: mdehoon on 2025-11-08 01:26:41
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-plain"
  4. comment changed to:
    The code pasted below works correctly on Linux and Mac, printing the
    line that was input by the user.
    
    On Windows however, by the time `StdinCallback` is called, the data
    from stdin have already been read. and the program ends up waiting in
    the call to `read`.
    
    Going through the program step by step, it seems that the channel,
    when checking if data is available on stdin, already reads the data
    from stdin into its internal buffer. I would expect that at that stage,
    Tcl just checks if data is available, but does not read it yet.
    
    If the program uses `Tcl_Read*` instead of `read` to read from stdin,
    then there is no problem, as `Tcl_Read*` can get the data from the
    internal buffer.
    
    But there are situations where `Tcl_Read*` cannot be used.
    
    One example is tkinter (the glue code between Python and Tcl/Tk).
    There, we would like to use Tcl_CreateChannelHandler in the
    Tcl_DoOneEvent loop to monitor for input on stdin. If input is
    present, then we exit the loop and return control to Python. Python
    does not have access to the Tcl API, and tries to read the input
    with `fgets`. This works on Unix/Mac without problems, but on Windows
    it fails to read the data from stdin as they have already been read
    into the channel's internal buffer.
    
    I am happy to look further into this issue, but I wanted to confirm
    first if this is intended or unavoidable behavior of channels on Windows.
    
    ---------
    #include <tcl.h>
    #include <stdio.h>
    #include <unistd.h>
    
    void StdinCallback(ClientData clientData, int mask) {
        char buffer[16];
    
        // Read a line from stdin
        int n;
        n = read(STDIN_FILENO, buffer, sizeof(buffer) - 1);
        if (n == 1) Tcl_Exit(0);
        if (n > 0) {
            buffer[n] = '\0';
            printf("Got input: %s\n", buffer);
            fflush(stdout);
        }
    }
    
    int main(int argc, char *argv[]) {
        Tcl_Interp *interp = Tcl_CreateInterp();
    
        if (Tcl_Init(interp) != TCL_OK) {
            fprintf(stderr, "Tcl_Init failed: %s\n", Tcl_GetStringResult(interp));
            return 1;
        }
    
        Tcl_Channel in = Tcl_GetStdChannel(TCL_STDIN);
        if (!in) {
            fprintf(stderr, "No stdin channel available\n");
            return 1;
        }
    
        Tcl_CreateChannelHandler(in, TCL_READABLE, StdinCallback, NULL);
    
        printf("Type something and press Enter:\n");
    
        // Run the event loop
        while (1) {
            Tcl_DoOneEvent(0);
        }
    
        return 0;
    }
    -----------
    
  5. foundin changed to: "all"
  6. is_private changed to: "0"
  7. login: "mdehoon"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "500c9a7a4e11b3ea0441850610e04006941c0cf3"
  10. resolution changed to: "None"
  11. severity changed to: "Important"
  12. status changed to: "Open"
  13. submitter changed to: "mdehoon"
  14. subsystem changed to: "01. Notifier"
  15. title changed to: "channel reads prematurely from stdin on Windows"
  16. type changed to: "Bug"