Skip to content

Commit

Permalink
Fixed temp cookie file handle leak in PhantomJS
Browse files Browse the repository at this point in the history
Fixes #1854

Signed-off-by: Luke Inman-Semerau <luke.semerau@gmail.com>
  • Loading branch information
Shanmuganandh authored and lukeis committed Mar 28, 2016
1 parent 6094ebf commit 0b23ace
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
5 changes: 3 additions & 2 deletions py/selenium/webdriver/phantomjs/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def __init__(self, executable_path, port=0, service_args=None, log_path=None):
if not log_path:
log_path = "ghostdriver.log"
if not self._args_contain("--cookies-file="):
self._cookie_temp_file = tempfile.mkstemp()[1]
self._cookie_temp_file_handle, self._cookie_temp_file = tempfile.mkstemp()
self.service_args.append("--cookies-file=" + self._cookie_temp_file)
else:
self._cookie_temp_file = None
Expand All @@ -65,4 +65,5 @@ def service_url(self):

def send_remote_shutdown_command(self):
if self._cookie_temp_file:
os.remove(self._cookie_temp_file)
os.close(self._cookie_temp_file_handle)
os.remove(self._cookie_temp_file)
53 changes: 53 additions & 0 deletions py/test/selenium/webdriver/phantomjs/phantomjs_launcher_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Licensed to the Software Freedom Conservancy (SFC) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The SFC licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

import psutil
import unittest
import logging
from selenium import webdriver


class PhantomJSLauncherTests(unittest.TestCase):

def testLaunchAndCloseBrowserWithoutLeakingCookieTempFileDescriptor(self):

# psutil module is used to get num open file descritors across platforms
self.p = psutil.Process()

self.num_fds_samples = []

self.driver = webdriver.PhantomJS()
self.driver.quit()

self.num_fds_samples.append(self.p.num_fds())

self.driver = webdriver.PhantomJS()
self.driver.quit()

self.num_fds_samples.append(self.p.num_fds())

self.driver = webdriver.PhantomJS()
self.driver.quit()

self.num_fds_samples.append(self.p.num_fds())

assert max(self.num_fds_samples) == min(self.num_fds_samples)


if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
unittest.main()

0 comments on commit 0b23ace

Please sign in to comment.