exit doesn't exit #193

Closed
bryanmills opened this Issue Jul 20, 2012 · 30 comments

Comments

Projects
None yet
@bryanmills

I have a casperjs script running in production and randomly I would login to my production machines and see multiple orphaned casper processes. After resolving all the various timeout issues that I thought was causing this issue I have been able to successfully write a two line script that reproduces the problem.

var casper = require("casper").create();
casper.exit();
while(1) { }

Interestingly, this also happens with straight phantomjs

phantom.exit()
while(1) { } 

I'm running casperjs 1.0.0-RC1 and phantomjs 1.6.0.

I believe this is related to the issue already filed against phantomjs:
http://code.google.com/p/phantomjs/issues/detail?id=444

Another tricky thing is it exhibits the same behavior if there is an exception thrown.

this.makes.no.sense()
while(1) { }

Any hints as to how to fix this or at least prevent it would be very helpful.

Thanks.

-bryan

@n1k0

This comment has been minimized.

Show comment
Hide comment
@n1k0

n1k0 Jul 21, 2012

Member

Indeed… I have no idea how to prevent this to happen though :/

Member

n1k0 commented Jul 21, 2012

Indeed… I have no idea how to prevent this to happen though :/

@bryanmills

This comment has been minimized.

Show comment
Hide comment
@bryanmills

bryanmills Jul 23, 2012

Do you have any idea why it happens? Does phantomjs execute each line of code independently? If I had a pointer into where to look I would try and fix it but I don't really know where to begin.

Do you have any idea why it happens? Does phantomjs execute each line of code independently? If I had a pointer into where to look I would try and fix it but I don't really know where to begin.

@n1k0

This comment has been minimized.

Show comment
Hide comment
@n1k0

n1k0 Jul 23, 2012

Member

I don't know how phantomjs is handling this internally, what's sure is I still don't have any idea how I could patch this behavior using javascript in casperjs.

FTR, I'm leaving the issue opened for when the phantomjs bug will be resolved :)

Member

n1k0 commented Jul 23, 2012

I don't know how phantomjs is handling this internally, what's sure is I still don't have any idea how I could patch this behavior using javascript in casperjs.

FTR, I'm leaving the issue opened for when the phantomjs bug will be resolved :)

@victor73

This comment has been minimized.

Show comment
Hide comment
@victor73

victor73 Aug 30, 2012

Running into this too. Tried using die() but that doesn't work either. Do die() and exit() do the same thing?

Running into this too. Tried using die() but that doesn't work either. Do die() and exit() do the same thing?

@n1k0

This comment has been minimized.

Show comment
Hide comment
@n1k0

n1k0 Oct 19, 2012

Member

@victor73 basically, yes, die() is just a shortcut for echo() then exit()

Member

n1k0 commented Oct 19, 2012

@victor73 basically, yes, die() is just a shortcut for echo() then exit()

@p

This comment has been minimized.

Show comment
Hide comment
@p

p Feb 9, 2013

Contributor

A workaround is to ask the operating system to destroy the process: p/test-repo@baced39

Currently the workaround does not work in casper.js due to failing child_process require, possibly due to #348.

A better solution would be to have phantomjs provide a terminate method that would immediately exit without emitting any events or waiting for any other code to run. Then a non-livelocking process could call exit() and fall back on terminate() some time later.

Contributor

p commented Feb 9, 2013

A workaround is to ask the operating system to destroy the process: p/test-repo@baced39

Currently the workaround does not work in casper.js due to failing child_process require, possibly due to #348.

A better solution would be to have phantomjs provide a terminate method that would immediately exit without emitting any events or waiting for any other code to run. Then a non-livelocking process could call exit() and fall back on terminate() some time later.

@n1k0

This comment has been minimized.

Show comment
Hide comment
@n1k0

n1k0 Feb 9, 2013

Member

I'm not aware phantomjs has a subprocess module… isn't this a node thing?

Member

n1k0 commented Feb 9, 2013

I'm not aware phantomjs has a subprocess module… isn't this a node thing?

@p

This comment has been minimized.

Show comment
Hide comment
@p

p Feb 9, 2013

Contributor

Apparently it is not documented. My impression is it is a partial implementation of the node version.

I modeled the workaround after http://code.google.com/p/phantomjs/source/browse/examples/child_process-examples.js?spec=svnf52044cd3106266aa8efc1fae05e8c923315a5e5&r=f52044cd3106266aa8efc1fae05e8c923315a5e5.

See here: http://code.google.com/p/phantomjs/issues/detail?id=219&can=1&q=child_process

That ticket says milestone is 1.9 but I am quite certain I tested the workaround on 1.8.1.

Contributor

p commented Feb 9, 2013

Apparently it is not documented. My impression is it is a partial implementation of the node version.

I modeled the workaround after http://code.google.com/p/phantomjs/source/browse/examples/child_process-examples.js?spec=svnf52044cd3106266aa8efc1fae05e8c923315a5e5&r=f52044cd3106266aa8efc1fae05e8c923315a5e5.

See here: http://code.google.com/p/phantomjs/issues/detail?id=219&can=1&q=child_process

That ticket says milestone is 1.9 but I am quite certain I tested the workaround on 1.8.1.

@n1k0

This comment has been minimized.

Show comment
Hide comment
@n1k0

n1k0 Feb 9, 2013

Member

Using 1.8.1:

$ phantomjs --version
1.8.1
$ phantomjs
phantomjs> require("child_process")
Cannot find module 'child_process'

Right now I can't do much about it.

Member

n1k0 commented Feb 9, 2013

Using 1.8.1:

$ phantomjs --version
1.8.1
$ phantomjs
phantomjs> require("child_process")
Cannot find module 'child_process'

Right now I can't do much about it.

@kevin-smets

This comment has been minimized.

Show comment
Hide comment
@kevin-smets

kevin-smets Jun 10, 2013

Hmmm, facing this problem too now. I can't get the callback of run to trigger either, is that possible? Wouldn't the following be an option?

this.run(
   // on end
   function(){
      this.echo('Test suite done, exiting..');
      this.exit();
   }
);

I thought this worked on phantom 1.9.0 + casper 1.0.2 but can't seem to get it working anymore.

Hmmm, facing this problem too now. I can't get the callback of run to trigger either, is that possible? Wouldn't the following be an option?

this.run(
   // on end
   function(){
      this.echo('Test suite done, exiting..');
      this.exit();
   }
);

I thought this worked on phantom 1.9.0 + casper 1.0.2 but can't seem to get it working anymore.

@maciejjankowski

This comment has been minimized.

Show comment
Hide comment
@maciejjankowski

maciejjankowski Oct 8, 2013

Contributor

same here. exit does not exit.
why even mention it in docs?

Contributor

maciejjankowski commented Oct 8, 2013

same here. exit does not exit.
why even mention it in docs?

@laurentj

This comment has been minimized.

Show comment
Hide comment
@laurentj

laurentj Nov 13, 2013

Collaborator

SlimerJS fails too on this kind of code

phantom.exit()
while(1) { } 

Exit() doesn't exit.
It seems it is related on how javascript engines work...

Collaborator

laurentj commented Nov 13, 2013

SlimerJS fails too on this kind of code

phantom.exit()
while(1) { } 

Exit() doesn't exit.
It seems it is related on how javascript engines work...

@n1k0

This comment has been minimized.

Show comment
Hide comment
@n1k0

n1k0 Nov 13, 2013

Member

Yeah, definitely a JavaScript issue. Closing as I'm currently doing some autumn cleaning in the tracker.

Member

n1k0 commented Nov 13, 2013

Yeah, definitely a JavaScript issue. Closing as I'm currently doing some autumn cleaning in the tracker.

@n1k0 n1k0 closed this Nov 13, 2013

@sennoy

This comment has been minimized.

Show comment
Hide comment
@sennoy

sennoy Jul 25, 2014

this one still not work, right? I cannot exit on
casper.exit()

sennoy commented Jul 25, 2014

this one still not work, right? I cannot exit on
casper.exit()

@maciejjankowski

This comment has been minimized.

Show comment
Hide comment
@maciejjankowski

maciejjankowski Jul 25, 2014

Contributor

I didn't see anyone mention it, but the solution that worked for me was very unexpected:
I have two video cards NVS4200 and Intel integrated something, making casper (or phantom, I don't remember) use integrated graphics helped to solve the issue (see this: ariya/phantomjs#10845)

Seriously - just by changing my graphic card. WTF?
Anyway - it exits now.

Contributor

maciejjankowski commented Jul 25, 2014

I didn't see anyone mention it, but the solution that worked for me was very unexpected:
I have two video cards NVS4200 and Intel integrated something, making casper (or phantom, I don't remember) use integrated graphics helped to solve the issue (see this: ariya/phantomjs#10845)

Seriously - just by changing my graphic card. WTF?
Anyway - it exits now.

@sennoy

This comment has been minimized.

Show comment
Hide comment
@sennoy

sennoy Jul 28, 2014

@maciejjankowski I suppose it's connected to rendering html pages inside phantomjs.. thanks for response!

sennoy commented Jul 28, 2014

@maciejjankowski I suppose it's connected to rendering html pages inside phantomjs.. thanks for response!

@maciejjankowski

This comment has been minimized.

Show comment
Hide comment
@maciejjankowski

maciejjankowski Jul 28, 2014

Contributor

pls confirm it it worked for you

Contributor

maciejjankowski commented Jul 28, 2014

pls confirm it it worked for you

@sennoy

This comment has been minimized.

Show comment
Hide comment
@sennoy

sennoy Jul 28, 2014

@maciejjankowski yes, it worked 👍

sennoy commented Jul 28, 2014

@maciejjankowski yes, it worked 👍

@maciejjankowski

This comment has been minimized.

Show comment
Hide comment
@maciejjankowski

maciejjankowski Jul 28, 2014

Contributor

🍻

Contributor

maciejjankowski commented Jul 28, 2014

🍻

@mickaelandrieu

This comment has been minimized.

Show comment
Hide comment
@mickaelandrieu

mickaelandrieu Jul 28, 2014

Member

Interesting solution, thoughs @n1k0 ? :) ty @maciejjankowski

Member

mickaelandrieu commented Jul 28, 2014

Interesting solution, thoughs @n1k0 ? :) ty @maciejjankowski

@pillarsdotnet

This comment has been minimized.

Show comment
Hide comment
@pillarsdotnet

pillarsdotnet Aug 5, 2015

Calling casper.bypass(999) forces an early exit.

Calling casper.bypass(999) forces an early exit.

@DarrenCook

This comment has been minimized.

Show comment
Hide comment
@DarrenCook

DarrenCook Sep 22, 2015

Curiously, this is working for me (with SlimerJS, and running casper in test mode):

casper.exit()
casper.bypass(1)

I.e. the number given to bypass seems to not matter.

Curiously, this is working for me (with SlimerJS, and running casper in test mode):

casper.exit()
casper.bypass(1)

I.e. the number given to bypass seems to not matter.

@pillarsdotnet

This comment has been minimized.

Show comment
Hide comment
@pillarsdotnet

pillarsdotnet Sep 27, 2015

The number doesn't have to be 999; it just has to be as big as your function nesting level. That is, in the main loop, pass a 1 or higher. In a function called by the main loop, pass a 2 or higher, and so on.
On Sep 22, 2015 06:21, Darren Cook notifications@github.com wrote:Curiously, this is working for me (with SlimerJS, and running casper in test mode):

casper.exit()
casper.bypass(1)

I.e. the number given to bypass seems to not matter.

—Reply to this email directly or view it on GitHub.

The number doesn't have to be 999; it just has to be as big as your function nesting level. That is, in the main loop, pass a 1 or higher. In a function called by the main loop, pass a 2 or higher, and so on.
On Sep 22, 2015 06:21, Darren Cook notifications@github.com wrote:Curiously, this is working for me (with SlimerJS, and running casper in test mode):

casper.exit()
casper.bypass(1)

I.e. the number given to bypass seems to not matter.

—Reply to this email directly or view it on GitHub.

@sergeipetrushenko

This comment has been minimized.

Show comment
Hide comment
@sergeipetrushenko

sergeipetrushenko Nov 13, 2015

i confirm. this works for me with casperjs 1.1-b3 running phantomjs 1.9.8

casper.exit();
casper.bypass(1);

i confirm. this works for me with casperjs 1.1-b3 running phantomjs 1.9.8

casper.exit();
casper.bypass(1);
@cirosantilli

This comment has been minimized.

Show comment
Hide comment
@cirosantilli

cirosantilli Dec 19, 2015

+1. Please clarify what exit actually does in the docs.

bypass failed on a complex example which I don't want to minimize. I recommend setting up your program logic to never run two exit, e.g.:

if (something) {
  casper.exit(0)
} else {
  casper.exit(1)
}

+1. Please clarify what exit actually does in the docs.

bypass failed on a complex example which I don't want to minimize. I recommend setting up your program logic to never run two exit, e.g.:

if (something) {
  casper.exit(0)
} else {
  casper.exit(1)
}
@wskorodecki

This comment has been minimized.

Show comment
Hide comment
@wskorodecki

wskorodecki Mar 18, 2016

Contributor

I confirm that bypass resolved the problem for me. Anyway please add some note to the docs that exit is working asynchronously. Just like here: https://docs.slimerjs.org/current/api/phantom.html#exit-code
It is not a full explanation how (or when) it works, but it is better than nothing.

Contributor

wskorodecki commented Mar 18, 2016

I confirm that bypass resolved the problem for me. Anyway please add some note to the docs that exit is working asynchronously. Just like here: https://docs.slimerjs.org/current/api/phantom.html#exit-code
It is not a full explanation how (or when) it works, but it is better than nothing.

@mickaelandrieu

This comment has been minimized.

Show comment
Hide comment
@mickaelandrieu

mickaelandrieu Mar 18, 2016

Member

@wskorodecki indeed. Can you make a pull request please?

Thank you

Member

mickaelandrieu commented Mar 18, 2016

@wskorodecki indeed. Can you make a pull request please?

Thank you

wskorodecki added a commit to wskorodecki/casperjs that referenced this issue Mar 18, 2016

Update casper.rst
Changes according to casperjs#193
@waza123x

This comment has been minimized.

Show comment
Hide comment
@waza123x

waza123x Oct 31, 2016

still not exiting/closing cmd sometimes, even with

casper.exit()
casper.bypass(1)

any other way ?

still not exiting/closing cmd sometimes, even with

casper.exit()
casper.bypass(1)

any other way ?

@Kenith

This comment has been minimized.

Show comment
Hide comment
@Kenith

Kenith Nov 22, 2017

seems it is a common issue from Phantomjs: SeleniumHQ/selenium#767

Kenith commented Nov 22, 2017

seems it is a common issue from Phantomjs: SeleniumHQ/selenium#767

@kensoh

This comment has been minimized.

Show comment
Hide comment
@kensoh

kensoh Mar 10, 2018

Member

For folks having this issue, try something like below. What I found is doing a .wait(0) right after an .exit() somehow would make the exit terminate at the point where it is called. (at least for my use cases).

casper.exit();
casper.wait(0);
Member

kensoh commented Mar 10, 2018

For folks having this issue, try something like below. What I found is doing a .wait(0) right after an .exit() somehow would make the exit terminate at the point where it is called. (at least for my use cases).

casper.exit();
casper.wait(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment