Skip to content

fix(knowledge): stop holding the connector table lock across the processing commit - #8191

Merged
waleedlatif1 merged 3 commits into
stagingfrom
fix/process-document-connector-lock-scope
Sep 23, 2026
Merged

waleedlatif1 merged 3 commits into
stagingfrom
fix/process-document-connector-lock-scope

Conversation

@waleedlatif1

@waleedlatif1 waleedlatif1 commented Sep 23, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • The document processing commit transaction started with a SELECT ... FOR UPDATE that checked the connector with an EXISTS on knowledge_connector. That read held AccessShareLock on knowledge_connector until commit, through every embedding and search-index write. When index writes are slow (tens of seconds under I/O pressure), a steady stream of these transactions keeps the table locked, and ALTER TABLE knowledge_connector cannot get its lock within lock_timeout
  • The opening FOR UPDATE now reads only the document row. The connector check, plus a new knowledge-base check, runs in the completion UPDATE at the end. knowledge_connector is now only locked from that statement until commit
  • knowledge_base is still locked for the whole pass, through the embedding foreign key and the projection triggers. That is out of scope here
  • The completion UPDATE now returns its matched rows. If it matched none, meaning the connector or knowledge base went inactive during the pass, the whole pass rolls back and returns superseded. Before this change, the embeddings committed while the document stayed processing
  • A cheap autocommit pre-check of connector and KB activity runs just before the transaction. If either went inactive after the claim, the pass skips the index writes and the rollback. Because the pre-check is its own statement, it releases its locks immediately. The in-transaction completion check stays the authoritative guard
  • Diff is small with ?w=1. Most of the stat is re-indentation from the .catch on the transaction

Type of Change

  • Bug fix

Testing

  • New processing-lock-scope.integration.ts (real Postgres), 5 tests, added to the knowledge integration step in test-build.yml:
    • a statement-level AFTER INSERT probe on embedding (fires after the row triggers and FK checks) fails if the backend holds any lock on knowledge_connector
    • deleting the connector or the KB while the embeddings are being written rolls back everything: embeddings, both search projections, and document state
    • deleting the connector or the KB after the claim skips the index writes entirely
  • Mutation-checked:
    • restoring the connector EXISTS in the opening select fails the lock probe
    • removing the pre-check fails both pre-check tests
    • removing the zero-row rollback fails the rollback tests
    • the staging version fails all 5
  • Unit mocks now arm the extra pre-check read. Knowledge unit tests (3200) and related integration suites (81 tests across 10 files, fresh migrated DB) pass, as do type-check, biome, and check:audits

Checklist

  • Code follows project style guidelines
  • Self-reviewed my changes
  • Tests added/updated and passing
  • No new warnings introduced
  • I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

@vercel

vercel Bot commented Sep 23, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated
docs Skipped Skipped Sep 23, 2026 5:55am UTC

Request Review

@greptile-apps

greptile-apps Bot commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge, with no outstanding correctness, security, or repository-rule issues identified.

Summary

This PR shortens the period for which document processing transactions lock knowledge_connector.

  • Moves connector and knowledge-base activity checks out of the opening row lock and into the authoritative completion update.
  • Rolls back generated embeddings and search projections when the completion guard no longer matches.
  • Adds an autocommit pre-check to avoid unnecessary index writes for inactive sources.
  • Adds PostgreSQL integration coverage for lock scope, rollback behavior, and pre-check behavior.
  • Includes the new integration suite in CI and updates unit-test database mocks for the additional read.
Diagram
sequenceDiagram
  participant Worker
  participant DB as PostgreSQL
  participant Index as Embedding/Search Writes

  Worker->>DB: Autocommit source activity pre-check
  DB-->>Worker: Active / inactive
  alt Source inactive
    Worker-->>Worker: Return superseded
  else Source active
    Worker->>DB: Begin transaction
    Worker->>DB: Lock active document row only
    Worker->>Index: Replace embeddings and projections
    Worker->>DB: Completion UPDATE with connector and KB guards
    alt Completion matched
      DB-->>Worker: Commit indexed output
    else Source became inactive
      DB-->>Worker: No rows returned
      Worker->>DB: Roll back transaction
      Worker-->>Worker: Return superseded
    end
  end
Loading

Reviews (3) · Last reviewed commit: "test(knowledge): arm the pre-commit sour..."

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 2 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/lib/knowledge/__integration__/processing-lock-scope.integration.ts Outdated
@waleedlatif1 waleedlatif1 changed the title fix(knowledge): check connector and knowledge base at the end of the processing commit fix(knowledge): stop holding the connector table lock across the processing commit Sep 23, 2026
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All reported issues were addressed across 6 files

Reply with feedback, questions, or to request a fix.

Fix all with cubic | Re-trigger cubic

Comment thread apps/sim/lib/knowledge/documents/service.ts
@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@greptile

@waleedlatif1

Copy link
Copy Markdown
Collaborator Author

@cubic-dev-ai review this PR

@cubic-dev-ai

cubic-dev-ai Bot commented Sep 23, 2026

Copy link
Copy Markdown
Contributor

@cubic-dev-ai review this PR

@waleedlatif1 I have started the AI code review. It will take a few minutes to complete.

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 6 files

Confidence score: 5/5

  • Automated review surfaced no issues in the provided summaries.
  • No files require special attention.

Re-trigger cubic

@waleedlatif1
waleedlatif1 merged commit 22ff39f into staging Sep 23, 2026
34 checks passed
@waleedlatif1
waleedlatif1 deleted the fix/process-document-connector-lock-scope branch September 23, 2026 06:20

This branch was previously deployed

1 inactive deployment
Preview — 7188b96e Deployed Sep 23, 2026 by vercel[bot]
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.

1 participant