# Why is PhantomJS disconnecting?

**URL:** <https://discuss.emberjs.com/t/why-is-phantomjs-disconnecting/9636>\
**Category:** Testing\
**Created:** [February 2, 2016, 6:17pm UTC](https://discuss.emberjs.com/t/why-is-phantomjs-disconnecting/9636 "2016-02-02T18:17:12Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamesarosen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jamesarosen/32/3535_2.png) [@jamesarosen](https://discuss.emberjs.com/u/jamesarosen)\
**Post date:** [February 2, 2016, 6:17pm UTC](https://discuss.emberjs.com/t/why-is-phantomjs-disconnecting/9636/1 "2016-02-02T18:17:12Z")

</div>

For a few months now, we’ve been having problems with our tests aborting in CI. On Ember-CLI 1.13.13, we got the error

> not ok 130 PhantomJS - Browser “phantomjs /home/travis/build/fastly/Tango/node\_modules/ember-cli/node\_modules/testem/assets/phantom.js [http://localhost:7357/9353](http://localhost:7357/9353)” exited unexpectedly.

That message comes from [Testem](https://github.com/testem/testem/blob/e783212e218ee1628c1a9edd4d8efedb1014c3d6/lib/ci/browser_test_runner.js#L84).

It would happen fairly erratically, but once it happened on a build, it would likely recur at least a few times. Here are some of our hypotheses and what we did to try to fix the problem:

### Bad version of PhantomJS

[This StackOverflow answer](http://stackoverflow.com/a/24985909/1190) suggests it’s a bad build of PhantomJS. We upgraded to a release version of PhantomJS 2.0.0 in dev and CI using [this PPA](https://launchpad.net/~tanguy-patte/+archive/ubuntu/phantomjs). That didn’t fix the problem.

### Out of memory

Since the error only happened on some builds and usually fairly late into the build, we thought perhaps the PhantomJS process was crashing because it was running our to memory.

We upgraded our CI infrastructure to machines with more memory. We split the test run into four separate test suites:

```auto
- ember build --environment=test
- ember test --path=dist --filter="JSHint"
- ember test --path=dist --filter="JSCS"
- ember test --path=dist --filter="Unit: "
- ember test --path=dist --filter="Acceptance: "

```

I went through the build in Chrome many times looking for memory leaks and eliminated all I could find. (Other than upgrading Mirage to take advantage of [this cleanup](https://github.com/samselikoff/ember-cli-mirage/pull/478), all were jQuery event handlers.)

I also ran the test suite on the CI machines and SSH’d into them while the tests were running. I ran `free -m` occasionally to find out how much memory was left. Over the course of the suite, it went from 7000MB to 4500MB – definitely some memory leaking, but not nearly enough to crash the process.

### Upgrade Ember-CLI

I noticed that more recent versions of Testem had a [slightly more informative error message](https://github.com/testem/testem/blob/3a396b15821f592bd73c009b51f2e96a007e6f81/lib/runners/browser_test_runner.js#L169), so I upgraded to Ember-CLI 2.3.0-beta.1. The upgrade went fine, but didn’t help. Now we get

> not ok 130 PhantomJS - Browser “phantomjs /home/travis/build/fastly/Tango/node\_modules/ember-cli/node\_modules/testem/assets/phantom.js [http://localhost:7357/9353](http://localhost:7357/9353)” disconnected unexpectedly.

from [this line](https://github.com/testem/testem/blob/3a396b15821f592bd73c009b51f2e96a007e6f81/lib/runners/browser_test_runner.js#L154).

### Print STDOUT, STDERR

I tried monkey-patching Testem to print STDOUT and STDERR when it exits, but they were empty.

## Summary

I’m at a loss. I can’t think of anything that would cause the socket to disconnect that wouldn’t also crash the browser. There’s no code that navigates away from the page or closes the window. The failures happen only sporadically – always on an acceptance test, but on many different ones. If anyone has any suggestions, please let me know.

---

<div class="post-metadata">

**Author:** ![Ahmad\_Musaffa](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ahmad_musaffa/32/13691_2.png) [@Ahmad\_Musaffa](https://discuss.emberjs.com/u/Ahmad_Musaffa)\
**Post date:** [February 2, 2016, 8:11pm UTC](https://discuss.emberjs.com/t/why-is-phantomjs-disconnecting/9636/2 "2016-02-02T20:11:51Z")

</div>

I had the same disconnection problem. Downgrading testem version to 0.9.11 solves the problem for now.

---

<div class="post-metadata">

**Author:** ![jamesarosen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jamesarosen/32/3535_2.png) [@jamesarosen](https://discuss.emberjs.com/u/jamesarosen)\
**Post date:** [February 2, 2016, 8:24pm UTC](https://discuss.emberjs.com/t/why-is-phantomjs-disconnecting/9636/3 "2016-02-02T20:24:09Z")

</div>

How did you downgrade? Did you have to fork Ember-CLI? Or is there some setting you can use to tell Ember-CLI to use another version?

---

<div class="post-metadata">

**Author:** ![jamesarosen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jamesarosen/32/3535_2.png) [@jamesarosen](https://discuss.emberjs.com/u/jamesarosen)\
**Post date:** [February 2, 2016, 8:27pm UTC](https://discuss.emberjs.com/t/why-is-phantomjs-disconnecting/9636/4 "2016-02-02T20:27:37Z")

</div>

I wouldn’t have thought it was related, but since @rwjblue mentioned it in Slack, I will. When I tried switching the test runner to Chrome, the tests seemed to boot, but never got any output back from the browser, so they just timed out.

---

<div class="post-metadata">

**Author:** ![Ahmad\_Musaffa](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ahmad_musaffa/32/13691_2.png) [@Ahmad\_Musaffa](https://discuss.emberjs.com/u/Ahmad_Musaffa)\
**Post date:** [February 2, 2016, 8:30pm UTC](https://discuss.emberjs.com/t/why-is-phantomjs-disconnecting/9636/5 "2016-02-02T20:30:09Z")

</div>

Go to `node_modules/ember-cli/` and run `npm install testem@0.9.11`.

---

<div class="post-metadata">

**Author:** ![Ahmad\_Musaffa](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/ahmad_musaffa/32/13691_2.png) [@Ahmad\_Musaffa](https://discuss.emberjs.com/u/Ahmad_Musaffa)\
**Post date:** [February 2, 2016, 8:37pm UTC](https://discuss.emberjs.com/t/why-is-phantomjs-disconnecting/9636/6 "2016-02-02T20:37:00Z")

</div>

If you can create reproducible steps with a vanilla ember app, then let the [dev] ([Fixes crash on missing items in dev mode by johanneswuerbach · Pull Request #764 · testem/testem · GitHub](https://github.com/testem/testem/pull/764)) know.

---

<div class="post-metadata">

**Author:** ![johannesw](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/johannesw/32/15029_2.png) [@johannesw](https://discuss.emberjs.com/u/johannesw)\
**Post date:** [February 2, 2016, 9:16pm UTC](https://discuss.emberjs.com/t/why-is-phantomjs-disconnecting/9636/7 "2016-02-02T21:16:02Z")

</div>

Testem maintainer here 👋

> [@jamesarosen](#):
>
> Print STDOUT, STDERR
> 
> I tried monkey-patching Testem to print STDOUT and STDERR when it exits, but they were empty.

I tried the same, but sadly phantomjs isn’t logging anything so I skipped the logging for now. If somebody knows how to get more details from phantom before it crashes I’m happy to change that in testem. PhantomJS debugging is currently rough.

> [@jamesarosen](#):
>
> I’m at a loss. I can’t think of anything that would cause the socket to disconnect that wouldn’t also crash the browser. There’s no code that navigates away from the page or closes the window. The failures happen only sporadically – always on an acceptance test, but on many different ones. If anyone has any suggestions, please let me know.

The lost socket connection looks more “promising” as test runs are usually really stressing the browser and [socket.io](http://socket.io) might have some to small timeouts. The previous testem version was somehow hiding this by always reconnecting **and restarting** your tests, which looks better in CI, but can be super expensive with bigger test suites.

I’ll look into the timeout issue as soon as time permits.

---

<div class="post-metadata">

**Author:** ![johannesw](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/johannesw/32/15029_2.png) [@johannesw](https://discuss.emberjs.com/u/johannesw)\
**Post date:** [February 2, 2016, 9:20pm UTC](https://discuss.emberjs.com/t/why-is-phantomjs-disconnecting/9636/8 "2016-02-02T21:20:15Z")

</div>

Testem issue, please add any additional details here: [https://github.com/testem/testem/issues/777](https://github.com/testem/testem/issues/777)
