Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python/Linux quit() does not terminate PhantomJS process #767

Closed
kylewm opened this issue Jul 11, 2015 · 12 comments
Closed

Python/Linux quit() does not terminate PhantomJS process #767

kylewm opened this issue Jul 11, 2015 · 12 comments
Labels

Comments

@kylewm
Copy link

@kylewm kylewm commented Jul 11, 2015

Calling quit() on the Python driver seems to leave the phantomjs process running.

(venv2)kmahan@lemur:~/projects/selenium-test$ python
Python 2.7.9 (default, Apr  2 2015, 15:33:21) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from selenium import webdriver
>>> browser = webdriver.PhantomJS()
>>> browser.quit()
>>> 
(venv2)kmahan@lemur:~/projects/selenium-test$ ps ax | grep phantomjs
17503 pts/18   Sl     0:00 /usr/local/lib/node_modules/phantomjs/lib/phantom/bin/phantomjs --webdriver=36954
17518 pts/18   S+     0:00 grep --color=auto phantomjs

dbac744 appears to revert a fix from https://code.google.com/p/selenium/issues/detail?id=5921

Possible solution: Calling self.process.terminate() instead of self.process.kill() seems to terminate the process correctly on Linux and should have the same effect as kill() on Windows.

@nmondal
Copy link

@nmondal nmondal commented Jul 21, 2015

Hi,
Did you take a look around Phantom JS Repo Bugs here?

@ddavison ddavison added the C-py label Jul 21, 2015
@kylewm
Copy link
Author

@kylewm kylewm commented Jul 21, 2015

HI @nmondal thanks for the pointer. I guess the issue is that webdriver.quit() doesn't terminate the process, and we should not have to rely on SIGKILL or SIGTERMing the process directly. I can try building phantom on my own machine instead of using the version from npm as one of the comments suggests. Will report back.

@romlok
Copy link

@romlok romlok commented Sep 15, 2015

I just hit this as well. The phantomjs subprocess forks a child to do the heavy lifting, and the parent script cannot know to terminate its forked child process when it is simply SIGKILLed by selenium (see the problematic commit mentioned in the first post).

For now, I'm manually sending a SIGTERM to my driver instance's subprocess:

browser.service.process.send_signal(signal.SIGTERM)
browser.quit()
@skovhus
Copy link

@skovhus skovhus commented Oct 18, 2015

Until this is fixed in ghostdriver, wouldn't it make sense to revert dbac744 ? @barancev

@mgedmin
Copy link

@mgedmin mgedmin commented Apr 11, 2016

Could it be because of #1291 (comment) ?

@changbl
Copy link

@changbl changbl commented Jul 15, 2016

This works for me as a fix:

browser.service.process.send_signal(signal.SIGTERM)
browser.quit()

@MatMercer
Copy link

@MatMercer MatMercer commented Dec 16, 2016

@romlok
Thanks a lot, my application is server side and this is a huge problem since every user that request a method that have the driver allocates 15 MB in the memory.

Your method works.

@barseghyanartur
Copy link

@barseghyanartur barseghyanartur commented Feb 25, 2017

As a temporary solution for Python bindings:

First define the function somewhere, for instance in helpers.

def phantom_js_clean_up():
    """Clean up Phantom JS.

    Kills all phantomjs instances, disregard of their origin.
    """
    processes = subprocess.Popen(['ps', '-A'], stdout=subprocess.PIPE)
    out, err = processes.communicate()

    for line in out.splitlines():
        if 'phantomjs' in line:
            pid = int(line.split(None, 1)[0])
            os.kill(pid, signal.SIGKILL)

Then run it in the scope of tearDownClass.

@classmethod
def tearDownClass(cls):
    """Tear down class."""
    try:
        cls.driver.quit()
        phantom_js_clean_up()
    except Exception as err:
        print(err)
@AlexandrVelikiy
Copy link

@AlexandrVelikiy AlexandrVelikiy commented Oct 2, 2017

Hello, for some time now, I have an error:
self.driver.quit() File "/usr/lib/python2.6/site-packages/selenium/webdriver/phantomjs/webdriver.py", line 76, in quit self.service.stop() File "/usr/lib/python2.6/site-packages/selenium/webdriver/common/service.py", line 149, in stop self.send_remote_shutdown_command() File "/usr/lib/python2.6/site-packages/selenium/webdriver/phantomjs/service.py", line 67, in send_remote_shutdown_command os.close(self._cookie_temp_file_handle) OSError: [Errno 9] Bad file descriptor
Code:
self.driver.service.process.send_signal(signal.SIGTERM) self.driver.quit()

CentOs version centos-release-6-7.el6.centos.12.3.i686

Any ideas?
Thanks

@zhaoli2017
Copy link

@zhaoli2017 zhaoli2017 commented Nov 7, 2017

Hi, I still have this problem.
File "/home/zhaoli/.local/lib/python2.7/site-packages/selenium/webdriver/phantomjs/webdriver.py", line 76, in quit self.service.stop() File "/home/zhaoli/.local/lib/python2.7/site-packages/selenium/webdriver/common/service.py", line 149, in stop self.send_remote_shutdown_command() File "/home/zhaoli/.local/lib/python2.7/site-packages/selenium/webdriver/phantomjs/service.py", line 67, in send_remote_shutdown_command os.close(self._cookie_temp_file_handle) OSError: [Errno 9] Bad file descriptor

I call the PhantomJS at every httprequest, they won't cause this problem all the time, and it will still work after a refresh.

ubuntu 14.04, python 2.7

Thanks~

@lmtierney
Copy link
Member

@lmtierney lmtierney commented Dec 6, 2017

Closing this as phantomjs support is being deprecated in favor of chrome or firefox headless

@lmtierney lmtierney closed this Dec 6, 2017
@barseghyanartur
Copy link

@barseghyanartur barseghyanartur commented Dec 6, 2017

@lmtierney:

That's what I finally ended up with (headless Firefox/Chrome).

@lock lock bot locked and limited conversation to collaborators Aug 16, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
You can’t perform that action at this time.