phantomjs icon indicating copy to clipboard operation
phantomjs copied to clipboard

phantomjs occasionally hangs when opening a page with many dependents

Open bhearsum opened this issue 14 years ago • 33 comments

[email protected] commented:

I'm using phantomjs + qunit to do some automated testing of some client side Javascript. While doing so, I found that phantom would sometimes hang in page.open(), a behaviour I was unable to reproduce by hand in Firefox. I tried using a Python-based web server as well as Apache - neither changed the behaviour. I tried different versions of QUnit, which also didn't affect behaviour. I was originally running with Phantom 1.4.0, and after upgrading to 1.6.0 is greatly reduced the rate at which I hit the hang, so I think this is a Phantom bug (or maybe a Webkit one). All my tests were done on Linux.

When the tests are run successfully, the output from the web server looks like this: 2012-07-11 15:38:29,402 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:29] "GET /tests.html HTTP/1.1" 401 - 2012-07-11 15:38:29,404 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:29] "GET /tests.html HTTP/1.1" 200 - 2012-07-11 15:38:29,406 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:29] "GET /static/jquery-1.6.4.js HTTP/1.1" 200 - 2012-07-11 15:38:29,408 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:29] "GET /static/qunit-1.9.0.js HTTP/1.1" 401 - 2012-07-11 15:38:29,408 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:29] "GET /static/ausadmin.js HTTP/1.1" 401 - 2012-07-11 15:38:29,409 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:29] "GET /static/test/test_ausadmin.js HTTP/1.1" 401 - 2012-07-11 15:38:29,411 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:29] "GET /static/test/test_ausadmin.js HTTP/1.1" 200 - 2012-07-11 15:38:29,412 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:29] "GET /static/qunit-1.9.0.js HTTP/1.1" 200 - 2012-07-11 15:38:29,414 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:29] "GET /static/ausadmin.js HTTP/1.1" 200 - 2012-07-11 15:38:29,450 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:29] "GET /reset HTTP/1.1" 401 - 2012-07-11 15:38:29,640 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:29] "GET /reset HTTP/1.1" 200 - 2012-07-11 15:38:29,678 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:29] "PUT /users/foo/permissions/admin HTTP/1.1" 201 - 2012-07-11 15:38:29,682 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:29] "GET /users/foo/permissions/admin?format=html HTTP/1.1" 200 -

When I hit the hang, it's only: 2012-07-11 15:38:30,487 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:30] "GET /tests.html HTTP/1.1" 401 - 2012-07-11 15:38:30,489 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:30] "GET /tests.html HTTP/1.1" 200 - 2012-07-11 15:38:30,492 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:30] "GET /static/jquery-1.6.4.js HTTP/1.1" 200 - 2012-07-11 15:38:30,493 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:30] "GET /static/qunit-1.9.0.js HTTP/1.1" 401 - 2012-07-11 15:38:30,494 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:30] "GET /static/test/test_ausadmin.js HTTP/1.1" 401 - 2012-07-11 15:38:30,495 - werkzeug._log#116: 127.0.0.1 - - [11/Jul/2012 15:38:30] "GET /static/ausadmin.js HTTP/1.1" 401 -

If I attach strace to phantom 1.4.0, I see that it's poll()'ing 3 different file descriptors: read(3, 0x7fff9192e500, 16) = -1 EAGAIN (Resource temporarily unavailable) recvfrom(6, "\10\2\255\0\30\303\31\16\257\0\0\0\257\0\0\0\0\0\0\0\215\5z\3\215\5z\3\0\0\0\2", 4096, 0, NULL, NULL) = 32 recvfrom(6, 0x1b14884, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable) recvfrom(6, 0x1b14884, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable) recvfrom(6, 0x1b14884, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable) poll([{fd=3, events=POLLIN}, {fd=6, events=POLLIN}, {fd=8, events=POLLIN}], 3, -1) = 1 ([{fd=8, revents=POLLIN}]) read(3, 0x7fff9192e500, 16) = -1 EAGAIN (Resource temporarily unavailable) recvfrom(6, 0x1b14884, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable) recvmsg(8, {msg_name(0)=NULL, msg_iov(1)=[{"l\4\1\1\31\0\0\0\256\23\30\0\265\0\0\0\1\1o\0000\0\0\0/org/fre"..., 2048}], msg_controllen=0, msg_flags=MSG_CMSG_CLOEXEC}, MSG_CMSG_CLOEXEC) = 225 recvmsg(8, 0x7fff9192db30, MSG_CMSG_CLOEXEC) = -1 EAGAIN (Resource temporarily unavailable) write(3, "\1\0\0\0\0\0\0\0", 8) = 8 write(7, "\1\0\0\0\0\0\0\0", 8) = 8 recvfrom(6, 0x1b14884, 4096, 0, 0, 0) = -1 EAGAIN (Resource temporarily unavailable) poll([{fd=3, events=POLLIN}, {fd=6, events=POLLIN}, {fd=8, events=POLLIN}], 3, 0) = 1 ([{fd=3, revents=POLLIN}])

Sometimes it appears to hit the poll() timeout, other times not. Those fds are: phantomjs 18082 bhearsum 3u 0000 0,9 0 6821 anon_inode phantomjs 18082 bhearsum 6u unix 0xffff88006e80bdc0 0t0 2180614 socket phantomjs 18082 bhearsum 8u unix 0xffff880074358680 0t0 2181568 socket

I'm not quite sure how to figure out precisely what sockets it's looking at.

In phantom 1.6.0 it's slightly different, it's stuck in select(): select(4, [3], [], [], NULL) = ? ERESTARTNOHAND (To be restarted) --- SIGWINCH (Window changed) @ 0 (0) --- select(4, [3], [], [], NULL) = ? ERESTARTNOHAND (To be restarted) --- SIGWINCH (Window changed) @ 0 (0) --- select(4, [3], [], [], NULL) = ? ERESTARTNOHAND (To be restarted) --- SIGWINCH (Window changed) @ 0 (0) --- select(4, [3], [], [], NULL

I tried to reduce this to a minimal non-python test case, but I was unable to reproduce that way. I have attached the script that I run with Phantom and the rendered output of tests.html. If there's anything else I can do to help figure out the root cause of this, please let me know.

Disclaimer: This issue was migrated on 2013-03-15 from the project's former issue tracker on Google Code, Issue #652. :star2:   12 people had starred this issue at the time of migration.

bhearsum avatar Jul 12 '12 03:07 bhearsum

[email protected] commented:

I think, that I have the same issue. I have a test suite with about 100 tests. The test infrastructure requires some heavy js (3-rd party libraries, etc) files to work. While the test suite passes in the browser using the standart QUnit test runner, it may occasionally hang when running from phantomjs. I tried to debug phantom's behaviour with console.log calls, and it seemes that the callback function which is passed into the setInterval inside waitFor function stops being called after a certain amount of time. This issue currently blocks integration of a JS test runner into continuous integration system.

gserebryanskyi avatar Jul 12 '12 16:07 gserebryanskyi

[email protected] commented:

I am getting a similar issue and socket strace produces the same output -- hanging at the select() call.

malaimo2900 avatar Aug 29 '12 20:08 malaimo2900

[email protected] commented:

I'm using phantomjs 1.7.0. It hangs randomly with strace select(4, [3], [], [], NULL

ariya avatar Nov 05 '12 23:11 ariya

[email protected] commented:

My environment with phantomjs 1.8.1 + ghostdriver has the same issue and hanging at "select(4, [3], [], [], NULL" as well.

Kudo avatar Jan 29 '13 14:01 Kudo

My workaround is to set a timeout, say 15 seconds, and use signal alarm to handle it. i.e.:

        # a bug in phantomjs: hang randomly
        # http://code.google.com/p/phantomjs/issues/detail?id=652
        timelimit = 15
        handler = signal.signal(signal.SIGALRM, timeout_handler)
        signal.alarm(timelimit)

        try:
            phantomjs = subprocess.Popen(test_args)
            phantomjs.communicate()
        except TimeoutException:
            print(("Process for the test {} is running beyond {} seconds, "
                   "restarting").format(test, timelimit))
            phantomjs.kill()

            # keep running the test until it finishes
            return self.run_unit_test(test, port)
        finally:
            # reset the handler to the old one
            signal.signal(signal.SIGALRM, handler)

        # cancel the alarm
        signal.alarm(0)

wujiang avatar Mar 16 '13 03:03 wujiang

I'm also seeing this problem. My test suite uses Jasmine, works fine in the browser and usually works fine in PhantomJS, but sometimes hangs (about 1 time in 10). I've also put in a timeout so that one hung test script doesn't crash my whole test suite but it is still an irritation.

In my case the HTML for the page being loaded is generated dynamically on the server. One of the things the server template does is take an array of .js filenames and produces a series of script tags. I added in some debugging on my dev server to console.log() the url that is about to be loaded and log it again from an onload event handler attached to the script tag, e.g.:

<script type="text/javascript">console.log("DEBUG: loading /js/jquery-1.10.2.min.js");</script>
<script type="text/javascript" src="/js/jquery-1.10.2.min.js" onload="console.log('DEBUG: loaded  /js/jquery-1.10.2.min.js');"></script>
<script type="text/javascript">console.log("DEBUG: loading /js/jquery-migrate-1.2.1.js");</script>
<script type="text/javascript" src="/js/jquery-migrate-1.2.1.js" onload="console.log('DEBUG: loaded  /js/jquery-migrate-1.2.1.js');"></script>
<script type="text/javascript">console.log("DEBUG: loading /js/jquery-ui-1.10.3.min.js");</script>
<script type="text/javascript" src="/js/jquery-ui-1.10.3.min.js" onload="console.log('DEBUG: loaded  /js/jquery-ui-1.10.3.min.js');"></script>
<script type="text/javascript">console.log("DEBUG: loading /js/jquery.scrollTo.min.js");</script>
<script type="text/javascript" src="/js/jquery.scrollTo.min.js" onload="console.log('DEBUG: loaded  /js/jquery.scrollTo.min.js');"></script>
<script type="text/javascript">console.log("DEBUG: loading /js/jquery.datetextentry.js");</script>
<script type="text/javascript" src="/js/jquery.datetextentry.js" onload="console.log('DEBUG: loaded  /js/jquery.datetextentry.js');"></script>
// ... etc ...

When the hang occurs, I see the "loading ..." message but not the "loaded ..." message for one of the .js files. It seems to be a different one each time. The Apache server log shows a response was provided, with a 200 status and the expected number of bytes were delivered.

So the problem would seem to be in the area of a script file failing to load successfully, or succeeding but not triggering the load event. This is quite reproducible - any suggestions on how I can help narrow it down further?

grantm avatar Oct 23 '13 22:10 grantm

I'm seeing similar behavior. Rarely, but reproducibly, Phantom will just hang opening a page. Any movement on this issue that's not reflected on the ticket?

jdolan avatar Feb 14 '14 16:02 jdolan

@jdolan I've "solved" it by using the timeout command in Ubuntu/Linux: timeout 10 phantomjs test.js

jonaslejon avatar Feb 17 '14 12:02 jonaslejon

+1

bcherny avatar Mar 18 '14 04:03 bcherny

+1

svalaskevicius avatar Mar 21 '14 15:03 svalaskevicius

+1

zllak avatar Jul 15 '14 13:07 zllak

I'm running into this issue with phantomjs 1.9.0. I set page.settings.resourceTimeout as a workaround.

Fransan avatar Aug 18 '14 18:08 Fransan

+1

xp65 avatar Nov 14 '14 17:11 xp65

I was having this same issue in 1.9.7, but it seems to have been fixed for me in 1.9.8.

msjonker avatar Nov 26 '14 12:11 msjonker

Just saying, not fixed for me in 1.9.8

eceeb avatar Nov 27 '14 08:11 eceeb

Is this issue back in 2.0? Heavy Javascript pages, especially ones using AngularJS or latest browser features, are causing my load to hang indefinitely. I'm not entirely sure how to troubleshoot it -- I've been trying to add timeouts inside the javascript:

var endScript = function(message, code) {
  console.log(message);
  phantom.exit(code);
};

window.setTimeout(function() {
  endScript('ERROR: Timeout', 2);
}, 60000);

But I'm only able to initiate the timeout outside of phantomjs.

voor avatar Mar 03 '15 14:03 voor

@voor +1 same here. @ariya any update on this (Why PhantomJS freezes)?

anands avatar Mar 08 '15 14:03 anands

A temporary fix I've found is making sure you don't have any script tags in the header, and then render the page anyway on timeout.

voor avatar Mar 09 '15 20:03 voor

+1

using casperjs v1.9.8, it's almost half the time I run my tests it hangs for me

ben432rew avatar May 13 '15 16:05 ben432rew

I've been seeing this issue in CasperJS as well. Adding the following to my tests at least allows PhantomJS to timeout instead of just hang forever.

casper.options.pageSettings.resourceTimeout = 20000;

The casper.options.pageSettings settings are all actually PhantomJS settings and are passed along from Casper to Phantom. It's been working pretty well for me thus far. Though would be nice to resolve this issue rather than work around it.

eojthebrave avatar Jun 11 '15 03:06 eojthebrave