Cypress: Cypress could not connect to Firefox. `Error: cannot open socket` displayed at beginning of running spec

Created on 10 Feb 2020  Β·  39Comments  Β·  Source: cypress-io/cypress

When running cypress with options '--headless --browser firefox', cypress halts at the start of the second spec with message:

Cypress could not connect to Firefox.

An unexpected error was received from Marionette connection:

Error: cannot open socket

To avoid this error, ensure sure that there are no other instances of Firefox launched by Cypress running.

This blocks running all specs in 'run' mode
Windows 10, Firefox version 72 (64 bit)

┆Issue is synchronized with this Jira Bug by Unito

windows internal-priority bug v4.0.1

Most helpful comment

I have the same issue with cypress 4.5.0 running on Linux on a Microsoft hosted agent in Azure Devops.

Edit: I have this when trying to two runs in the same job, so first for chrome then for Firefox. If I run only for firefox it works, so it seems there is something not closed properly from the first run.

All 39 comments

Apart from this: _fantastic_ that you are working on the Firefox option!

Yes, confirmed on 4.0.1, firefox execution halts between first and second spec.
Running with "npx cypress run --browser firefox"
Windows Server 2016 (64 bit), Firefox version 72

I have not been able to reproduce this exactly as described, where it throws this message in between the first and second spec. I tried with --headless flag and without. Maybe this is related to Windows? cc @flotwig

I can get this to happen for several of the reasons that were happening before for me as described in https://github.com/cypress-io/cypress/issues/6380, but these always fail at or before the first spec file run for me. (also, I can't get these to reproduce in #6400, so I think it's been fixed).

This may be fixed as part of this PR: https://github.com/cypress-io/cypress/pull/6400 There are instructions for pulling down and running the fixes from this PR here: https://github.com/cypress-io/cypress/commit/3007c4f3549dc51b215ead23db10abe856a72717#commitcomment-37216685 If you can pull that down, let us know if that fixes your issue.

Also:

  • Do you have Firefox Developer or Nightly version installed on your system as well?
  • Are you also running Firefox outside of Cypress when you run your tests and see this error?

Do you have Firefox Developer or Nightly version installed on your system as well?
Are you also running Firefox outside of Cypress when you run your tests and see this error?

The answer to both questions is 'no'.
I checked beforehand with the Taskmanager that no firefox processes were running.
I noticed that after a run with firefox (with only one spec), a number of firefox processes stay present in the Taskmanager.
When I run the same spec again with Firefox, without killing these remaining processes, cypress responds with this remark:
~~~
Warning: We failed to remove old browser profiles from previous runs.

This error will not alter the exit code.

Error: EBUSY: resource busy or locked, unlink '\AppData\Roaming\Cypress\cyproduction\browsersfirefox\run-9852\cert9.db'
~~~
The number of remaining Firefox processes have doubled.

Running a spec with headless chrome I see all chrome processes are neatly cleared after the run.

Did not have the opportunity yet to try fix #6400

A bit off-topic (but related, since I would like to try out #6400):
Isn't the dev-builds accessible by npm?

Unfortunately #6400 did not bring the solution :(
running headless firefox resulted in the same message.
When I execute 'run --browser firefox' (not headless) the run ends with the following statement:
~
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:183:27)
{
errno: 'ECONNRESET',
code: 'ECONNRESET',
syscall: 'read'
}
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:183:27)
~

This may not be linked, or may assist with investigation but I found the following behaviour when using the Test Runner with Firefox:

1 - Select to run spec file A
2 - Wait for spec to complete
3 - Without closing Firefox select to run spec file B in test runner
4 - Modal appears stating:
'Firefox is already running, but is not responding. The old Firefox process must be closed to open a new window.'

(Run Starting)

  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚ Cypress:    4.0.1                                                                              β”‚
  β”‚ Browser:    Firefox 9                                                                          β”‚
  β”‚ Specs:      1 found (login.spec.js)                                                            β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜


────────────────────────────────────────────────────────────────────────────────────────────────────

  Running:  login.spec.js                                                                   (1 of 1)

Your project has set the configuration option: `chromeWebSecurity: false`

This option will not have an effect in Firefox. Tests that rely on web security being disabled will not run as expected.
Cypress could not connect to Firefox.

An unexpected error was received from Marionette connection:

Error: cannot open socket

To avoid this error, ensure sure that there are no other instances of Firefox launched by Cypress running.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! Exit status 1
npm ERR! 
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/jenkins/.npm/_logs/2020-02-12T11_53_58_003Z-debug.log

Above log by running in MAC with only one spec. Any solution?

This should be fixed in #6400. In the meantime, you can pass -no-remote via the Browser Launch API while launching Firefox, which should help: https://docs.cypress.io/api/plugins/browser-launch-api.html#Modify-browser-launch-arguments-preferences-and-extensions

still having the issue, even after #6400

Option -no-remote has no effect in a Windows 10 environment. Issue remains. Tested with cypress 4.0.2

+1 - #6400 doesn't fix it as stated above. Firefox stays open at end of run and a dialog pops up stating something about needing to close the browser to continue (sorry don't have a screenshot handy). This happens locally on Windows 10 environment as well as in CI

Just in case if it helps in debugging, here's the screenshot of the error I received.
image

In my case, I had selected Firefox from the dropdown in the Test Runner. And after re-trying it a couple of time, it worked fine.

I observed the similar issue with firefox.

I have test in the gitlab, both chrome and firefox using the imge cypress/included:4.1.0. Test usually takes about 4-5 minutes to finish. However, running the test with firefox 'yarn cypress:run --headless --browser firefox' hangs at the last spec until 1 hour limit of the gitlab.

This PR above did fix some occurances of this issue, but likely there is something else specific to the config, environment or tests that we will need to isolate.

  • What OS are you running in?
  • Can anyone provide the simplest example of test code that you are running that we can run?
  • Any configuration around the tests also.
  • Is this happening during cypress run or cypress open?

Edit: Actually, I would really like to ensure that the -no-remote arg is being passed through in Windows also. Can you paste the following into your plugins/index.js and print what the console log is from the terminal?

module.exports = (on, config) => {
  on('before:browser:launch', (browser = {}, launchOptions) => {
    console.log('LAUNCH OPTION ARGS', launchOptions.args) // print all current args
  })
}

When I run this - it prints the below.

Screen Shot 2020-03-18 at 3 36 05 PM

For me at least this is the details I can share

  • Running on Windows 10
  • Can't share my current setup, but it's nothing fancy. In my first spec: Open up a web page, do some validation. In my second spec: Open up a different web page, do some validation. Fails between specs. (works on Chrome)
  • Vanilla setup, nothing in support/command.js or support/index.js. Nothing (except your part above) in plugins/index.js
  • Running with 'npx cypress run --browser firefox'

My output:
image

Edit:
Tested with both 4.1.0 and 4.2.0
Firefox version 72.0.1 (64 bit)

I can confirm that this still happens.

Versions
Windows 10
Cypress 4.2.0 (Also occurred on 4.1.0, that's the reason I upgraded)
Firefox 74.0 (64 bit) (I also tried with developer edition earlier, but I removed it to rule that out)

Command
node_modules/.bin/cypress run --browser firefox --headless

Launch options

LAUNCH OPTION ARGS [
  '-marionette',
  '-new-instance',
  '-foreground',
  '-start-debugger-server',
  '-no-remote',
  '-headless'
]

cypress.json

{
    "video": false
}

support/index.js

const resizeObserverLoopErrRe = /^ResizeObserver loop limit exceeded/;

Cypress.on("uncaught:exception", err => {
    if (resizeObserverLoopErrRe.test(err.message)) {
        return false;
    }
});

Cypress.Screenshot.defaults({
    screenshotOnRunFailure: false
});

Did anyone found a workaround for this issue? Happens to me as well firefox instance is not being close after first spec

I have the same issue in Jenkins trying to run in parallel chrome and firefox:

Jenkinsfile:
Following the official repo

pipeline {
    agent {
        docker {
            image 'cypress/browsers:node13.6.0-chrome80-ff72' 
            args '-p 3000:3000' 
        }
    }
    stages {
        stage('Install Dependencies') { 
            steps {
                sh 'npm ci'
                sh 'npm run cy:verify'
            }
        }
        stage('parallel tests E2E') {
            parallel {
                stage('Firefox') {
                    steps {
                        sh "npm run cy:run:firefox"
                    }
                }
                stage('Chrome') {
                    steps {
                        sh "npm run cy:run:chrome"
                    }
                }
            }
        }
    }
}

package.json

"cy:verify": "cypress verify",
 "cy:run:chrome": "cypress run --spec \"**/*.feature\" --browser chrome --headless",
 "cy:run:firefox": "cypress run --spec \"**/*.feature\" --browser firefox --headless"

Firefox options

Cypress:    4.1.0                                                                              
Browser:    Firefox 72 (headless)
LAUNCH OPTION ARGS [
  '-marionette',
  '-new-instance',
  '-foreground',
  '-start-debugger-server',
  '-no-remote',
  '-headless'
]

But if I only run the Firefox browser, everything works fine :

pipeline {
    agent {
        docker {
            image 'cypress/browsers:node13.6.0-chrome80-ff72' 
            args '-p 3000:3000' 
        }
    }
    stages {
        stage('Install Dependencies') { 
            steps {
                sh 'npm ci'
                sh 'npm run cy:verify'
            }
        }
        stage('Firefox') {
            steps {
                sh "npm run cy:run:firefox"
            }
        }
    }
}

Same issue occurs in Github Actions as well. Firefox execution hangs forever, until cancelled manually.

Firefox cypress prefs:

[
  '-marionette',
  '-new-instance',
  '-foreground',
  '-start-debugger-server',
  '-no-remote',
  '-headless'
]

Actions yml file

  firefox:
    runs-on: ubuntu-16.04
    container:
      image: cypress/browsers:node12.13.0-chrome80-ff74
      options: --user 1001

    steps:
      - uses: actions/checkout@v1

      - name: npm install
        run: |
          npm install

      - name: npm run
        run: |
          nohup npm start &>/dev/null &

      - name: cypress firefox all test
        run: |
          npm run test:firefox

any update on this? we get the same issue when running cypress within the azure devops pipeline, using firefox browser

Still existing in 4.4.0

There are no updates on this issue. An issue will be closed when it is fixed with a comment on what version it's released in. There's no need to comment asking for updates if neither of these actions is taken.

So my issue #7159 has been marked as a duplicate, but it seems to me that the easily reproducible example that I took the time to set-up has purely been ignored... I will copy it here, hoping that it can actually be useful...

Repository: https://github.com/Seb-C/test-cypress-action
CI script: https://github.com/Seb-C/test-cypress-action/blob/master/.github/workflows/tests.yml
(all tests are the default one when we first do cypress open in a new project)
Runs history (showing the randomness): https://github.com/Seb-C/test-cypress-action/actions
Logs of a failed run: https://github.com/Seb-C/test-cypress-action/runs/625344325?check_suite_focus=true

I checked beforehand with the Taskmanager that no firefox processes were running.
I noticed that after a run with firefox (with only one spec), a number of firefox processes stay present in the Taskmanager.
When I run the same spec again with Firefox, without killing these remaining processes, cypress responds with this remark:

Warning: We failed to remove old browser profiles from previous runs.

This error will not alter the exit code.

Error: EBUSY: resource busy or locked, unlink '<path>\AppData\Roaming\Cypress\cy\production\browsers\firefox\run-9852\cert9.db'

The number of remaining Firefox processes have doubled.

Running a spec with headless chrome I see all chrome processes are neatly cleared after the run.

Same here. Running specs in Firefox from ci resulted in the same errors as described in this issue, as well as (in the timespan of a week) more than 80 running Firefox processes in the task manager eating up memory.

I have the same issue with cypress 4.5.0 running on Linux on a Microsoft hosted agent in Azure Devops.

Edit: I have this when trying to two runs in the same job, so first for chrome then for Firefox. If I run only for firefox it works, so it seems there is something not closed properly from the first run.

Doing like @Evertvdw mentioned worked for me as well.

Adding to what @Evertvdw said -

Environment: GitHub Actions, ubuntu-latest runner.
When Cypress runs Chrome and then Firefox tests in the same step, testing on FF crashes with the same error as OP posted before the first spec.

I noticed also that, when I run the Firefox tests in its own Job, it fails with the same error. But if I do all the browser test executions in the same Job, starting with the Firefox run, it works!

Weird (and limiting) behaviour...

Update
After consistently failing, I moved the FF job to a larger instance size and it started working. Perhaps there some timeout issue happening, as I don't believe there to be resource contention. We're using GCP and it failed on n1-standard-2, and works on n2-standard-2.
**

I believe I'm also hitting this issue. The Firefox run worked for me a few times, but now consistently fails.

Environment:
Kubernetes 1.14.10
GitLab for running Cypress
Docker Image: cypress/included:4.5.0 for Linux

args: [
     '-marionette',
     '-new-instance',
     '-foreground',
     '-start-debugger-server',
     '-no-remote',
     '-headless',
     '-profile',
     '/root/.config/Cypress/cy/production/browsers/firefox-stable/run-29'
   ]

I have two separate jobs in my pipeline, one for FF and one for Chrome. Oddly enough the FF job just hangs at the end even though it shows it failed. I can then exec into the pod and run commands. I've re-run the cypress run --browser firefox --headless command and everything executes as expected.

This happens consistently and I can pass the debug flag, so happy to provide any additional information if needed.

Excerpt of the final messages on the failed run:

cypress:server:server Got CONNECT request from firefox.settings.services.mozilla.com:443 +174ms
   cypress:https-proxy Writing browserSocket connection headers { url: 'firefox.settings.services.mozilla.com:443', headLength: 0, headers: { 'user-agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0', 'proxy-connection': 'keep-alive', connection: 'keep-alive', host: 'firefox.settings.services.mozilla.com:443' } } +580ms
   cypress:https-proxy Got first head bytes { url: 'firefox.settings.services.mozilla.com:443', head: '\u0016\u0003\u0001\u0002\u0000\u0001\u0000\u0001οΏ½\u0003\u0003\u0000οΏ½\u0019\u0016οΏ½\u0004\u0013}οΏ½οΏ½οΏ½οΏ½οΏ½+οΏ½u_οΏ½[ΟΊοΏ½οΏ½\u0014\u0019οΏ½/kοΏ½|οΏ½οΏ½ ZE^tοΏ½οΏ½\rοΏ½xnWK\u0002-οΏ½MοΏ½`οΏ½οΏ½D' } +12ms
   cypress:network:cors Parsed URL { port: '443', tld: 'com', domain: 'mozilla' } +761ms
   cypress:server:server HTTPS request does not match URL: https://firefox.settings.services.mozilla.com:443 with props: { port: '443', tld: '<redacted>', domain: '<redacted>' } +18ms
   cypress:https-proxy Making connection to firefox.settings.services.mozilla.com:443 +10ms
   cypress:https-proxy getting proxy URL { port: 443, serverPort: 44695, sniPort: 36419, url: 'https://firefox.settings.services.mozilla.com:443' } +4ms
   cypress:server:browsers:firefox-util error in marionette { from: 'connection', err: Error: cannot open socket at Timeout.waitForSocket [as _onTimeout] (/root/.cache/Cypress/4.5.0/Cypress/resources/app/packages/server/node_modules/socket-retry-connect/index.js:37:21)     at listOnTimeout (internal/timers.js:533:17)     at processTimers (internal/timers.js:475:7) } +12s
   cypress:server:cypress exiting with err Error: Cypress could not connect to Firefox.
 An unexpected error was received from Marionette connection:
 Error: cannot open socket
 To avoid this error, ensure that there are no other instances of Firefox launched by Cypress running.
     at Object.get (/root/.cache/Cypress/4.5.0/Cypress/resources/app/packages/server/lib/errors.js:348:11)
     at /root/.cache/Cypress/4.5.0/Cypress/resources/app/packages/server/lib/browsers/firefox-util.js:262:47
  {
   isCypressErr: true,
   type: 'FIREFOX_MARIONETTE_FAILURE',
   details: undefined
 } +36s
   cypress:server:cypress calling exit 1 +2ms
 Cypress could not connect to Firefox.
 An unexpected error was received from Marionette connection:
 Error: cannot open socket
 To avoid this error, ensure that there are no other instances of Firefox launched by Cypress running.

I am able to reproduce the timeout connecting to the second launch of Firefox on Windows, but I only ever get the EBUSY: resource busy or locked, not the Error: cannot open socket error... using MOZ_FORCE_DISABLE_E10S=1 does not seem to help.


Let's limit discussion in this issue to the bug affecting Windows users only, where Cypress will fail to launch the second spec file because of an "Error: cannot open socket" error.

@arvinder06 @chengzh2008 @Guusy @mtayfur-pointr @khitrenovich @mvn-srizzari Discussion about intermittently encountering "Error: cannot open socket" upon launching the first spec file (which I believe all of you are experiencing) can be moved to this issue: #7159


 Browser:    Firefox 9

Above log by running in MAC with only one spec. Any solution?

@arvinder06 Are you truly using Firefox 9? Try upgrading to the latest version, it's unlikely that a version that old will work.

Cypress version 4.6.0 did not solve this problem :(
After the first test, Firefox is still running, preventing the next test to start.
With every next test I get a popup from "XULRunner" : "Firefox is already active ", resulting in a whole stack of popups.
CypressFF

Maybe it will help someone.

  • (plugin config step) during test I generate special flag - instanceId (cypress.CYPRESS_GROUP.TIMESTAMP)
  • (plugin before:browser:launch) before start of firefox I append this flag to command that starts browser as --instanceId=INSTANCE_ID
  • (support after) after suite is done (one spec file) I store that instanceId to cypress/tmp/firefox/CYPRESS_GROUP.instanceId
  • then npm run killFirefoxWin (nodejs custom script based on ps-node) got to be executed. It looks for CYPRESS_GROUP in environment (or group field in cypress.env.json), if not found - "general" string will be used instead. It tries to find .instanceId file and based on instanceId from it - find main firefox process, kill it and check if any other child processes (children are linked to parent process by --channel parameter) exist - will kill them if yes.
  • in case file with instanceId is not found - fallback to kill all processes with name firefox.exe

I know that it is ugly, but I had to make it work.

When I try running my test in firefox (both in test runner and command line) I get the following error Error: connect ECONNREFUSED 127.0.0.1:52415 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1128:14). I have tried upgrading my firefox version (Currently on 56.0) and updating my cypress (4.8.0). I am running my test on windows 10. I think this may be a similar issue?

image

Fixed this by upgrading firefox to 77.0.1.

Maybe it will help someone.

  • (plugin config step) during test I generate special flag - instanceId (cypress.CYPRESS_GROUP.TIMESTAMP)
  • (plugin before:browser:launch) before start of firefox I append this flag to command that starts browser as --instanceId=INSTANCE_ID
  • (support after) after suite is done (one spec file) I store that instanceId to cypress/tmp/firefox/CYPRESS_GROUP.instanceId
  • then npm run killFirefoxWin (nodejs custom script based on ps-node) got to be executed. It looks for CYPRESS_GROUP in environment (or group field in cypress.env.json), if not found - "general" string will be used instead. It tries to find .instanceId file and based on instanceId from it - find main firefox process, kill it and check if any other child processes (children are linked to parent process by --channel parameter) exist - will kill them if yes.
  • in case file with instanceId is not found - fallback to kill all processes with name firefox.exe

I know that it is ugly, but I had to make it work.

@drumslave-git ,

I liked your idea of killing the instances by writing some custom script which would atleast serve as a workaround until it is fixed by cypress team.
Can you please let me know where you are actually invoking the custom nodejs script npm run killFirefoxWin? From your comments it's not exactly clear and it would be better if you can share some code snippet on the same.

Thanks,
Bhasker

@bhaskerchari not able to share code (company rules).
Cypress is used as a part of android apps automation, part of scenarios got to be executed in browser - done with cypress. So my tests are triggered by Java (testng), after every spec Java also invokes npm run killFirefoxWin.
If you are using JS only - you can write node js custom runner which will do same.

@drumslave-git ,

I am more of interest in what is the event or hook that you have used when you say after every spec you are invoking "npm run killFirefoxWin"

Actually I am using JS and I have tried running the custom script in after () callback of every spec using cy.exec() but it didn't help so that's why interested in where you have invoked the custom script to kill firefox process.

The code for this is done in cypress-io/cypress#7106, but has yet to be released.
We'll update this issue and reference the changelog when it's released.

Released in 4.11.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v4.11.0, please open a new issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jennifer-shehane picture jennifer-shehane  Β·  3Comments

rbung picture rbung  Β·  3Comments

tahayk picture tahayk  Β·  3Comments

dkreft picture dkreft  Β·  3Comments

egucciar picture egucciar  Β·  3Comments