Skip to content

fix(handler): prevent busy-loop / 100% CPU in interactive mode#563

Open
GourangaDasSamrat wants to merge 1 commit intoxo:mainfrom
GourangaDasSamrat:fix/busy-loop-cpu-100
Open

fix(handler): prevent busy-loop / 100% CPU in interactive mode#563
GourangaDasSamrat wants to merge 1 commit intoxo:mainfrom
GourangaDasSamrat:fix/busy-loop-cpu-100

Conversation

@GourangaDasSamrat
Copy link
Copy Markdown
Contributor

@GourangaDasSamrat GourangaDasSamrat commented Mar 26, 2026

Problem

In certain environments — particularly static builds on Linux — the
readline library can stop blocking on input and instead return
immediately with no data, no error, and no command. Because the main
Run loop's switch had no default case, this caused a tight
busy-loop pegging CPU at 100% (see #546).

Fix

Add a default case to the switch statement in handler/handler.go
that sleeps for 1 millisecond when the session is interactive:

default:
    if iactive {
        time.Sleep(time.Millisecond)
    }

The 1ms yield is imperceptible to the user but eliminates the spin-loop.
The iactive guard ensures non-interactive (scripted/piped) sessions
are unaffected.

Testing

Run usql_static in an affected Linux environment and confirm CPU usage
stays near 0% at the idle prompt.

CI Note

The Shell Tests step failure (go run testcli.go) is a pre-existing issue
unrelated to this change — it requires live database connections (Postgres,
MySQL, SQL Server, Cassandra) that are not available in this CI context. The
Unit Tests (go test -v ./stmt) and Build with all drivers steps pass
cleanly. This change only touches handler/handler.go and introduces no new
dependencies or build tags.

…loop

In some environments, particularly static builds on Linux, the readline
library may return immediately with no input, no error, and no command.
Without a default case in the switch statement, this causes the main
run loop to spin at 100% CPU.

Add a default case that sleeps for 1ms when running interactively,
yielding the CPU when readline returns an empty result.

Fixes xo#546
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.

1 participant