Skip to content

Code highlighting failed, falling back to plain text warning logged on exit (destroy race) #1319

Description

@HARIOM-JHA01

Summary

When an app using @opentui/core exits, TreeSitterClient.destroy() rejects any in-flight highlightOnce() requests. CodeRenderable treats those rejections as genuine highlighting failures and logs a misleading warning:

Code highlighting failed, falling back to plain text: Error: TreeSitter client destroyed

This occurs during normal shutdown, so nothing is actually wrong. As a result, applications such as opencode print this warning on every exit (see anomalyco/opencode#36454).

Reproduction

  1. Run any OpenTUI app that renders a CodeRenderable (or markdown containing code).
  2. Exit the application while syntax highlighting is still in progress (e.g. Ctrl+C or /exit).
  3. Observe the warning printed after teardown.

Root Cause

During shutdown, TreeSitterClient.destroy() rejects all pending highlight requests with:

new Error("TreeSitter client destroyed")

CodeRenderable.startHighlight() catches every rejected highlightOnce() call and assumes it represents a real highlighting failure, logging:

console.warn(
  "Code highlighting failed, falling back to plain text:",
  error,
);

However, in this case the rejection is an expected consequence of teardown, not an actual error.

Expected Behavior

Shutdown-related cancellations should be treated as expected and should not:

  • emit a warning, or
  • trigger the normal highlighting failure path.

Actual highlighting failures (for example, parser initialization or worker errors) should continue to log warnings so genuine issues remain visible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions