Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

retry CRI.List when connecting to the browser #6133

Merged
merged 6 commits into from
Jan 13, 2020
Merged

Conversation

bahmutov
Copy link
Contributor

@bahmutov bahmutov commented Jan 9, 2020

User facing changelog

The CI runs should stop failing due to Cypress not being able to find the tab to control.

Additional details

I did experiments with closing and opening Chrome Canary with lots of tabs and extensions and could get into situations where the initial CRI.List call returns no targets, but the next call returns full list of tabs. Adding retries seems like a solution we need to try.

How has the user experience changed?

No changes, unless the user is running Cypress with debug logs DEBUG=cypress:server:protocol. In that case they should see several calls

  cypress:server:protocol CRI.List on port 56428 +317ms
  cypress:server:protocol CRI List { numTargets: 1, targets: [ { description: '', devtoolsFrontendUrl: '/devtools/inspector.html?ws=localhost:56428/devtools/page/2FCCB2A0FA8B1DCD5E1CDC2849A6A326', id: '2FCCB2A0FA8B1DCD5E1CDC2849A6A326', title: 'Sortify', type: 'background_page', url: 'chrome-extension://aehmpfeibpcclkbjjkclhncmjncpebef/_generated_background_page.html', webSocketDebuggerUrl: 'ws://localhost:56428/devtools/page/2FCCB2A0FA8B1DCD5E1CDC2849A6A326' } ] } +88ms
  cypress:server:protocol CRI.List on port 56428 +1s
  cypress:server:protocol CRI List { numTargets: 10,
...

PR Tasks

  • Have tests been added/updated?
  • Has the original issue been tagged with a release in ZenHub?

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jan 9, 2020

Thanks for the contribution! Below are some guidelines Cypress uses when doing PR reviews.

  • Please write [WIP] in the title of your Pull Request if your PR is not ready for review - someone will review your PR as soon as the [WIP] is removed.
  • Please familiarize yourself with the PR Review Checklist and feel free to make updates on your PR based on these guidelines.

PR Review Checklist

If any of the following requirements can't be met, leave a comment in the review selecting 'Request changes', otherwise 'Approve'.

User Experience

  • The feature/bugfix is self-documenting from within the product.
  • The change provides the end user with a way to fix their problem (no dead ends).

Functionality

  • The code works and performs its intended function with the correct logic.
  • Performance has been factored in (for example, the code cleans up after itself to not cause memory leaks).
  • The code guards against edge cases and invalid input and has tests to cover it.

Maintainability

  • The code is readable (too many nested 'if's are a bad sign).
  • Names used for variables, methods, etc, clearly describe their function.
  • The code is easy to understood and there are relevant comments explaining.
  • New algorithms are documented in the code with link(s) to external docs (flowcharts, w3c, chrome, firefox).
  • There are comments containing link(s) to the addressed issue (in tests and code).

Quality

  • The change does not reimplement code.
  • There's not a module from the ecosystem that should be used instead.
  • There is no redundant or duplicate code.
  • There are no irrelevant comments left in the code.
  • Tests are testing the code’s intended functionality in the best way possible.

Internal

  • The original issue has been tagged with a release in ZenHub.

@cypress
Copy link

cypress bot commented Jan 9, 2020



Test summary

3609 0 47 0


Run details

Project cypress
Status Passed
Commit 94624a4
Started Jan 13, 2020 2:39 PM
Ended Jan 13, 2020 2:44 PM
Duration 04:34 💡
OS Linux Debian - 9.11
Browser Multiple

View run in Cypress Dashboard ➡️


This comment has been generated by cypress-bot as a result of this project's GitHub integration settings. You can manage this integration in this project's settings in the Cypress Dashboard

@bahmutov bahmutov changed the title WIP: retry CRI.List when connecting to the browser retry CRI.List when connecting to the browser Jan 9, 2020
@bahmutov bahmutov requested a review from flotwig January 9, 2020 21:26
@bahmutov
Copy link
Contributor Author

bahmutov commented Jan 9, 2020

Funny enough, I see empty targets right now in another recipe with Chrome

  (Run Starting)

  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ Cypress:    3.8.1                                                                              │
  │ Browser:    Chrome 79 (headless)                                                               │
  │ Specs:      1 found (spec.js)                                                                  │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘


────────────────────────────────────────────────────────────────────────────────────────────────────
                                                                                                    
  Running:  spec.js                                                                         (1 of 1)
  cypress:server:protocol ensureRdpPort 59547 +0ms
  cypress:server:protocol Chrome arguments [ '--test-type', '--ignore-certificate-errors', '--start-maximized', '--silent-debugger-extension-api', '--no-default-browser-check', '--no-first-run', '--noerrdialogs', '--enable-fixed-layout', '--disable-popup-blocking', '--disable-password-generation', '--disable-save-password-bubble', '--disable-single-click-autofill', '--disable-prompt-on-repos', '--disable-background-timer-throttling', '--disable-renderer-backgrounding', '--disable-renderer-throttling', '--disable-restore-session-state', '--disable-translate', '--disable-new-profile-management', '--disable-new-avatar-menu', '--allow-insecure-localhost', '--reduce-security-for-testing', '--enable-automation', '--disable-device-discovery-notifications', '--disable-infobars', '--autoplay-policy=no-user-gesture-required', '--disable-site-isolation-trials', '--metrics-recording-only', '--disable-prompt-on-repost', '--disable-hang-monitor', '--disable-sync', '--disable-web-resources', '--safebrowsing-disable-auto-update', '--safebrowsing-disable-download-protection', '--disable-client-side-phishing-detection', '--disable-component-update', '--disable-default-apps', '--use-fake-ui-for-media-stream', '--use-fake-device-for-media-stream', '--disable-ipc-flooding-protection', '--disable-backgrounding-occluded-window', '--disable-breakpad', '--password-store=basic', '--use-mock-keychain', '--proxy-server=http://localhost:59544', '--proxy-bypass-list=<-loopback>', '--headless', '--remote-debugging-port=59547', '--remote-debugging-address=127.0.0.1' ] +1ms
  cypress:server:protocol Getting WS connection to CRI on port 59547 +0ms
  cypress:server:protocol CRI.List on port 59547 +107ms
  cypress:server:protocol CRI List { numTargets: 0, targets: [] } +17ms
could not find CRI target
Error: could not find CRI target
    at lazyAssLogic (/Users/gleb/Library/Caches/Cypress/3.8.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/lazy-ass/index.js:110:14)
    at lazyAss (/Users/gleb/Library/Caches/Cypress/3.8.1/Cypress.app/Contents/Resources/app/packages/server/node_modules/lazy-ass/index.js:115:28)
    at /Users/gleb/Library/Caches/Cypress/3.8.1/Cypress.app/Contents/Resources/app/packages/server/lib/browsers/protocol.js:81:5

So the message could be in your case

Retrying 20/33...
Retrying 21/33...
could not find CRI target

But I seriously doubt it now. We could keep the current retry in this PR and add a message there https://github.com/sindresorhus/p-retry#onfailedattempterror but again I doubt it will give you much

Retrying 20/33...
Retrying 21/33...
tried finding CRI target 10 times, failed
could not find CRI target

@bahmutov
Copy link
Contributor Author

bahmutov commented Jan 9, 2020

Oh, one last thing. Monkey-patching 3.8.1 binary using new code in protocol.js fixes the above failure

  cypress:server:protocol Getting WS connection to CRI on port 59700 +0ms
  cypress:server:protocol CRI.List on port 59700 +108ms
  cypress:server:protocol CRI List { numTargets: 0, targets: [] } +26ms
  cypress:server:protocol CRI.List on port 59700 +1s
  cypress:server:protocol CRI List { numTargets: 1, targets: [ { description: '', devtoolsFrontendUrl: '/devtools/inspector.html?ws=localhost:59700/devtools/page/9BD87A144C2102288BDFF45FB6113D98', id: '9BD87A144C2102288BDFF45FB6113D98', title: 'about:blank', type: 'page', url: 'about:blank', webSocketDebuggerUrl: 'ws://localhost:59700/devtools/page/9BD87A144C2102288BDFF45FB6113D98' } ] } +3ms
  cypress:server:protocol found CRI target { description: '', devtoolsFrontendUrl: '/devtools/inspector.html?ws=localhost:59700/devtools/page/9BD87A144C2102288BDFF45FB6113D98', id: '9BD87A144C2102288BDFF45FB6113D98', title: 'about:blank', type: 'page', url: 'about:blank', webSocketDebuggerUrl: 'ws://localhost:59700/devtools/page/9BD87A144C2102288BDFF45FB6113D98' } +1ms

👀 on the 🏀

@flotwig
Copy link
Contributor

flotwig commented Jan 10, 2020

@bahmutov I updated the way it retries to incorporate the delay messaging & added tests that it console.logs as expected.

I tried using p-retry for this, but the onFailedAttempt callback isn't well-documented and I didn't feel like poking at its behavior would be worth it when it's easy just to write the retry code inline.

@bahmutov
Copy link
Contributor Author

@flotwig yeah, now you just need to use extra variables and write custom code, I understand it might feel simple right now. But I don't want to argue about it any more.

We have an example of failing CRI target in GH action! https://github.com/cypress-io/cypress-example-kitchensink/runs/383684300

@erezrokah
Copy link

Looks like I'm a bit late to the party, but instead of polling one could listen to the Target.targetCreated/Target.targetInfoChanged events.
You can see this approach here: erezrokah@470f051#diff-173311a633635f1f07ee99371b5c362dR61

Copy link
Contributor

@flotwig flotwig left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, but I'm not sure how to test locally. @bahmutov it would be great to test this one last time with your super super slow Chrome setup and ensure that it still works.

@bahmutov
Copy link
Contributor Author

I have tested this branch using cypress-example-recipe by putting the protocol.js into global binary 3.8.2 and running

~/git/cypress-example-recipes/examples/fundamentals__chrome-remote-debugging on master
$ DEBUG=cypress:server:protocol npm run cypress:run:chrome

> chrome-remote-debugging@1.0.0 cypress:run:chrome /Users/gleb/git/cypress-example-recipes/examples/fundamentals__chrome-remote-debugging
> ../../node_modules/.bin/cypress run --headless --browser chrome


====================================================================================================

  (Run Starting)

  ┌────────────────────────────────────────────────────────────────────────────────────────────────┐
  │ Cypress:    3.8.2                                                                              │
  │ Browser:    Chrome 79 (headless)                                                               │
  │ Specs:      1 found (spec.js)                                                                  │
  └────────────────────────────────────────────────────────────────────────────────────────────────┘


────────────────────────────────────────────────────────────────────────────────────────────────────
                                                                                                    
  Running:  spec.js                                                                         (1 of 1)
  cypress:server:protocol ensureRdpPort 59465 +0ms
  cypress:server:protocol Chrome arguments [ '--test-type', '--ignore-certificate-errors', '--start-maximized', '--silent-debugger-extension-api', '--no-default-browser-check', '--no-first-run', '--noerrdialogs', '--enable-fixed-layout', '--disable-popup-blocking', '--disable-password-generation', '--disable-save-password-bubble', '--disable-single-click-autofill', '--disable-prompt-on-repos', '--disable-background-timer-throttling', '--disable-renderer-backgrounding', '--disable-renderer-throttling', '--disable-restore-session-state', '--disable-translate', '--disable-new-profile-management', '--disable-new-avatar-menu', '--allow-insecure-localhost', '--reduce-security-for-testing', '--enable-automation', '--disable-device-discovery-notifications', '--disable-infobars', '--autoplay-policy=no-user-gesture-required', '--disable-site-isolation-trials', '--metrics-recording-only', '--disable-prompt-on-repost', '--disable-hang-monitor', '--disable-sync', '--disable-web-resources', '--safebrowsing-disable-auto-update', '--safebrowsing-disable-download-protection', '--disable-client-side-phishing-detection', '--disable-component-update', '--disable-default-apps', '--use-fake-ui-for-media-stream', '--use-fake-device-for-media-stream', '--disable-ipc-flooding-protection', '--disable-backgrounding-occluded-window', '--disable-breakpad', '--password-store=basic', '--use-mock-keychain', '--proxy-server=http://localhost:59462', '--proxy-bypass-list=<-loopback>', '--headless', '--remote-debugging-port=59465', '--remote-debugging-address=127.0.0.1' ] +1ms
  cypress:server:protocol Getting WS connection to CRI on port 59465 +0ms
  cypress:server:protocol attempting to find CRI target... { retryIndex: 1 } +207ms
  cypress:server:protocol CRI.List { host: '127.0.0.1', port: 59465, getDelayMsForRetry: [Function: getDelayMsForRetry] } +2ms
  cypress:server:protocol CRI List { numTargets: 0, targets: [] } +22ms
  cypress:server:protocol error finding CRI target, maybe retrying { delay: 100, err: Error: could not find CRI target at lazyAssLogic (/Users/gleb/Library/Caches/Cypress/3.8.2/Cypress.app/Contents/Resources/app/packages/server/node_modules/lazy-ass/index.js:110:14) at lazyAss (/Users/gleb/Library/Caches/Cypress/3.8.2/Cypress.app/Contents/Resources/app/packages/server/node_modules/lazy-ass/index.js:115:28) at findStartPage (/Users/gleb/Library/Caches/Cypress/3.8.2/Cypress.app/Contents/Resources/app/packages/server/lib/browsers/protocol.js:58:3)     at processTicksAndRejections (internal/process/task_queues.js:85:5) } +0ms
  cypress:server:protocol attempting to find CRI target... { retryIndex: 2 } +101ms
  cypress:server:protocol CRI.List { host: '127.0.0.1', port: 59465, getDelayMsForRetry: [Function: getDelayMsForRetry] } +0ms
  cypress:server:protocol CRI List { numTargets: 1, targets: [ { description: '', devtoolsFrontendUrl: '/devtools/inspector.html?ws=localhost:59465/devtools/page/0AE23AD5873121DC5BC9153E5C7BD315', id: '0AE23AD5873121DC5BC9153E5C7BD315', title: '', type: 'page', url: 'about:blank', webSocketDebuggerUrl: 'ws://localhost:59465/devtools/page/0AE23AD5873121DC5BC9153E5C7BD315' } ] } +2ms
  cypress:server:protocol found CRI target { description: '', devtoolsFrontendUrl: '/devtools/inspector.html?ws=localhost:59465/devtools/page/0AE23AD5873121DC5BC9153E5C7BD315', id: '0AE23AD5873121DC5BC9153E5C7BD315', title: '', type: 'page', url: 'about:blank', webSocketDebuggerUrl: 'ws://localhost:59465/devtools/page/0AE23AD5873121DC5BC9153E5C7BD315' } +1ms


  Test
    ✓ Printable div is not visible normally (137ms)

Before, it was failing, but now it is retrying correctly.

@flotwig
Copy link
Contributor

flotwig commented Jan 13, 2020

Looks like I'm a bit late to the party, but instead of polling one could listen to the Target.targetCreated/Target.targetInfoChanged events.

@erezrokah Thanks! I've created an issue here to track that: #6153.

Since this PR already has tests and has been manually confirmed, we'll merge it for now, but using the CDP events would be an improvement since it could cut down on the potential slowdown from retrying at an interval.

@erezrokah
Copy link

Thanks @flotwig, I'm just glad to see it resolved :)

@noamtamir
Copy link

When can we expect this fix to be released?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Error: could not find CRI target / Failed to connect to Chrome
4 participants