#745·lipgloss

Windows: HasDarkBackground blocks forever when stdin is not a console (CONIN$ fallback is not cancellable)

Author: keppyCreated Sep 19, 2026Updated Sep 20, 2026

What happens

On Windows, lipgloss.HasDarkBackground (and BackgroundColor) never returns when os.Stdin is not a console. The query's own 2-second timeout does not fire, so the call blocks for the life of the process.

Why

BackgroundColor's Windows branch substitutes the console whenever either stream is not a terminal:

go
if !term.IsTerminal(in.Fd()) {
    f, err := os.OpenFile("CONIN$", os.O_RDWR, 0o644)
    if err != nil { ... }
    in = f
}

queryTerminal then builds its cancelable reader with uv.NewCancelReader(in). ultraviolet returns its cancellable conInputReader only when the file is os.Stdin and GetConsoleMode succeeds:

go
if f, ok := r.(cancelreader.File); !ok || f.Fd() != os.Stdin.Fd() ||
    windows.GetConsoleMode(windows.Handle(f.Fd()), &dummy) != nil {
    return fallback(r)   // cancelreader.fallbackCancelReader
}

Handed the freshly opened CONIN$, it takes the fallback. That reader's own doc comment says it "does not actually support cancelation during an ongoing Read() call. Thus, Cancel() always returns false" — it only makes subsequent reads return ErrCanceled. The timeout goroutine therefore fires into a no-op and the read stays parked in syscall.ReadConsole forever.

#635 is the same bug on the Unix side and was fixed by #636, which added

go
if !term.IsTerminal(in.Fd()) || !term.IsTerminal(out.Fd()) {
    return nil, fmt.Errorf("input/output is not a terminal")
}

— but that guard sits after the Windows branch's return backgroundColor(in, out), so Windows still substitutes CONIN$ and blocks. (Likely related to #73 as well.)

Repro

go
package main

import (
	"fmt"
	"os"

	lipgloss "charm.land/lipgloss/v2"
)

func main() {
	fmt.Println(lipgloss.HasDarkBackground(os.Stdin, os.Stdout))
}

On Windows 11, from a session that has a console, with stdin redirected:

$ ./probe < /dev/null     # never returns

No TTY needed, no CI needed. The console must exist for CONIN$ to open — a process with no console takes the error path instead and is unaffected, which is why this hides on some machines.

Importing compat makes it worse, because the query then runs in a package-level var, before main, where not even a test timeout can interrupt it:

go
package main

import _ "charm.land/lipgloss/v2/compat"   // empty main; still hangs

// compat/color.go:
// var HasDarkBackground = lipgloss.HasDarkBackground(os.Stdin, os.Stdout)

Impact

On Windows, any tool that renders with a terminal-derived theme hangs on every invocation whose stdin is not a console — piped, redirected, NUL, or spawned by another program that captures its output (agent harnesses, editors, CI). Observed as a 90-minute park:

github.com/muesli/cancelreader.(*fallbackCancelReader).Read
charm.land/lipgloss/v2.queryTerminal
charm.land/lipgloss/v2.HasDarkBackground

and in one case as a child process that never exited while the parent waited in WaitForSingleObject(handle, INFINITE).

Versions

  • lipgloss v2.0.5 and v2.0.6 — query.go and compat/color.go are byte-identical between the two, so upgrading does not help.
  • Go 1.26.0 windows/amd64; Git Bash / Windows Terminal console on Windows 11.

Possible directions

  • Give the Windows branch the same guard #636 added on Unix: refuse to substitute, rather than substituting a handle the reader cannot cancel.
  • Or only substitute the console when the substituted handle is one the cancel reader can actually cancel.
  • Or enforce the timeout without depending on Cancel() (query in a goroutine and return on timer expiry).
  • Making compat.HasDarkBackground lazy would also stop programs that never read it from hanging at init.