fix(handler): prevent busy-loop / 100% CPU in interactive mode#563
Open
GourangaDasSamrat wants to merge 1 commit intoxo:mainfrom
Open
fix(handler): prevent busy-loop / 100% CPU in interactive mode#563GourangaDasSamrat wants to merge 1 commit intoxo:mainfrom
GourangaDasSamrat wants to merge 1 commit intoxo:mainfrom
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Runloop'sswitchhad nodefaultcase, this caused a tightbusy-loop pegging CPU at 100% (see #546).
Fix
Add a
defaultcase to theswitchstatement inhandler/handler.gothat 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
iactiveguard ensures non-interactive (scripted/piped) sessionsare unaffected.
Testing
Run
usql_staticin an affected Linux environment and confirm CPU usagestays near 0% at the idle prompt.
CI Note
The
Shell Testsstep failure (go run testcli.go) is a pre-existing issueunrelated 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) andBuild with all driverssteps passcleanly. This change only touches
handler/handler.goand introduces no newdependencies or build tags.