cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
46.77k stars 3.16k forks source link

Cypress Error Socket Hang Up on NodeJS requests. #7037

Open RicardoVaranda opened 4 years ago

RicardoVaranda commented 4 years ago

Current behavior:

When importing a nodeJS library or file that uses request, when we attempt to do a request we are getting:

socket hang up
    at connResetException (internal/errors.js:559:14)
    at TLSSocket.socketOnEnd (_http_client.js:433:23)
    at TLSSocket.emit (events.js:208:15)
    at endReadableNT (_stream_readable.js:1168:12)
    at processTicksAndRejections (internal/process/task_queues.js:77:11) {
  code: 'ECONNRESET',

When running this same file via node test.js it runs successfully.

Desired behavior:

It should work the same way when node executes the file and when Cypress calls the file.

Test code to reproduce

In Plugins > index.js

var api = require('api-helper.js');

module.exports = (on, config) => {
  on('task', {
   test: async () => {
   const result = await api.testEndpoint();
   console.log('Result is: ', result);
  }
  });
  return config;
};

Plugins > api-helper.js

var request = require('request');

class ApiHelper  {

constructor() {}

    testEndpoint(){
        return request.get("http://ourApiEndpoint.com/somethinghere/something");
    }

}

var test = new ApiHelper();

test.testEndpoint();

Now if we run this api-helper.js file using node in the cli like:

node api-helper.js

It runs successfully, when it's called through cypress we receive the following error:

Error: socket hang up
    at connResetException (internal/errors.js:559:14)
    at TLSSocket.socketOnEnd (_http_client.js:433:23)
    at TLSSocket.emit (events.js:208:15)
    at endReadableNT (_stream_readable.js:1168:12)
    at processTicksAndRejections (internal/process/task_queues.js:77:11)

Couple of disclaimers and clarifications.

1) We are not not using cy.request because we need to do certain request sending a certificate pfx file which is not supported curently. 2) We are obviously not running the code as stated above to execute as soon as it starts, this is just to demonstrate the error.

Versions

Windows 10 Cypress 4.0.2 Electron 78

jennifer-shehane commented 4 years ago

I see that you are using an older version of Cypress. Please update to the current version of Cypress and let us know if this is still happening for you. Your issue may have already been fixed. Thanks!

RicardoVaranda commented 4 years ago

Hi @jennifer-shehane , Thanks for replying. I've updated to 4.4.0 and can confirm that this issue is still present.

jennifer-shehane commented 4 years ago

This code does not run exactly as provided. Can you double check the code you provided runs with only this code because I've run into several errors that I'm having to fix.

saudixit commented 3 years ago

@brian-mann We are facing same problem. Is there any update on this? @jennifer-shehane For the reference below are the logs from cypress We tried multiple ways like => Cy.request, cy,visit, cy.task but getting same error.

SocketHangup

SocketIssue

If same thing we try using any other framework or tool it works fine. its a simple API call.

fanktom commented 3 years ago

Same here, I'm trying to run against a self-signed CA that is added to the image (curl'ing works), using version cypress/included:6.4.0:

Screen Shot 2021-02-04 at 13 04 45

UPDATE: I was able to solve this by changing my internal port from 80 to something else, e.g. 4000. We had PORT=80 but were using HTTPS, which is technically not a problem, but apparently there is some special logic going on with port 80.

rankun203 commented 3 years ago

Hi, we are seeing the same issue on our end. In our case, the API responded 401 because the client sent a wrong token.

After we fixed the 401, the cy.request passed again.

In this case it would be better to have more meaningful error message other than socket hang up.

idanRiski commented 2 years ago

Please assist to solved this issue out.

I've the same error when I'm sending cy.request POST request to our elasticdb, The request is sent ok and the order is created and rendered in the web application but after that a lot of xhr requests in the background are failed or stucks and the browser is crashed after a few minutes.

Screen Shot 2022-03-12 at 17 59 32 Screen Shot 2022-03-12 at 18 01 33

Also in debugging execution I've found the related errors below:

cypress:server:server-base Getting remote state: { auth: null, props: { port: '443', tld: 'com', domain: 'blabla' }, origin: 'https://blabla.bla.com', strategy: 'http', visiting: false, domainName: 'domain.com', fileServer: null }

  cypress:https-proxy received error on client browserSocket { err: Error: read ECONNRESET at TCP.onStreamRead (node:internal/stream_base_commons:211:20) { errno: -54, code: 'ECONNRESET', syscall: 'read' }, url: 'blabla.bla.com:443' } +4s

  cypress:server:util:socket_allowed allowed socket closed, removing { localPort: 58066 } +4s
  cypress:server:util:socket_allowed allowed socket closed, removing { localPort: 58063 } +0ms
  cypress:server:util:socket_allowed allowed socket closed, removing { localPort: 57978 } +0ms
  cypress:server:request aborting { requestId: 'request19' } +5s
  cypress:server:request aborting { requestId: 'request20' } +0ms
  cypress:server:request aborting { requestId: 'request16' } +1ms
  cypress:server:request aborting { requestId: 'request17' } +0ms
  cypress:server:request aborting { requestId: 'request15' } +0ms

  cypress:server:util:socket_allowed allowed socket closed, removing { localPort: 58065 } +5ms
  cypress:https-proxy Got first head bytes { url: 'blabla.bla.com:443', head: '\x16\x03\x01\x02;\x01\x00\x027\x03\x03-8�\x0F!?���\x13�6\x01\x06v-�$=��<(\f��N\x04�P �\x7F���O�\x18�&�>�J�\x14:W�V��' } +1m

  cypress:server:request received an error making http request { timeout: undefined, retryIntervals: [], url: 'https://blabla.bla.com/webapp/session/logged_in', requestId: 'request24', retryOnNetworkFailure: true, retryOnStatusCodeFailure: false, delaysRemaining: [], err: Error: socket hang up     at connResetException (node:internal/errors:690:14)     at TLSSocket.socketOnEnd (node:_http_client:471:23)     at TLSSocket.emit (node:events:406:35)     at endReadableNT (node:internal/streams/readable:1331:12)     at processTicksAndRejections (node:internal/process/task_queues:83:21) { code: 'ECONNRESET' } } +80ms
  cypress:server:request exhausted all attempts retrying request { timeout: undefined, retryIntervals: [], url: 'https://blabla.bla.com/webapp/session/logged_in', requestId: 'request24', retryOnNetworkFailure: true, retryOnStatusCodeFailure: false, delaysRemaining: [], err: Error: socket hang up     at connResetException (node:internal/errors:690:14)     at TLSSocket.socketOnEnd (node:_http_client:471:23)     at TLSSocket.emit (node:events:406:35)     at endReadableNT (node:internal/streams/readable:1331:12)     at processTicksAndRejections (node:internal/process/task_queues:83:21) { code: 'ECONNRESET' } } +1ms
  cypress:proxy:http:error-middleware error proxying request { error: Error: socket hang up at connResetException (node:internal/errors:690:14)     at TLSSocket.socketOnEnd (node:_http_client:471:23)     at TLSSocket.emit (node:events:406:35)     at endReadableNT (node:internal/streams/readable:1331:12)     at processTicksAndRejections (node:internal/process/task_queues:83:21) { code: 'ECONNRESET' }, url: '/webapp/session/logged_in', headers: { host: 'playground.riskxint.com', connection: 'keep-alive', 'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="99", "Google Chrome";v="99"', 'sec-ch-ua-mobile': '?0', 'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36', 'sec-ch-ua-platform': '"macOS"', accept: '*/*', 'sec-fetch-site': 'same-origin', 'sec-fetch-mode': 'cors', 'sec-fetch-dest': 'empty', referer: 'https://playground.riskxint.com/app/login?date_field=created_at&start_date=2022-02-09T22:00:00.000Z&end_date=2022-03-12T16:10:16.060Z&dimension=billing_country_code&filters_id=&kpi_calc_method=count', 'accept-encoding': 'gzip', 'accept-language': 'en-US,en;q=0.9', cookie: '_ga=GA1.2.742975187.1647101413; _gid=GA1.2.1071170102.1647101413; _gat=1' } } +0ms
  cypress:server:request aborting { requestId: 'request14' } +3ms

...

Cypress version: 9.3.0 Chrome: 99 OS: Mac Monteray 12.0.1

@jennifer-shehane 🙏🏽

ImanEshraghi commented 2 years ago

I'm having a similar issue since two days ago. My cypress test runner opens up and I click on a single spec, test starts running but at some point the test runner shuts down and I see the error below on the console: Error: read ECONNRESET at TCP.onStreamRead (node:internal/stream_base_commons:211:20) { errno: -4077, code: 'ECONNRESET', syscall: 'read' } Error: read ECONNRESET at TCP.onStreamRead (node:internal/stream_base_commons:211:20) image

The strange thing is that I didn't have this issue two days ago but suddenly started noticing this. I also upgraded cypress to the latest version of '9.5.4', but had no luck in solving this issue. image

Can you please help with some directions in solving this issue?

vr0204 commented 1 year ago

Hi Team, I am facing the Socket Hang up issue when using the Cypress HTTP requests. Request made using Cypress -

utils.request.fetch = async function fetch(url, options) {
  options = { url, retryOnNetworkFailure: false, ...options };
  return Cypress.backend('http:request', options);
};

Error Received:

RequestError: Error: socket hang up
  at new RequestError (/root/.cache/Cypress/10.6.0/Cypress/resources/app/node_modules/request-promise-core/lib/errors.js:14:15)
  at Request.plumbing.callback (/root/.cache/Cypress/10.6.0/Cypress/resources/app/node_modules/request-promise-core/lib/plumbing.js:87:29)
  at Request.RP$callback [as _callback] (/root/.cache/Cypress/10.6.0/Cypress/resources/app/node_modules/request-promise-core/lib/plumbing.js:46:31)
  at self.callback (/root/.cache/Cypress/10.6.0/Cypress/resources/app/node_modules/@cypress/request/request.js:183:22)
  at Request.emit (node:events:526:28)
  at Request.emit (node:domain:475:12)
  at Request.onRequestError (/root/.cache/Cypress/10.6.0/Cypress/resources/app/node_modules/@cypress/request/request.js:869:8)
  at ClientRequest.emit (node:events:526:28)
  at ClientRequest.emit (node:domain:475:12)
  at Socket.socketOnEnd (node:_http_client:466:9)
  at Socket.emit (node:events:538:35)
  at Socket.emit (node:domain:475:12)
  at endReadableNT (node:internal/streams/readable:1345:12)
  at processTicksAndRejections (node:internal/process/task_queues:83:21)

The issue doesn't occur on every run. It is just a random sporadic event. And I believe this issue is related to this thread. Just checking, if we have any work around or solutions for resolving this issue. I am using cypress 10.6. Would upgrading to latest version of Cypress help in resolving this issue. as upgrading would be a time consuming task, would like gain some confidence before commencing that. Any help would be highly appreciated.

cypress-app-bot commented 1 year ago

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

sharmilajesupaul commented 1 year ago

Hey just reporting that we see this issue too on Cypress v12. It's sporadic, and seems to happen on failed network requests. Is there anyway to prevent Cypress from crashing in this instance?

nagash77 commented 1 year ago

@sharmilajesupaul thanks for confirming you are still seeing this issue sporadically. Can you provide a bit more detail about when you see these hangs? Is it the same request each time? What are the requests doing? Are you seeing anything in your debug logs? Is it in the same spec each time? The same test? The more details you can provide the better.

janaki commented 1 year ago

Hi @nagash77, Our team has seen this a few different times. In my case, I had a test that authenticated, then executed a cy.pause(), while I did an interaction (clicked through a modal).

The test will run a few times and then I will see it crash, similar to what others have posted above. Others on my team have seen this after a few test runs. Unfortunately, I couldn't capture the debug logs in time. I'll try and capture more details once this happens again. Here is a screenshot of the crash, if that helps:

Screenshot 2023-07-26 at 3 53 25 PM
nagash77 commented 1 year ago

@janaki it seems like something is happening to the socket before the test is finished. Can you share the test that was running while this crash happened? Is there something that is starting/stopping the AUT during your test runs? Is it possible someone is deploying code while your tests are running?

At this point we don't have enough info to investigate further.

nickgrout commented 1 year ago

Hi @nagash77 , I work with @janaki and have more context on this issue. We captured a more detailed stack trace with DEBUG=cypress:* turned on, but it contains some sensitive information. May I email you the detailed stack trace directly so it's not shared publicly?

We're thinking it is something due to websockets with our AUT.

nickgrout commented 1 year ago

Hi @nagash77 , I wanted to share that this is still an issue, but using electron as our test runner seems to help.

Also I realize I did not share my versions: Chrome version: 115.0.5790.114 Cypress version: 12.9.0 (seems to be reproducible with multiple versions of cy though)

nickgrout commented 1 year ago

Hi @brian-mann , wondering if you can take a look? This issue is causing a lot of issues for us for an application we're trying to test.

nickgrout commented 1 year ago

Hi @chrisbreiding , wondering if you can take a look? This issue is blocking our use of cypress for an application we're trying to test.

UgurGumushan commented 1 year ago

I also am experiencing this issue The project works on node 16 We can't easily upgrade to 18+ MacOS Ventura 13.5 I think node version may be relevant This issue doesn't occur on Ubuntu Github Runner

UgurGumushan commented 1 year ago

I figured out that the container that the request is sent to, runs out of memory and I've increased the memory size of Docker and this is resolved.

nagash77 commented 1 year ago

Hi @nickgrout is this separate from the other issue you have filed in the repo?

joshkaplan commented 1 year ago

@nagash77 no it's not separate. upon closer inspection, we realized that the behavior we were seeing did not exactly line up with some of the other activity in this thread, so we thought it would be best to open a new one.

apologies for the confusion!

MisbahAmaan commented 8 months ago

Hey! I am facing the same issue "socket hang up" while using it with GitHub actions. Cypress: 13.6.3
Browser: Chrome 120 (headless)
Node Version: v20.8.1 (/home/runner/runners/2.311.0/externals/node20/bin/node)

ERROR Message 1) "before each" hook for "test case": CypressError: cy.request() failed trying to load:

We attempted to make an http request to this URL but the request failed without a response.

We received this error at the network level:

Error: socket hang up


The request we sent was:

Method: POST URL: http://--------


Common situations why this would fail:

aisyahhz163 commented 4 months ago

Got the same error, already updated to the latest version of Cypress, and the error are randomly occurs at different test cases each run.

Screenshot (23) Screenshot (25)

MisbahAmaan commented 4 months ago

i was getting the same error but I fixed it just increase its maximum time on GitHub and It will solve this issue