locustio/locust · warning · InitError

Terminal was not a tty. Keyboard input disabled

Error message

Terminal was not a tty. Keyboard input disabled

What it means

The non-Windows keyboard listener needs to put the terminal in cbreak mode, which requires stdin to be a TTY. If `sys.stdin.isatty()` is false it raises InitError, disabling keyboard input (e.g. the web-UI quit hotkey listener).

Source

Thrown at locust/input_events.py:38

    )
else:
    import select
    import termios
    import tty


class InitError(Exception):
    pass


class UnixKeyPoller:
    def __init__(self):
        if sys.stdin.isatty():
            self.stdin = sys.stdin.fileno()
            self.tattr = termios.tcgetattr(self.stdin)
            tty.setcbreak(self.stdin, termios.TCSANOW)
        else:
            raise InitError("Terminal was not a tty. Keyboard input disabled")

    def cleanup(self):
        termios.tcsetattr(self.stdin, termios.TCSANOW, self.tattr)

    def poll(_self):
        dr, dw, de = select.select([sys.stdin], [], [], 0)
        if not dr == []:
            return sys.stdin.read(1)
        return None


class WindowsKeyPoller:
    def __init__(self):
        if sys.stdin.isatty():
            try:
                self.read_handle = GetStdHandle(STD_INPUT_HANDLE)
                self.read_handle.SetConsoleMode(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT | ENABLE_PROCESSED_INPUT)
                self.cur_event_length = 0

View on GitHub (pinned to f391a716e1)

Solutions

  1. Run locust in a real terminal (TTY) if keyboard input is needed
  2. If no TTY is available, this InitError is expected/benign — run headless (`--headless`) or ignore the listener error
  3. In Docker use `docker run -it` when interactive key events are desired

Example fix

// before
subprocess.run("locust", shell=True)  # stdin is a pipe
// after
import pty, os
subprocess.run("locust", shell=True, stdin=os.open("/dev/tty", os.O_RDONLY))
# or run headless: locust --headless
Defensive patterns

Strategy: try-catch

Validate before calling

import sys
if not sys.stdin.isatty():
    print("No TTY: keyboard input listener unavailable; run --headless")

Type guard

def has_tty() -> bool:
    import sys
    return sys.stdin.isatty()

Try / catch

from locust.input_events import BaseListener
try:
    listener = GKeyboardListener()
except InitError:
    listener = None  # no keyboard input in this environment

Prevention

When it happens

Trigger: Running locust with stdin redirected from a file or pipe; running under a process manager/cron/CI without a terminal; detaching stdin via nohup.</br>

Common situations: `locust --web ... < /dev/null` or service wrappers without a TTY; Docker containers run without -t; CI pipelines starting the web UI.

Related errors


AI-assisted analysis of locustio/locust@f391a716e1 (2026-08-29). Data as JSON: /api/errors/faac66ddadcb7faa. Report an issue: GitHub.