harlequin: Quitting with a non-empty buffer causes hang on restart

Before Proceeding, please acknowledge:

  • I have consulted the Troubleshooting guide.
  • I have searched Issues and Discussions in this repo.
  • Feature requests should be initiated as Discussions. This is a bug report.

Describe the bug If the editor is closed with the quit keybinding with a non-empty buffer, future restarts of harlequin hang. I think this is cache related as deleting the harlequin cache directory fixes this.

To Reproduce

  • Start harlequin
  • Type something in the editor
  • Quit (^q or otherwise)
  • Start harlequin

Expected behavior Harlequin should run normally on second invocation.

Actual behavior Harlequin hangs on second invocation.

Contributing Are you interested in contributing a fix?

  • Yes
  • Maybe
  • No

Additional context I can load the cache locally after stopping harlequin using the load_cache() method, so the cache isn’t corrupted. If I use the methods to clear the editor cache, harlequin also behaves normally.

What is the output of harlequin --version?

What database adapter are you using with Harlequin? (Default is duckdb)

This occurs with duckdb and postgres.

What other options are you using when invoking Harlequin? (If you are using a profile, please add relevant items from your profile here).

Just postgres connection details & limits for duckdb.

Can you tell us more about your system?

  • Shell: Zsh
  • Terminal: Alacritty
  • OS of the shell: Void Linux (also seen on Arch Linux)
  • OS of the terminal (if different from the shell): -
  • Default locale / language for your OS/Terminal/Shell: en_GB.UTF-8

[!TIP] For example, for my system, these are:

  • Bash
  • Windows Terminal
  • Ubuntu 22.04 / WSL2
  • Windows 11
  • en_US/UTF-8

How did you install Harlequin?

  • pipx
  • conda/mamba
  • pip (global install)
  • pip + venv/virtualenv or pipenv
  • Poetry
  • uv/Rye
  • other: _____________

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 1
  • Comments: 19 (10 by maintainers)

Most upvoted comments

I have a branch with the upgrade, and can confirm it fixes this issue; unfortunately it introduces a few new ones, and it’ll take me some time to iron that out. (Time I don’t have today)

I can also reliably reproduce this with uv / pipx installed Harlequin on MacOS. The thing I noticed, is it’s not deadlocking, it’s hanging with 100% CPU. I used py-spy to get a thread dump which shows it’s spinning in this function:

Thread 0x1F4090F40 (active+gil): "MainThread"
    _split_cells (rich/segment.py:136)
    split_cells (rich/segment.py:178)
    crop (textual/strip.py:412)
    render_line (textual/widgets/_text_area.py:1222)
    render_line (textual/_styles_cache.py:441)
    render (textual/_styles_cache.py:218)
    render_widget (textual/_styles_cache.py:119)
    render_lines (textual/widget.py:3420)
    _get_renders (textual/_compositor.py:974)
    _render_chops (textual/_compositor.py:1126)
    render_partial_update (textual/_compositor.py:1082)
    render_update (textual/_compositor.py:1024)
    _compositor_refresh (textual/screen.py:823)
    _refresh_layout (textual/screen.py:983)
    _on_timer_update (textual/screen.py:842)
    _invoke (textual/_callback.py:45)
    invoke (textual/_callback.py:85)
    _tick (textual/timer.py:185)
    _run (textual/timer.py:177)
    _run_timer (textual/timer.py:148)
    _run (asyncio/events.py:80)
    _run_once (asyncio/base_events.py:1909)
    run_forever (asyncio/base_events.py:603)
    run_until_complete (asyncio/base_events.py:636)
    run (asyncio/runners.py:44)
    run (textual/app.py:1630)
    inner_cli (harlequin/cli.py:381)
    new_func (click/decorators.py:33)
    invoke (click/core.py:782)
    invoke (click/core.py:1434)
    main (rich_click/rich_command.py:152)
    __call__ (click/core.py:1157)
    harlequin (harlequin/cli.py:414)
    <module> (harlequin:8)

Rich 13.9.2 was released ~2 weeks ago and mentions a fix to “split_cells”: https://github.com/Textualize/rich/releases/tag/v13.9.2 - suspiciously around the time these reports started coming in.

Downgrading rich to 13.9.1 seems to fix it for me.

[edit] I think this is the problem/fix: https://github.com/Textualize/rich/issues/3532

Yeah, it’s even weirder than that – it can restore the cache if the cache is only whitespace (including newlines). But if you have a selection or non-whitespace characters, the packaged version can’t restore the cache, and just hangs.

@TLipede Pickle was just a convenience.

Given WHEN this is hanging (and what I wrote above), I suspect we’re reading from the cache file fine, and it’s hanging when the CodeEditor/TextArea is being initialized with some text.