phantomjs not closing and leaving orphan processes

luksch picture luksch · Oct 2, 2013 · Viewed 8.9k times · Source

On PhantomJS 1.9.2, ubuntu 12 LTS and Ghostdirver 1.04 together with selenium 2.35 I get dangling phantomjs processes after my tests. Anyone knows a good way how to fix this?

Here is a test program that demonstrates the odd behavior:

package testing;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

public class PhantomIsNotKilledDemo {

    private static WebDriver getDriver(){
        String browserPathStr = System.getProperty("selenium.pathToBrowser");
        if (browserPathStr == null) browserPathStr = "/home/user1/apps/phantomjs/bin/phantomjs";

        DesiredCapabilities caps = DesiredCapabilities.phantomjs();

        caps.setCapability("takesScreenshot", true);
        caps.setCapability(
                PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                browserPathStr );

        WebDriver driver = new PhantomJSDriver(caps);

        return driver;
    }

    public static void main(String[] args) {
        int max = 10;
        for (int i = 0; i < max; i++){
            WebDriver d1 = getDriver();
            d1.get("http://www.imdb.com/title/tt1951264");

            System.out.println("done with cycle " + (i+1) +" of "+max);
            d1.close();
            //d1.quit();
        }

        System.out.println("done");
        System.exit(0);
    }
}

To run this, you should supply the path of your phantomjs binary as system property or set the variable accordingly.

After letting this run I do this shell command

ps -ef | grep phantomjs

and find 10 dangling phantomjs processes.

If I use d1.quit() instead, I end up with no dangling process. This is clearly better, but still I would have expected to get the same result with .close.

Note, this is a crosspost of https://github.com/detro/ghostdriver/issues/162#issuecomment-25536311

Update This post is changed according to Richard's suggestion (see below).

Answer

Ryan Guest picture Ryan Guest · Oct 31, 2015

You should be using quit() to terminate the process instead of close().

As you have discovered, close will close the current window (and browser), but it will not shut down the process. This is useful for if you are going to send additional commands to the process or want to inspect the process.

Quit is for when you want to close every window and stop the process, which sounds like what you are looking for.

The documentation for these two methods reads:

close()

Close the current window, quitting the browser if it's the last window currently open.

quit()

Quits this driver, closing every associated window.