Skip to content

Fix concurrent INSTALL/LOAD extension race on cold cache#711

Merged
adsharma merged 1 commit into
mainfrom
fix/issue-710-extension-install-race
Jul 22, 2026
Merged

Fix concurrent INSTALL/LOAD extension race on cold cache#711
adsharma merged 1 commit into
mainfrom
fix/issue-710-extension-install-race

Conversation

@adsharma

Copy link
Copy Markdown
Contributor

Summary

Fixes #710. Makes extension installation process-safe on a cold cache by eliminating three TOCTOU races.

Races fixed

1. createDir race

LocalFileSystem::createDir did an exists() pre-check before calling create_directories(), creating a TOCTOU window. Two concurrent callers both passed the check and one threw "Directory already exists".

Fix: Removed the exists() check. std::filesystem::create_directories already returns false (without setting errCode) when the directory already exists — we only throw on actual filesystem errors (errCode set).

2. File mutation under dlopen (SIGBUS/SIGSEGV)

tryDownloadExtensionFile opened the destination with CREATE_AND_TRUNCATE_IF_EXISTS and wrote in-place. If another connection had dlopend the same path (across threads or processes), the truncation or partial write caused the dynamic loader to fault on mutated mmap content → SIGBUS (truncated past EOF) or SIGSEGV (hash-walk over partial content).

Fix: Download to a unique temp file ({path}.tmp.{thread_hash}.{counter}), then rename() into place. POSIX rename() is atomic on the same filesystem:

  • Readers holding the old inode via dlopen keep a consistent view of the old file.
  • New opens always see the complete new file.

Temp files are cleaned up on both write failure and rename failure.

3. Wasted download race

Two processes both passed the fileOrPathExists check before the first finished writing. Added a re-check after the dir-creation section so the second process skips the download.

Testing

  • All 4 extension tests pass (extension~extension.*)
  • All 13 graph tests pass (including AnyGraphLabelsFunctions)
  • createDir change passes all existing tests (pre-existing failures in copy_to_csv, copy_to_parquet, copy_to_big_results also fail on main)
  • Build clean (lbug_extension, lbug_file_system)

Three TOCTOU races in the extension install path cause SIGBUS/SIGSEGV or
unhandled IOException when multiple connections (across threads or processes)
INSTALL the same extension on a cold cache:

1. createDir race: LocalFileSystem::createDir checked exists() before calling
   create_directories(). Two concurrent callers both passed the check and one
   hit "Directory already exists". Fixed by removing the TOCTOU pre-check and
   calling create_directories directly (returns false on "already exists"
   without setting errCode; we only throw on errCode).

2. File mutation under dlopen: tryDownloadExtensionFile opened the destination
   with CREATE_AND_TRUNCATE_IF_EXISTS and wrote in-place. If another process
   had dlopend the same path, the truncation / partial write caused the
   dynamic loader to fault on mutated mmap content (SIGBUS / SIGSEGV).
   Fixed by downloading to a unique temp file (thread-id hashed + atomic
   counter), then rename()ing into place. POSIX rename() is atomic on the
   same filesystem: readers holding the old inode keep a consistent view,
   new opens always see the complete file.

3. Wasted download race: Two processes both passed the fileOrPathExists check
   before the first one had finished writing. Added a re-check after the dir-
   creation section so the second process skips the download when the first
   already placed the file.

Co-Authored-By: Álvaro Tejero-Cantero <alvorithm@users.noreply.github.com>
@adsharma
adsharma force-pushed the fix/issue-710-extension-install-race branch from e3f8445 to 627d765 Compare July 21, 2026 22:22
@adsharma
adsharma merged commit a50a39a into main Jul 22, 2026
4 checks passed
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.

Concurrent INSTALL/LOAD of an extension is not process-safe on a cold cache (SIGBUS/SIGSEGV)

1 participant