Skip to content

Closing stdin by pressing Ctrl+D, is a thing you can do during readline #30790

@oxinabox

Description

@oxinabox

This has been bothing me for a little while,
This is basically a regression of #1306

Consider the following example code
(It need to be in a block. running each line on a seperate repl prompt will not do this)

begin
       x = readline()
       y = readline()
       @show (x,y)
end

If you press Control+D to sent a EOF to the first readline() then all subsequent calls to readline()will return an empty string. So the output will be(x, y) = ("", "")`

Presumably what is happening is the stdin stream is beng closed by the EOS character.
And reading a line from a closed stream returns "".

This is particularly annoying if you are doing a loop waiting for user input.
E.g. somethilg like

function ask()
   while (true)
    println("Y/N?")
    ans = readline()
    ans == "Y" && return true
    ans == "N" && return false
  end
end

I believe this is why DataDeps tests when a package had not setup the right enviroment variables
would flood the CI with Gigabytes of prompts.
Because CI systems have a closed stdin
( @andreasnoack )

I see a few resolutions, both require special casing the stdin stream

  1. Calling readline on stdin, if the stdin stream is closed could throw an error
  2. Pressing Ctrl+D in response to to readline, could not cause stdin to be closed

The behavour of Pressing Ctrl+D causing StdIn to close is not documented.
The current behavour of readline on a closed stream is not documented.
which does open the option of documenting this behavour and explaining a work-around.

begin
       x = readline()
       isopen(stdin) || error()
       y = readline()
       isopen(stdin)||errror()
       @show (x,y)
 end

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsThis change adds or pertains to documentationioInvolving the I/O subsystem: libuv, read, write, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions