René's URL Explorer Experiment


Title: fix(files): lock the workspace row before inserting a workspace file by waleedlatif1 · Pull Request #8972 · simstudioai/sim · GitHub

Open Graph Title: fix(files): lock the workspace row before inserting a workspace file by waleedlatif1 · Pull Request #8972 · simstudioai/sim

X Title: fix(files): lock the workspace row before inserting a workspace file by waleedlatif1 · Pull Request #8972 · simstudioai/sim

Description: Summary Fixes deadlock detected (40P01) between a file content write and a concurrent new-file insert in the same workspace. It recurs in bursts when a workspace gets concurrent uploads and overwrites, e.g. an agent writing many files. Cycle: updateWorkspaceFileContent locks the workspace row (lockWorkspaceStorageForMutationInTx, FOR NO KEY UPDATE), then UPDATEs the file. The AFTER trigger workspace_files_search_index_pending then upserts the one-row-per-workspace workspace_file_search_dispatch_queue row. uploadWorkspaceFile, registerUploadedWorkspaceFile and fork executeForkFileBlobCopies INSERT the file first, so the trigger takes the queue row. They lock the workspace row only later, in incrementStorageUsageForBillingContextInTx. In prod logs the blocked content write was "inserting index tuple in relation workspace_file_search_dispatch_queue". The transaction blocking it was waiting on select … from workspace … for no key update. Fix: call lockWorkspaceStorageForMutationInTx right before the file insert in those three paths and in createOwnedWorkspaceFile. createOwnedWorkspaceFile uses non-workspace contexts, so the trigger never reaches the queue row today. It gets the same order for consistency. The increment's later re-lock is already held, so it never waits. Documents the canonical order in the TSDoc of lockWorkspaceStorageForMutationInTx: folder advisory lock → workspace row → file rows → file writes (trigger queue row) → payer row. The trigger is unchanged. Making its queue upsert ON CONFLICT DO NOTHING would lose wakeups. Why it's safe No new lock pair with the folder lock. Upload and register already took the folder advisory lock and then the workspace row (via the increment). Only the workspace row moves ahead of the file/queue writes. Every acquireFolderMutationLock / withFolderTreeLock call is the first lock in its transaction. Mapped-import and lockForkRevision take the folder lock before the workspace row. So no transaction holds the workspace row and then waits on the folder lock. The other workspace-row lockers already follow workspace → files: content write, purgeCreatedWorkspaceFile, cleanup-soft-deletes, payer transfer, and fork KB rollback. The other workspace_files inserts don't charge storage in the same transaction, or don't reach the queue row: chat uploads use mothership context. upload finalizers use execution context and have no tx. chat fork copies don't charge storage in that transaction. KB ownership rows use knowledge-base context, which the trigger skips. The dispatcher never locks workspace rows. It claims queue rows with SKIP LOCKED. Lock waits are unchanged. Upload/register wait under the same 5s lock_timeout the folder lock already sets. The lock is taken after folder-path resolution, so the workspace row is held only across the insert and the increment that already followed it. Extra cost: one cheap re-select of an already-held row per insert. Race and replay paths that end up not charging now also briefly hold the workspace row. No behavior, API or schema changes. Type of Change Bug fix Testing New apps/sim/lib/uploads/contexts/workspace/__integration__/storage-lock-order.integration.ts runs against real Postgres, once each for uploadWorkspaceFile and registerUploadedWorkspaceFile: One transaction holds the workspace row the way updateWorkspaceFileContent does. The finalize path starts, and the test polls until it is blocked by that transaction. The holder then bumps content_updated_at on an existing file in the same workspace. Its trigger upserts the queue row the seed upload created. Asserts that both transactions commit. Pre-fix, the test fails (reasoned, not run locally): With the old order, the finalize path has already inserted its row (trigger → queue row lock) before blocking on the workspace row in the increment. The holder's UPDATE then waits on the queue row, which closes the cycle. Postgres aborts one side with 40P01 after deadlock_timeout (1s, well under the 5s lock_timeout), so a settled result is rejected. Post-fix, the finalize path blocks before its insert, the holder's trigger finds the queue row free, and both commit. Ran biome on the changed files. Per repo policy I did not run type-check, check:audits or integration suites locally; CI runs them. Checklist Code follows project style guidelines Self-reviewed my changes Tests added/updated and passing (new tests pass the test-audit authoring gate) No new warnings introduced I confirm that I have read and agree to the terms outlined in the Contributor License Agreement (CLA)

Open Graph Description: Summary Fixes deadlock detected (40P01) between a file content write and a concurrent new-file insert in the same workspace. It recurs in bursts when a workspace gets concurrent uploads and overwr...

X Description: Summary Fixes deadlock detected (40P01) between a file content write and a concurrent new-file insert in the same workspace. It recurs in bursts when a workspace gets concurrent uploads and overwr...

Opengraph URL: https://github.com/simstudioai/sim/pull/8972

X: @github

direct link

Domain: github.com

react-profiling0
react-import-mapreact,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf
route-pattern/:user_id/:repository/pull/:id/files(.:format)
route-controllerpull_requests
route-actionfiles
fetch-noncev2:906c2582-1240-bedd-0959-317c6042edd5
current-catalog-service-hashae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b
request-idCC50:71DFA:909552:CCB74A:6AFB5124
html-safe-nonce31348ea889cbaeb36c8a5750491a018a60d8d5ae64c6c23d3dc728690f86851a
visitor-payloadeyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQzUwOjcxREZBOjkwOTU1MjpDQ0I3NEE6NkFGQjUxMjQiLCJ2aXNpdG9yX2lkIjoiNzg0MjE0NTI4NDMzNTQxNDEyIiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0=
visitor-hmace2c9a49d6545559019aeab3856dd5045e73012c26238a862e573ce7707de56b4
hovercard-subject-tagpull_request:4847607637
github-keyboard-shortcutsrepository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot
google-site-verificationApib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I
octolytics-urlhttps://collector.github.com/github/collect
analytics-location///pull_requests/show/files
fb:app_id1401488693436528
apple-itunes-appapp-id=1477376905, app-argument=https://github.com/simstudioai/sim/pull/8972/files
twitter:imagehttps://avatars.githubusercontent.com/u/40672544?s=400&v=4
twitter:cardsummary_large_image
og:imagehttps://avatars.githubusercontent.com/u/40672544?s=400&v=4
og:image:altSummary Fixes deadlock detected (40P01) between a file content write and a concurrent new-file insert in the same workspace. It recurs in bursts when a workspace gets concurrent uploads and overwr...
og:site_nameGitHub
og:typeobject
hostnamegithub.com
expected-hostnamegithub.com
None7122bb414b542dba14d71a3f0b9f6fd2abce715b9f5912d082800563e6c806d1
turbo-cache-controlno-preview
diff-viewunified
go-importgithub.com/simstudioai/sim git https://github.com/simstudioai/sim.git
octolytics-dimension-user_id199344406
octolytics-dimension-user_loginsimstudioai
octolytics-dimension-repository_id912559512
octolytics-dimension-repository_nwosimstudioai/sim
octolytics-dimension-repository_publictrue
octolytics-dimension-repository_is_forkfalse
octolytics-dimension-repository_network_root_id912559512
octolytics-dimension-repository_network_root_nwosimstudioai/sim
turbo-body-classeslogged-out env-production page-responsive full-width
disable-turbotrue
browser-stats-urlhttps://api.github.com/_private/browser/stats
browser-errors-urlhttps://api.github.com/_private/browser/errors
releasecbf66c024bb6d10e6955baa7413c54cf13a201bb
ui-targetfull
theme-color#1e2327
color-schemelight dark

Links:

Skip to contenthttps://github.com/simstudioai/sim/pull/8972/files#start-of-content
https://github.com/
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fsimstudioai%2Fsim%2Fpull%2F8972%2Ffiles
GitHubRepository hosting, code review, and collaborationhttps://github.com/platform
CopilotAI coding agent for tasks, projects, and automated workhttps://github.com/features/copilot
ActionsCI/CD and automationhttps://github.com/features/actions
Code QualityQuality analysis, fixes, and enforcementhttps://github.com/features/code-quality
CodespacesCloud development environmentshttps://github.com/features/codespaces
Code SecurityVulnerability detection and remediationhttps://github.com/security/advanced-security/code-security
Secret ProtectionSecret scanning and leak preventionhttps://github.com/security/advanced-security/secret-protection
All Featureshttps://github.com/features
Documentationhttps://docs.github.com/en
Traininghttps://learn.github.com/skills
Certificationshttps://learn.github.com/credentials
Build with Copilothttps://github.com/features/ai
Customize Copilothttps://docs.github.com/en/copilot/how-tos/copilot-on-github/customize-copilot/customize-copilot-overview
Integrationshttps://github.com/marketplace
Trending reposhttps://github.com/trending
GitHub Sponsorshttps://github.com/open-source/sponsors
Developer articleshttps://github.com/resources/articles
ChangelogYour source for the latest features, improvements, and fixes across GitHub products, all in one place.https://github.blog/changelog/
Enterpriseshttps://github.com/enterprise
Teamhttps://github.com/team
Startupshttps://github.com/enterprise/startups
Nonprofitshttps://github.com/solutions/industry/nonprofits
Healthcarehttps://github.com/solutions/industry/healthcare
Financial serviceshttps://github.com/solutions/industry/financial-services
Manufacturinghttps://github.com/solutions/industry/manufacturing
Governmenthttps://github.com/solutions/industry/government
See all industrieshttps://github.com/solutions/industry
Why GitHubhttps://github.com/why-github
Customer Storieshttps://github.com/customer-stories
Business insightshttps://github.com/solutions/executive-insights
Trust Centerhttps://github.com/trust-center
GitHub Advanced SecurityEnterprise-grade code securityhttps://github.com/security/advanced-security
Copilot for businessEnterprise-grade AI featureshttps://github.com/features/copilot/copilot-business
Premium SupportEnterprise-grade 24/7 supporthttps://github.com/enterprise/premium-support
Documentationhttps://docs.github.com/en
Resource centerhttps://github.com/resources
Bloghttps://github.blog/
Events & webinarshttps://github.com/resources/events
Community Forumhttps://github.com/orgs/community/discussions
Customer Storieshttps://github.com/customer-stories
Integrationshttps://github.com/marketplace
Customer supporthttps://help.github.com/
Traininghttps://learn.github.com/skills
GitHub Statushttps://www.githubstatus.com/
Abouthttps://github.com/about
Partnershttps://github.com/partners
Changeloghttps://github.blog/changelog/
GitHub UniverseThe event for developers, by developers — talks, workshops, and what’s next for GitHub.https://githubuniverse.com/
Pricinghttps://github.com/pricing
Sign inhttps://github.com/login?return_to=https%3A%2F%2Fgithub.com%2Fsimstudioai%2Fsim%2Fpull%2F8972%2Ffiles
Sign uphttps://github.com/signup?ref_cta=Sign+up&ref_loc=header+logged+out&ref_page=%2F%3Cuser-name%3E%2F%3Crepo-name%3E%2Fpull_requests%2Fshow%2Ffiles&source=header-repo&source_repo=simstudioai%2Fsim
Reloadhttps://github.com/simstudioai/sim/pull/8972/files
Reloadhttps://github.com/simstudioai/sim/pull/8972/files
Reloadhttps://github.com/simstudioai/sim/pull/8972/files
simstudioaihttps://github.com/simstudioai
simhttps://github.com/simstudioai/sim
Codehttps://github.com/simstudioai/sim
Issues129 (129)https://github.com/simstudioai/sim/issues
Pull requests197 (197)https://github.com/simstudioai/sim/pulls
Actionshttps://github.com/simstudioai/sim/actions
Projectshttps://github.com/simstudioai/sim/projects
Security and qualityhttps://github.com/simstudioai/sim/security
Insightshttps://github.com/simstudioai/sim/pulse
Sign up for GitHub https://github.com/signup?return_to=%2Fsimstudioai%2Fsim%2Fissues%2Fnew%2Fchoose
terms of servicehttps://docs.github.com/terms
privacy statementhttps://docs.github.com/privacy
Sign inhttps://github.com/login?return_to=%2Fsimstudioai%2Fsim%2Fissues%2Fnew%2Fchoose
waleedlatif1https://github.com/waleedlatif1
staginghttps://github.com/simstudioai/sim/tree/staging
fix/file-write-lock-orderhttps://github.com/simstudioai/sim/tree/fix/file-write-lock-order
Conversation 12 https://github.com/simstudioai/sim/pull/8972
Commits 3 https://github.com/simstudioai/sim/pull/8972/commits
Checks 50 https://github.com/simstudioai/sim/pull/8972/checks
Files changed https://github.com/simstudioai/sim/pull/8972/files
Please reload this pagehttps://github.com/simstudioai/sim/pull/8972/files
fix(files): lock the workspace row before inserting a workspace file https://github.com/simstudioai/sim/pull/8972/files#top
Show all changes 3 commits https://github.com/simstudioai/sim/pull/8972/files
760b920 fix(files): lock the workspace row before inserting a workspace file waleedlatif1 Oct 13, 2026 https://github.com/simstudioai/sim/pull/8972/commits/760b920bfef6336e3a8f87bcc12daa07fe85f368
189f5c8 improvement(files): take the workspace row lock inside the shared fil… waleedlatif1 Oct 13, 2026 https://github.com/simstudioai/sim/pull/8972/commits/189f5c86d211ef4bfe3b346c816756fc3f7eca46
ad85d9a fix(files): skip the lock-order test without the search trigger and s… waleedlatif1 Oct 13, 2026 https://github.com/simstudioai/sim/pull/8972/commits/ad85d9a05a9f34c42904b70cb7437adc80696f88
Clear filters https://github.com/simstudioai/sim/pull/8972/files
Please reload this pagehttps://github.com/simstudioai/sim/pull/8972/files
Please reload this pagehttps://github.com/simstudioai/sim/pull/8972/files
copy-files.ts https://github.com/simstudioai/sim/pull/8972/files#diff-bf18028615819590a8db4f975a917d4a1885a8209f47999ae0d505fa24b3b772
tracking.ts https://github.com/simstudioai/sim/pull/8972/files#diff-6fd53176883b29868eaf2da3c52a8bcab3fecdd0af61802d5303a8149f3c9ba9
storage-lock-order.integration.ts https://github.com/simstudioai/sim/pull/8972/files#diff-9f550d00b99d17107865e1b02dfc5acf46e8e45c8773a5c39c0e0412a4a72831
workspace-file-manager.ts https://github.com/simstudioai/sim/pull/8972/files#diff-fcf305d0b37403347596bca42fc6889e9038c9f8e2c69ba20676c0f0d8fead34
apps/sim/ee/workspace-forking/lib/copy/copy-files.tshttps://github.com/simstudioai/sim/pull/8972/files#diff-bf18028615819590a8db4f975a917d4a1885a8209f47999ae0d505fa24b3b772
View file https://github.com/simstudioai/sim/blob/ad85d9a05a9f34c42904b70cb7437adc80696f88/apps/sim/ee/workspace-forking/lib/copy/copy-files.ts
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/simstudioai/sim/pull/8972/{{ revealButtonHref }}
https://github.com/simstudioai/sim/pull/8972/files#diff-bf18028615819590a8db4f975a917d4a1885a8209f47999ae0d505fa24b3b772
https://github.com/simstudioai/sim/pull/8972/files#diff-bf18028615819590a8db4f975a917d4a1885a8209f47999ae0d505fa24b3b772
https://github.com/simstudioai/sim/pull/8972/files#diff-bf18028615819590a8db4f975a917d4a1885a8209f47999ae0d505fa24b3b772
https://github.com/simstudioai/sim/pull/8972/files#diff-bf18028615819590a8db4f975a917d4a1885a8209f47999ae0d505fa24b3b772
apps/sim/lib/billing/storage/tracking.tshttps://github.com/simstudioai/sim/pull/8972/files#diff-6fd53176883b29868eaf2da3c52a8bcab3fecdd0af61802d5303a8149f3c9ba9
View file https://github.com/simstudioai/sim/blob/ad85d9a05a9f34c42904b70cb7437adc80696f88/apps/sim/lib/billing/storage/tracking.ts
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/simstudioai/sim/pull/8972/{{ revealButtonHref }}
https://github.com/simstudioai/sim/pull/8972/files#diff-6fd53176883b29868eaf2da3c52a8bcab3fecdd0af61802d5303a8149f3c9ba9
https://github.com/simstudioai/sim/pull/8972/files#diff-6fd53176883b29868eaf2da3c52a8bcab3fecdd0af61802d5303a8149f3c9ba9
apps/sim/lib/uploads/contexts/workspace/__integration__/storage-lock-order.integration.tshttps://github.com/simstudioai/sim/pull/8972/files#diff-9f550d00b99d17107865e1b02dfc5acf46e8e45c8773a5c39c0e0412a4a72831
View file https://github.com/simstudioai/sim/blob/ad85d9a05a9f34c42904b70cb7437adc80696f88/apps/sim/lib/uploads/contexts/workspace/__integration__/storage-lock-order.integration.ts
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/simstudioai/sim/pull/8972/{{ revealButtonHref }}
Please reload this pagehttps://github.com/simstudioai/sim/pull/8972/files
Please reload this pagehttps://github.com/simstudioai/sim/pull/8972/files
apps/sim/lib/uploads/contexts/workspace/workspace-file-manager.tshttps://github.com/simstudioai/sim/pull/8972/files#diff-fcf305d0b37403347596bca42fc6889e9038c9f8e2c69ba20676c0f0d8fead34
View file https://github.com/simstudioai/sim/blob/ad85d9a05a9f34c42904b70cb7437adc80696f88/apps/sim/lib/uploads/contexts/workspace/workspace-file-manager.ts
Open in desktop https://desktop.github.com
https://github.co/hiddenchars
https://github.com/simstudioai/sim/pull/8972/{{ revealButtonHref }}
https://github.com/simstudioai/sim/pull/8972/files#diff-fcf305d0b37403347596bca42fc6889e9038c9f8e2c69ba20676c0f0d8fead34
https://github.com/simstudioai/sim/pull/8972/files#diff-fcf305d0b37403347596bca42fc6889e9038c9f8e2c69ba20676c0f0d8fead34
Please reload this pagehttps://github.com/simstudioai/sim/pull/8972/files
https://github.com
Termshttps://docs.github.com/site-policy/github-terms/github-terms-of-service
Privacyhttps://docs.github.com/site-policy/privacy-policies/github-privacy-statement
Securityhttps://github.com/security
Statushttps://www.githubstatus.com/
Communityhttps://github.community/
Docshttps://docs.github.com/
Contacthttps://support.github.com?tags=dotcom-footer

Viewport: width=device-width


URLs of crawlers that visited me.