Fix file descriptor leak with Capybara
When Capybara is used in a debugger in feature specs, developers will
often see "Too many open files". As described in
https://github.com/teamcapybara/capybara/issues/2322, it appears that
this may be an issue with WebMock, which stops Net::HTTP.start
from
connecting. If you start a connection and issue multiple requests with
it, WebMock will create a new connection per request.
Selenium calls Net::HTTP.start
here: https://github.com/SeleniumHQ/selenium/blob/238ca35ee569e43228042e0dc1f3330088301726/rb/lib/selenium/webdriver/remote/http/default.rb#L68.
If you issue a find
request in a debugger with Capybara running for an element that has not appeared, this causes repeated POST requests until the timeout occurs. Each of these requests gets a new connection because WebMock changes the behavior of Net::HTTP
and makes a new connection per request: https://github.com/bblimke/webmock/blob/382d84ca586fc87755d5657616d7a492421ee11b/lib/webmock/http_lib_adapters/net_http.rb#L104
Setting net_http_connect_on_start: true
appears to fix the problem:
https://github.com/bblimke/webmock/blob/master/README.md#connecting-on-nethttpstart
Many specs depending on the behavior of WebMock stubbing
Net::HTTP.start
, so use the WebMock default behavior in most cases and
enable it only for feature specs. Kubeclient needs an exception because
as well, so add a :kubeclient
label for this.
Documented upstream: https://github.com/teamcapybara/capybara/pull/2340/files