Method: Selenium::WebDriver::Firefox::Launcher#with_lock

Defined in:
lib/selenium/webdriver/firefox/launcher.rb

#with_lockObject


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/selenium/webdriver/firefox/launcher.rb', line 49

def with_lock
  max_time = Time.now + SOCKET_LOCK_TIMEOUT
  locking_port = @port - 1

  until Time.now > max_time
    begin
      socket_lock = TCPServer.new(@host, locking_port)
      # make sure the fd is not inherited by firefox
      socket_lock.fcntl(Fcntl::F_SETFD, Fcntl::FD_CLOEXEC) if defined? Fcntl::FD_CLOEXEC

      yield
      return
    rescue SocketError, Errno::EADDRINUSE
      sleep 0.1
    end
  end

  raise Error::WebDriverError, "unable to bind to locking port #{locking_port} within #{SOCKET_LOCK_TIMEOUT} seconds"
ensure
  socket_lock.close if socket_lock
end