Skip to content

Studio: stop a linked folder re-indexing failed files forever and let Unlink stop a running sync - #13145

Merged
danielhanchen merged 12 commits into
unslothai:mainfrom
indrajeetapache:studio/linked-folder-cancel-under-lock
Oct 10, 2026
Merged

danielhanchen merged 12 commits into
unslothai:mainfrom
indrajeetapache:studio/linked-folder-cancel-under-lock

Conversation

@indrajeetapache

@indrajeetapache indrajeetapache commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Two backend problems kept a linked folder of scanned PDFs (#13095) indexing forever, with no working way out:

  1. A file that fails to index was retried on every periodic sync. Failed files never get a mapping, so the 30 s auto-sync treated them as new each time and parsed them again. A folder with scanned PDFs and no OCR re-parsed the same files around the clock, so the spinner never stopped. The folder now remembers which files failed to ingest (size, mtime, inode). A periodic sync reports them as failed again without re-parsing them until the file changes, the user clicks Sync changes or Rebuild index, or 6 hours pass, which covers a temporary embedder failure. The folder still shows the same "N file(s) could not be indexed" error.
  2. Unlink could not stop a running sync. Unlink retired the folder and then waited for the document being embedded to finish, which can take minutes for a large PDF. While it waited, it hit database is locked and returned a 500, and the folder came back. Unlink now sets a process-local cancel flag that the sync worker and the embedding progress callback check without touching rag.db. The retire write waits up to 30 s instead of 5 s.

Unlink and keep indexed files therefore works as the stop for a long first sync, and it keeps everything already indexed. There are no UI changes.

Evidence

Reproduced on main (fdaa9dc) with the real linked-folder worker and real JPEG2000 scanned PDFs (no Tesseract):

check main this PR
ingest attempts of one unchanged failing scanned PDF over 40 s (sync interval 1 s) 34 1
Unlink while a large document is embedding 29.6 s, then sqlite3.OperationalError: database is locked 1.8 s, folder removed

New tests, base vs head (test sources kept from head):

  • test_periodic_sync_does_not_retry_an_unchanged_failed_file: fails on main, passes here
  • test_unlink_stops_the_document_being_embedded: fails on main, passes here

tests/test_rag_linked_folders.py: 123 passed. RAG backend tests: 1572 passed. The only 3 failures are in test_amd_smi_hip_index_and_coverage.py, and they fail the same way on main.

Fixes #13095

Unlink and Stop could not land while ingest held the writer past the
5s busy_timeout (unslothai#13095). Signal cancel in-process first, retry the
retire write, and expose Stop indexing in the running-folder menu.
@danielhanchen danielhanchen self-assigned this Oct 9, 2026
@danielhanchen danielhanchen changed the title Studio: stop linked-folder indexing under a held rag.db lock Studio: stop a linked folder re-indexing failed files forever and let Unlink stop a running sync Oct 10, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-10T02:33:45.761450Z d9a57b3 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

Copy link
Copy Markdown

Requested correction for #13095: an intentional Stop/Cancel/Unlink should be a normal terminal cancellation, not a failure or exit-code-1 error. Keep actual indexing/database failures visible.

At head e0c1ce3, _stop_job() still sets status="failed", stage="error", and error="Indexing was stopped". delete_folder() also changes pending/running sync jobs to failed/error with Linked folder was removed. Please change these intentional-cancellation paths to a supported terminal cancelled state with no error, updating _TERMINAL, pruning/queue logic, API status types and frontend handling consistently. Preserve the separate pause/restart behavior for application shutdown.

The ingestion cancellation currently travels through JobLeaseLost("Linked folder was removed"); please distinguish requested cancellation from genuine lease loss so it does not become a failure downstream. Stop should clean up in-flight work and release leases/database resources while leaving the application running. Unlink with remove_index=False must preserve previously indexed documents.

Regression coverage requested: cancellation during embedding and while the DB writer is busy finishes as cancelled with no error; cancelled jobs are terminal and do not queue successors/restart; keep-index unlink preserves existing documents; a real ingestion exception and exhausted database-lock handling still report failure. If a dedicated subprocess exits after successful cancellation cleanup, it should not use exit 1 for that intentional action.

This is a requested follow-up correction; I have not modified or run tests on this contributor branch.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 190a3da27f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread studio/backend/core/rag/folder_sync.py Outdated

def request_cancel(folder_id: str) -> None:
with _folder_cancels_lock:
_folder_cancels.add(folder_id)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Propagate unlink cancellation between processes

When two backend processes share rag.db (the mode the lease layer is written to support), an unlink handled in one process only adds the folder ID to that process's module-local _folder_cancels. A sync leased by another process therefore never sees _SyncCancelled in either _check_running or ingestion._progress; delete_folder then waits in _delete_retired_folder for the foreign lease, so unlink still blocks for the full long parse/embed. Use a cross-process cancellation marker or make ingestion observe the persisted folder retirement.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlink against another process's sync lease waits for that lease exactly as it does on main (test_unlink_waits_for_a_foreign_sync_lease pins this). The cancel flag only shortens the common single-backend case; cross-process signalling is out of scope here.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 727149a5e9

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +288 to +289
if folder_cols and "failed_files" not in folder_cols:
conn.execute("ALTER TABLE linked_folders ADD COLUMN failed_files TEXT")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Serialize the failed_files migration across processes

When two backend processes open an existing pre-change rag.db concurrently, both can read folder_cols before either ALTER commits; after one adds the column, the other's identical statement wakes and raises sqlite3.OperationalError: duplicate column name: failed_files. The process-local schema lock cannot serialize processes (and metadata initialization uses a different lock), so one initialization or request can fail during rollout; acquire a database write lock before inspecting the schema or tolerate and recheck this duplicate-column race.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same lazy ALTER pattern as withheld_paths and successor_kind in this function; a second process losing that one-time race fails a single connection and the next one sees the column. Not changing the migration scheme here.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Something went wrong. Try again later by commenting “@codex review”.

Unknown error
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ba283362a1

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

"UPDATE linked_folders SET failed_files=CASE WHEN EXISTS(SELECT 1 FROM "
"linked_folder_sync_jobs WHERE id=? AND successor_kind IS NOT NULL) THEN NULL ELSE ? END "
"WHERE id=?",
(job_id, json.dumps(ingest_failed) if ingest_failed else None, folder["id"]),

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Exclude the failure cache from folder-list polling

When an allowed 10,000-file folder has many persistent failures (the scanned-PDF case this cache targets), this serializes every path and signature into one potentially multi-megabyte linked_folders.failed_files value. list_folders() still executes SELECT f.* in studio/backend/core/rag/folder_sync.py:452-456, while useLinkedFolders calls that endpoint every four seconds in studio/frontend/src/features/rag/components/use-linked-folders.ts:253-255; _folder_view discards the field only after SQLite and Python materialize it. Thus merely leaving Sources open repeatedly reads and allocates the entire cache, so omit failed_files from list queries (or normalize it) to keep polling cost independent of the number of failed paths.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing: an entry is ~120 bytes, so the reporter's folder is a few KB, and even a 10,000-failure folder is about 1 MB read by a 4 s poll, against main re-parsing all of those files every 30 s. Listing explicit columns to drop it would add a second place to keep in sync with the schema.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
chatgpt-codex-connector[bot]

This comment was marked as resolved.

chatgpt-codex-connector[bot]

This comment was marked as resolved.

@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@unslothai unslothai deleted a comment from chatgpt-codex-connector Bot Oct 10, 2026
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: d9a57b3f37

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] No option to stop, pause or cancel embedding of linked folder once it has begun and the process is now stuck indefinitely

3 participants