Skip to content

os/signal: ignoring SIGTTIN and reading os.Stdin.Read fails badly #5572

@gopherbot

Description

@gopherbot

by viriketo:

On linux x86_64, go 1.1:

I want a process to be able to read on Stdin, but if set to background, I want it not
stopped.

Currently, os.Stdin.Read() goes into infinite loop, if I ignore the SIGTTIN signal
received when reading on a tty while the process is in background.

os.Stdin.Read() calls the read() syscall; if the process is in background, it's
interrupted with ERESTART and the process receives SIGTTIN; that should stop the
process. If I ignore the signal, the process doesn't stop, but os.Stdin.Read() calls
read() in a loop due to ERESTART, getting SIGTTIN again, and so on (max cpu usage).

How can I make a process that doesn't stop when sent to background, if I want it also to
be able to read stdin (when foreground)?

Code to reproduce it (run with &):
-----------------
package main

import (
        "os"
        "os/signal"
        "syscall"
        )

func main() {
        ignore := make(chan os.Signal)
        signal.Notify(ignore, syscall.SIGTTIN)

        v := make([]byte, 10)
        os.Stdin.Read(v)
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions