Skip to content

Remove Dir.chdir for thread-safety - #397

Open
lovro-bikic wants to merge 1 commit into
rubysec:masterfrom
lovro-bikic:threadsafe-database-methods
Open

Remove Dir.chdir for thread-safety#397
lovro-bikic wants to merge 1 commit into
rubysec:masterfrom
lovro-bikic:threadsafe-database-methods

Conversation

@lovro-bikic

Copy link
Copy Markdown

Currently, Bundler::Audit::Database#update!, #commit_id and #last_updated_at fail in multi-threaded environments (like background processing jobs) because they use Dir.chdir.

From Ruby docs on Dir.chdir:

In a multi-threaded program an error is raised if a thread attempts to open a chdir block while another thread has one open, or a call to chdir without a block occurs inside a block passed to chdir (even in the same thread).

This PR removes the need for Dir.chdir by using Open3.capture2 from the stdlib with the chdir option.

External behavior should stay the same: Dir.chdir raises an Errno::ENOENT error given an non-existent directory, and the same holds with the new implementation.

Fixes #156

@simi

simi commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

@lovro-bikic what about to use git -C (work dir), would that work also? 🤔

@lovro-bikic

Copy link
Copy Markdown
Author

Nice one! I believe we could use it. I only have two questions:

  1. -C option was introduced in v1.8.5. I'm not sure if we should support older Git versions?
  2. While undocumented and untested, the gem currently raises Errno::ENOENT for unknown directories. With git -C, the error will look like below. Is that acceptable?
# with fake DB path "blabla"

> bin/bundler-audit update
Updating ruby-advisory-db ...
fatal: cannot change to 'blabla': No such file or directory

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.

Bundler audit is not thread safe

2 participants