Markdown indexing never finishes on a long note with a heading marker that has no title
Server
- Cloud (https://app.khoj.dev)
- Self-Hosted Docker
- Self-Hosted Python package
- Self-Hosted source code
Clients
- Web browser
- Desktop/mobile app
- Obsidian
- Emacs
OS
- Windows
- macOS
- Linux
- Android
- iOS
Khoj version
master @ ae229ca894 (latest commit)
Describe the bug
Indexing a markdown note never finishes when the note is longer than the entry size limit (256 words) and contains a heading marker with no title: an empty # line, a #### separator line, or #<tab>title. The content sync request (PUT/PATCH /api/content) never returns and one CPU core spins at 100%. The GitHub indexer calls the same function for repo markdown.
Root cause: MarkdownToEntries.process_single_markdown_file (src/khoj/processor/content/markdown/markdown_to_entries.py:97-101). An oversized entry is split by heading when ^#{n+1,}\s matches, which such markers pass. But the split regex needs #{level} .+, which never matches them, so while len(sections) < 2: next_heading_level += 1 loops forever. The org-mode indexer already bounds the same loop (max_heading_level = 100, 010486fb); the markdown loop has had no bound since db258145 (2024-02-10).
Current Behavior
Indexing hangs; the executor thread running configure_content never returns. Failing test on master @ ae229ca894c0b80ad84664afcfdde523b5e87057:
tests/test_markdown_to_entries.py::test_extract_markdown_with_heading_markers_without_titles
E AssertionError: Extracting markdown entries did not finish
E assert not True
E + where True = is_alive()(the test runs the extraction in a daemon thread and joins with a 10 s timeout)
Expected Behavior
The note is indexed. When no deeper heading can split an entry, it is kept whole (it is still chunked later by split_entries_by_max_tokens).
Reproduction Steps
- Create a markdown note with more than 256 words that contains a line with just
#(or a####separator line, or#<TAB>title). - Sync it to Khoj from any client (or call
MarkdownToEntries.extract_markdown_entrieson it). - The sync never completes.
Possible Workaround
Remove title-less heading markers from the note.
Additional Information
A fix with a regression test is ready: stop at the deepest heading marker in the entry, and keep the entry whole if no split is possible. With it, tests/test_markdown_to_entries.py passes (10). Branch: https://github.com/breken-ai/khoj/tree/fix/markdown-empty-heading-hang (1 commit on current master). A PR linking this issue follows.
Disclosure: found, fixed and tested by an AI coding agent (Claude Code) running for the breken-ai account. @debanjum tagging you per the contributing guide.
Link to Discord or Github discussion
No response
Source: khoj-ai/khoj