Skip to content

Fix keyboard input latency (~100ms per keystroke) from VMIN=3/VTIME=1 - #63

Merged
grossmj merged 1 commit into
GNS3:masterfrom
yueguobin:fix/readline-input-latency
Jul 31, 2026
Merged

Fix keyboard input latency (~100ms per keystroke) from VMIN=3/VTIME=1#63
grossmj merged 1 commit into
GNS3:masterfrom
yueguobin:fix/readline-input-latency

Conversation

@yueguobin

@yueguobin yueguobin commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Problem

Typing in the VPCS console feels sluggish: each ordinary keystroke is delayed by roughly 100ms.

Root cause

set_terminal() puts the terminal in non-canonical mode with VMIN=3 and VTIME=1 (interbyte-timer mode). This was introduced in 03baea1 ("arrow keys is OK in cygwin") so that a 3-byte escape sequence such as an arrow key (\e[A) would be returned by a single read().

With VMIN=3, read() blocks until it has collected 3 bytes or the interbyte timer (VTIME=1 = 100ms) expires with no new byte arriving. A normal key produces only 1 byte, so every keystroke has to wait for the 100ms timeout before it is returned — hence the ~0.1s-per-character lag. (Escape sequences happen to be 3 bytes, which is why arrow keys did not feel laggy while ordinary typing did.)

Fix

  • set_terminal(): switch to VMIN=1, VTIME=0 so each byte is returned immediately.
  • Add read_escape_tail(): after a lone ESC, drain any trailing bytes arriving within a short select() window (50ms for the first byte, 10ms for subsequent ones), so multi-byte sequences (arrow keys, Home/End, ...) are still parsed as a single key instead of leaking their control bytes ([, A, ...) into the input line.

This removes the per-character delay while preserving the arrow-key / Home / End / history behavior — the original reason VMIN was raised.

Verification

  • Builds cleanly with gcc -O2 -Wall; with -Wextra there are no new warnings (the few -Wextra warnings are pre-existing and unrelated).
  • PTY smoke test: feeding \x1b[A (Up arrow) under VMIN=1 is recognized as a single key and does not leak [/A into the input line.
  • Manual testing confirms arrow keys, Home/End, backspace and history navigation still work.

set_terminal() used VMIN=3 + VTIME=1 (interbyte-timer mode), introduced
in 03baea1 to make arrow keys work under Cygwin. With VMIN=3, a single
ordinary keystroke must wait for the 100ms interbyte timeout before
read() returns, so typing feels sluggish (~0.1s per character).

- set_terminal: switch to VMIN=1/VTIME=0 so each byte returns immediately
- read_escape_tail(): after a lone ESC, drain trailing bytes within a
  short select() window so multi-byte sequences (arrow keys, Home/End)
  are still parsed as a single key instead of leaking control bytes
  ('[', 'A', ...) into the input line
@grossmj
grossmj merged commit a69dc36 into GNS3:master Jul 31, 2026
3 checks passed
@yueguobin
yueguobin deleted the fix/readline-input-latency branch August 1, 2026 02:01
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.

2 participants