From 06b7a6bd59d354bcb3a7c9dc373edd791a2bcbd7 Mon Sep 17 00:00:00 2001 From: Alan Guo Xiang Tan Date: Fri, 2 Feb 2024 11:04:37 +0800 Subject: [PATCH] DEV: Accquire file lock before checking if selenium webdriver cache (#25534) Why this change? We are getting the following error on CI: `Text file busy - /github/home/.cache/selenium/chromedriver/linux64/121.0.6167.85/chromedriver` This happens when two processes tries to download the chromedriver at the same time. I'm not entirely sure why the previous implementatio is not locking properly since we still saw the `Text file busy` error but by accquring the lock before we even check for the existence of `~/.cache/selenium`, we should be able to eliminate the chance of two processes trying to download the chromedriver binary at the same time. --- spec/rails_helper.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 3969532bfe4..0c3b752f7ca 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -572,9 +572,10 @@ RSpec.configure do |config| # # The long term fix here is to get `selenium-manager` to download the `chromedriver` binary to a unique path for each # process but the `--cache-path` option for `selenium-manager` is currently not supported in `selenium-webdriver`. - if !File.directory?(File.expand_path("~/.cache/selenium")) - File.open("#{Rails.root}/tmp/chrome_driver_flock", File::RDWR | File::CREAT, 0644) do |file| - file.flock(File::LOCK_EX) + File.open("#{Rails.root}/tmp/chrome_driver_flock", File::RDWR | File::CREAT, 0644) do |file| + file.flock(File::LOCK_EX) + + if !File.directory?(File.expand_path("~/.cache/selenium")) `#{Selenium::WebDriver::SeleniumManager.send(:binary)} --browser chrome` end end