Skip to content

[🐛 Bug]: ws causes webdriver crash when a websocket candidate errors after a different candidate has connected #14742

@pokdeep

Description

@pokdeep

Have you read the Contributing Guidelines on issues?

WebdriverIO Version

9.19.2

Node.js Version

24.7.0

Mode

WDIO local runner

Which capabilities are you using?

What happened?

When the webdriver tries to connect to my selenium grid 4 websocket candidate urls are generated

wss://privatedomain.co.uk/session/0c38ebd974e6e2e9366127b537d25837/se/bidi
wss://10.166.106.101/session/0c38ebd974e6e2e9366127b537d25837/se/bidi
wss://10.166.101.206/session/0c38ebd974e6e2e9366127b537d25837/se/bidi
wss://10.166.109.168/session/0c38ebd974e6e2e9366127b537d25837/se/bidi

the last 3 won't connect as they're private ips on a network I'm not a part of, I can only connect through wss://privatedomain.co.uk

If the last websocket connection promise to resolve is privatedomain.co.uk then everything is fine otherwise webdriver crashes

The cause is when bidi.ts tries to cleanup the sockets. The existing listeners are removed and another one isn't registered to listen to the error event
the following error is thrown

[0-0] SYNCHRONOUS TERMINATION NOTICE: When explicitly exiting the process via process.exit or via a parent process, asynchronous tasks in your exitHooks will not run. Either remove these tasks, use gracefulExit() instead of process.exit(), or ensure your parent process sends a SIGINT to the process running this code.
[0-0] node:events:486
[0-0]       throw er; // Unhandled 'error' event
[0-0]       ^
[0-0] 
[0-0] Error: Unexpected server response: 404
[0-0]     at ClientRequest.<anonymous> (/Users/pokdeep/repos/webapp/automation/node_modules/ws/lib/websocket.js:913:7)
[0-0]     at ClientRequest.emit (node:events:508:28)
[0-0]     at ClientRequest.emit (node:domain:489:12)
[0-0]     at HTTPParser.parserOnIncomingClient (node:_http_client:772:27)
[0-0]     at HTTPParser.parserOnHeadersComplete (node:_http_common:117:17)
[0-0]     at TLSSocket.socketOnData (node:_http_client:614:22)
[0-0]     at TLSSocket.emit (node:events:508:28)
[0-0]     at TLSSocket.emit (node:domain:489:12)
[0-0]     at addChunk (node:internal/streams/readable:559:12)
[0-0]     at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
[0-0] Emitted 'error' event on WebSocket instance at:
[0-0]     at emitErrorAndClose (/Users/pokdeep/repos/webapp/automation/node_modules/ws/lib/websocket.js:1041:13)
[0-0]     at process.processTicksAndRejections (node:internal/process/task_queues:90:21)

What is your expected behavior?

No response

How to reproduce the bug.

I believe this can be recreated via a unit test which resolves/rejects the websocket connection promises in the correct order, bear with me and I'll attempt to create it

Alternatively configure the dns for your websocket url to return several ips which aren't resolvable

Relevant log output

This is the attempt that failed

pokdeep automation % npm run acceptance
> tests@0.0.1 acceptance
> TEST_ENV=$npm_config_testenv BRANCH=$npm_config_branch NODE_TLS_REJECT_UNAUTHORIZED=0 STRICT_SSL=false wdio run config/selenium-grid.conf.ts

Tests have been triggered using test tag @stub for  execution, the environment URL is https://webapp.privatedomain.co.uk using the Selenium Grid at privatedomain.co.uk

Execution of 1 workers started at 2025-09-03T16:08:45.552Z

2025-09-03T16:08:45.561Z INFO @wdio/cli:launcher: Run onPrepare hook
2025-09-03T16:08:45.563Z INFO @wdio/cli:launcher: Run onWorkerStart hook
2025-09-03T16:08:45.563Z INFO @wdio/xvfb: XvfbManager.init() called
2025-09-03T16:08:45.563Z INFO @wdio/xvfb: Xvfb not needed on current platform
2025-09-03T16:08:45.563Z INFO @wdio/local-runner: Start worker 0-0 with arg: run config/selenium-grid.conf.ts
2025-09-03T16:08:45.572Z INFO @wdio/xvfb:ProcessFactory: xvfb-run not found, falling back to regular fork
2025-09-03T16:08:45.572Z INFO @wdio/xvfb:ProcessFactory: ProcessFactory: shouldRun=false, isAvailable=false
2025-09-03T16:08:45.572Z INFO @wdio/xvfb:ProcessFactory: Creating worker process with regular fork
2025-09-03T16:08:45.573Z DEBUG @wdio/local-runner: Send command run to worker with cid "0-0"
[0-0] 2025-09-03T16:08:46.132Z INFO @wdio/local-runner: Run worker command: run
[0-0] Tests have been triggered using test tag @stub for  execution, the environment URL is https://webapp.privatedomain.co.uk using the Selenium Grid at privatedomain.co.uk
[0-0] 2025-09-03T16:08:46.142Z DEBUG @wdio/runner: init remote session
[0-0] 2025-09-03T16:08:46.542Z WARN @wdio/cucumber-framework: 'tagExpression' is deprecated. Use 'tags' instead.
[0-0] RUNNING in chrome - file:///features/otherLinks.feature
[0-0] 2025-09-03T16:08:46.554Z DEBUG @wdio/runner: init remote session
[0-0] 2025-09-03T16:08:46.556Z INFO webdriver: Initiate new session using the WebDriver protocol
[0-0] 2025-09-03T16:08:46.556Z INFO @wdio/utils: Connecting to existing driver at https://privatedomain.co.uk:443/wd/hub
[0-0] 2025-09-03T16:08:46.561Z INFO webdriver: [POST] https://privatedomain.co.uk/wd/hub/session
[0-0] (node:19864) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification.
[0-0] (Use `node --trace-warnings ...` to show where the warning was created)
[0-0] 2025-09-03T16:08:47.717Z INFO webdriver: Register BiDi handler for session with id 0c38ebd974e6e2e9366127b537d25837
[0-0] 2025-09-03T16:08:47.717Z INFO webdriver: Connecting to webSocketUrl wss://privatedomain.co.uk/session/0c38ebd974e6e2e9366127b537d25837/se/bidi
[0-0] 2025-09-03T16:08:47.719Z DEBUG webdriver: Attempt to connect to webSocketUrl wss://privatedomain.co.uk/session/0c38ebd974e6e2e9366127b537d25837/se/bidi
[0-0] 2025-09-03T16:08:47.721Z DEBUG webdriver: Attempt to connect to webSocketUrl wss://10.166.106.101/session/0c38ebd974e6e2e9366127b537d25837/se/bidi
[0-0] 2025-09-03T16:08:47.721Z DEBUG webdriver: Attempt to connect to webSocketUrl wss://10.166.101.206/session/0c38ebd974e6e2e9366127b537d25837/se/bidi
[0-0] 2025-09-03T16:08:47.721Z DEBUG webdriver: Attempt to connect to webSocketUrl wss://10.166.109.168/session/0c38ebd974e6e2e9366127b537d25837/se/bidi
[0-0] 2025-09-03T16:08:47.824Z DEBUG webdriver: Could not connect to Bidi protocol at wss://10.166.106.101/session/0c38ebd974e6e2e9366127b537d25837/se/bidi: Unexpected server response: 404
[0-0] 2025-09-03T16:08:47.824Z DEBUG webdriver: Could not connect to Bidi protocol at wss://10.166.101.206/session/0c38ebd974e6e2e9366127b537d25837/se/bidi: Unexpected server response: 404
[0-0] 2025-09-03T16:08:47.837Z INFO webdriver: Connected to Bidi protocol at wss://privatedomain.co.uk/session/0c38ebd974e6e2e9366127b537d25837/se/bidi
[0-0] 2025-09-03T16:08:47.837Z INFO webdriver: Connected to WebDriver Bidi interface at wss://privatedomain.co.uk/session/0c38ebd974e6e2e9366127b537d25837/se/bidi
[0-0] 2025-09-03T16:08:47.838Z INFO webdriver: BIDI COMMAND session.subscribe {"events":["browsingContext.contextCreated"]}
[0-0] 2025-09-03T16:08:47.838Z INFO webdriver: BIDI COMMAND session.subscribe {"events":["log.entryAdded","browsingContext.navigationStarted"]}
[0-0] 2025-09-03T16:08:47.838Z INFO webdriver: BIDI COMMAND script.addPreloadScript { functionDeclaration: <PreloadScript[1319 bytes]>, contexts: undefined }
[0-0] 2025-09-03T16:08:47.838Z INFO webdriver: BIDI COMMAND session.subscribe {"events":["browsingContext.navigationStarted","browsingContext.fragmentNavigated","network.responseCompleted","network.beforeRequestSent","network.fetchError"]}
[0-0] 2025-09-03T16:08:47.839Z INFO webdriver: BIDI COMMAND session.subscribe {"events":["browsingContext.userPromptOpened"]}
[0-0] 2025-09-03T16:08:47.839Z INFO webdriver: BIDI COMMAND session.subscribe {"events":["browsingContext.navigationStarted"]}
[0-0] 2025-09-03T16:08:47.839Z INFO webdriver: COMMAND getWindowHandle()
[0-0] 2025-09-03T16:08:47.839Z INFO webdriver: BIDI COMMAND browsingContext.getTree {}
[0-0] 2025-09-03T16:08:47.839Z INFO webdriver: [GET] https://privatedomain.co.uk/wd/hub/session/0c38ebd974e6e2e9366127b537d25837/window
[0-0] SYNCHRONOUS TERMINATION NOTICE: When explicitly exiting the process via process.exit or via a parent process, asynchronous tasks in your exitHooks will not run. Either remove these tasks, use gracefulExit() instead of process.exit(), or ensure your parent process sends a SIGINT to the process running this code.
[0-0] node:events:486
[0-0]       throw er; // Unhandled 'error' event
[0-0]       ^
[0-0] 
[0-0] Error: Unexpected server response: 404
[0-0]     at ClientRequest.<anonymous> (/Users/pokdeep/repos/webapp/automation/node_modules/ws/lib/websocket.js:913:7)
[0-0]     at ClientRequest.emit (node:events:508:28)
[0-0]     at ClientRequest.emit (node:domain:489:12)
[0-0]     at HTTPParser.parserOnIncomingClient (node:_http_client:772:27)
[0-0]     at HTTPParser.parserOnHeadersComplete (node:_http_common:117:17)
[0-0]     at TLSSocket.socketOnData (node:_http_client:614:22)
[0-0]     at TLSSocket.emit (node:events:508:28)
[0-0]     at TLSSocket.emit (node:domain:489:12)
[0-0]     at addChunk (node:internal/streams/readable:559:12)
[0-0]     at readableAddChunkPushByteMode (node:internal/streams/readable:510:3)
[0-0] Emitted 'error' event on WebSocket instance at:
[0-0]     at emitErrorAndClose (/Users/pokdeep/repos/webapp/automation/node_modules/ws/lib/websocket.js:1041:13)
[0-0]     at process.processTicksAndRejections (node:internal/process/task_queues:90:21)
[0-0] 
[0-0] Node.js v24.7.0
2025-09-03T16:08:47.863Z DEBUG @wdio/local-runner: Runner 0-0 finished with exit code 1
[0-0] FAILED in chrome - file:///features/otherLinks.feature
2025-09-03T16:08:47.863Z INFO @wdio/cli:launcher: Run onWorkerEnd hook

Spec Files:      0 passed, 1 failed, 1 total (100% completed) in 00:00:02  

2025-09-03T16:08:47.863Z INFO @wdio/local-runner: Shutting down spawned worker
2025-09-03T16:08:48.115Z INFO @wdio/local-runner: Waiting for 0 to shut down gracefully
2025-09-03T16:08:48.115Z INFO @wdio/local-runner: shutting down
2025-09-03T16:08:48.116Z INFO @wdio/cli:launcher: Run onComplete hook




This is the attempt that succeeded

pokdeep automation % npm run acceptance

> tests@0.0.1 acceptance
> TEST_ENV=$npm_config_testenv BRANCH=$npm_config_branch NODE_TLS_REJECT_UNAUTHORIZED=0 STRICT_SSL=false wdio run config/selenium-grid.conf.ts

Tests have been triggered using test tag @stub for  execution, the environment URL is https://webapp.privatedomain.co.uk using the Selenium Grid at privatedomain.co.uk

Execution of 1 workers started at 2025-09-03T16:12:48.801Z

2025-09-03T16:12:48.809Z INFO @wdio/cli:launcher: Run onPrepare hook
2025-09-03T16:12:48.811Z INFO @wdio/cli:launcher: Run onWorkerStart hook
2025-09-03T16:12:48.811Z INFO @wdio/xvfb: XvfbManager.init() called
2025-09-03T16:12:48.811Z INFO @wdio/xvfb: Xvfb not needed on current platform
2025-09-03T16:12:48.812Z INFO @wdio/local-runner: Start worker 0-0 with arg: run config/selenium-grid.conf.ts
2025-09-03T16:12:48.821Z INFO @wdio/xvfb:ProcessFactory: xvfb-run not found, falling back to regular fork
2025-09-03T16:12:48.821Z INFO @wdio/xvfb:ProcessFactory: ProcessFactory: shouldRun=false, isAvailable=false
2025-09-03T16:12:48.821Z INFO @wdio/xvfb:ProcessFactory: Creating worker process with regular fork
2025-09-03T16:12:48.822Z DEBUG @wdio/local-runner: Send command run to worker with cid "0-0"
[0-0] 2025-09-03T16:12:49.401Z INFO @wdio/local-runner: Run worker command: run
[0-0] Tests have been triggered using test tag @stub for  execution, the environment URL is https://webapp.privatedomain.co.uk using the Selenium Grid at privatedomain.co.uk
[0-0] 2025-09-03T16:12:49.413Z DEBUG @wdio/runner: init remote session
[0-0] 2025-09-03T16:12:49.934Z WARN @wdio/cucumber-framework: 'tagExpression' is deprecated. Use 'tags' instead.
[0-0] RUNNING in chrome - file:///features/otherLinks.feature
[0-0] 2025-09-03T16:12:49.951Z DEBUG @wdio/runner: init remote session
[0-0] 2025-09-03T16:12:49.952Z INFO webdriver: Initiate new session using the WebDriver protocol
[0-0] 2025-09-03T16:12:49.953Z INFO @wdio/utils: Connecting to existing driver at https://privatedomain.co.uk:443/wd/hub
[0-0] 2025-09-03T16:12:49.958Z INFO webdriver: [POST] https://privatedomain.co.uk/wd/hub/session
[0-0] (node:20339) Warning: Setting the NODE_TLS_REJECT_UNAUTHORIZED environment variable to '0' makes TLS connections and HTTPS requests insecure by disabling certificate verification.
[0-0] (Use `node --trace-warnings ...` to show where the warning was created)
[0-0] 2025-09-03T16:12:51.174Z INFO webdriver: Register BiDi handler for session with id e99b2835146d1b3090d7396180412f57
[0-0] 2025-09-03T16:12:51.174Z INFO webdriver: Connecting to webSocketUrl wss://privatedomain.co.uk/session/e99b2835146d1b3090d7396180412f57/se/bidi
[0-0] 2025-09-03T16:12:51.176Z DEBUG webdriver: Attempt to connect to webSocketUrl wss://privatedomain.co.uk/session/e99b2835146d1b3090d7396180412f57/se/bidi
[0-0] 2025-09-03T16:12:51.178Z DEBUG webdriver: Attempt to connect to webSocketUrl wss://10.166.101.206/session/e99b2835146d1b3090d7396180412f57/se/bidi
[0-0] 2025-09-03T16:12:51.178Z DEBUG webdriver: Attempt to connect to webSocketUrl wss://10.166.109.168/session/e99b2835146d1b3090d7396180412f57/se/bidi
[0-0] 2025-09-03T16:12:51.178Z DEBUG webdriver: Attempt to connect to webSocketUrl wss://10.166.106.101/session/e99b2835146d1b3090d7396180412f57/se/bidi
[0-0] 2025-09-03T16:12:51.269Z DEBUG webdriver: Could not connect to Bidi protocol at wss://10.166.106.101/session/e99b2835146d1b3090d7396180412f57/se/bidi: Unexpected server response: 404
[0-0] 2025-09-03T16:12:51.277Z DEBUG webdriver: Could not connect to Bidi protocol at wss://10.166.101.206/session/e99b2835146d1b3090d7396180412f57/se/bidi: Unexpected server response: 404
[0-0] 2025-09-03T16:12:51.294Z DEBUG webdriver: Could not connect to Bidi protocol at wss://10.166.109.168/session/e99b2835146d1b3090d7396180412f57/se/bidi: Unexpected server response: 404
[0-0] 2025-09-03T16:12:51.297Z INFO webdriver: Connected to Bidi protocol at wss://privatedomain.co.uk/session/e99b2835146d1b3090d7396180412f57/se/bidi
[0-0] 2025-09-03T16:12:51.298Z INFO webdriver: Connected to WebDriver Bidi interface at wss://privatedomain.co.uk/session/e99b2835146d1b3090d7396180412f57/se/bidi
[0-0] 2025-09-03T16:12:51.298Z INFO webdriver: BIDI COMMAND session.subscribe {"events":["browsingContext.contextCreated"]}
[0-0] 2025-09-03T16:12:51.299Z INFO webdriver: BIDI COMMAND session.subscribe {"events":["log.entryAdded","browsingContext.navigationStarted"]}
[0-0] 2025-09-03T16:12:51.299Z INFO webdriver: BIDI COMMAND script.addPreloadScript { functionDeclaration: <PreloadScript[1319 bytes]>, contexts: undefined }
[0-0] 2025-09-03T16:12:51.299Z INFO webdriver: BIDI COMMAND session.subscribe {"events":["browsingContext.navigationStarted","browsingContext.fragmentNavigated","network.responseCompleted","network.beforeRequestSent","network.fetchError"]}
[0-0] 2025-09-03T16:12:51.299Z INFO webdriver: BIDI COMMAND session.subscribe {"events":["browsingContext.userPromptOpened"]}
[0-0] 2025-09-03T16:12:51.299Z INFO webdriver: BIDI COMMAND session.subscribe {"events":["browsingContext.navigationStarted"]}
[0-0] 2025-09-03T16:12:51.300Z INFO webdriver: COMMAND getWindowHandle()
[0-0] 2025-09-03T16:12:51.300Z INFO webdriver: BIDI COMMAND browsingContext.getTree {}
[0-0] 2025-09-03T16:12:51.300Z INFO webdriver: [GET] https://privatedomain.co.uk/wd/hub/session/e99b2835146d1b3090d7396180412f57/window
[0-0] 2025-09-03T16:12:51.332Z INFO webdriverio:PolyfillManager: Adding polyfill script to context with id 192271C9341A88CCD806E6AE87C9F2E7
[0-0] 2025-09-03T16:12:51.332Z INFO webdriver: BIDI COMMAND script.callFunction {"functionDeclaration":"<Function[443 bytes] webdriverioPolyfill>","target":{"children":null,"clientWindow":"1836966127","context":"192271C9341A88CCD806E6AE87C9F2E7","originalOpener":null,"parent":null,"url":"about:blank","userContext":"default"},"awaitPromise":false}
[0-0] 2025-09-03T16:12:51.332Z INFO webdriver: BIDI COMMAND script.addPreloadScript { functionDeclaration: <PreloadScript[443 bytes]>, contexts: ["192271C9341A88CCD806E6AE87C9F2E7"] }
[0-0] 2025-09-03T16:12:51.333Z INFO webdriver: BIDI RESULT {"id":1,"result":{"subscription":"4c43de74-34bb-4f56-b5ee-1b9b1863b6e9"},"type":"success"}
[0-0] 2025-09-03T16:12:51.333Z INFO webdriver: BIDI RESULT {"id":2,"result":{"subscription":"95db704f-7424-4522-8178-9a62b84f0208"},"type":"success"}
[0-0] 2025-09-03T16:12:51.336Z INFO webdriver: BIDI RESULT {"id":4,"result":{"subscription":"03427563-5acd-4eaf-8ac0-d9e98e69dfd1"},"type":"success"}
[0-0] 2025-09-03T16:12:51.336Z INFO webdriver: BIDI RESULT {"id":5,"result":{"subscription":"cc003fff-b19a-42e0-a18e-92263bacb11f"},"type":"success"}
[0-0] 2025-09-03T16:12:51.336Z INFO webdriver: BIDI RESULT {"id":6,"result":{"subscription":"2d16496f-b783-4faa-b2ef-508309edbb2a"},"type":"success"}
[0-0] 2025-09-03T16:12:51.337Z INFO webdriver: BIDI RESULT {"id":7,"result":{"contexts":[{"children":[],"clientWindow":"1836966127","context":"192271C9341A88CCD806E6AE87C9F2E7","originalOpener":null,"parent":null,"url":"about:blank","userContext":"default"}]},"type":"success"}
[0-0] 2025-09-03T16:12:51.337Z INFO webdriver: BIDI RESULT {"id":3,"result":{"script":"964f9ad9-ddab-42b7-8958-e5bae9d52486"},"type":"success"}
[0-0] 2025-09-03T16:12:51.362Z INFO webdriver: BIDI RESULT {"id":8,"result":{"realm":"-2557371104352114333.7266163095988573889","result":{"type":"undefined"},"type":"success"},"type":"success"}
[0-0] 2025-09-03T16:12:51.363Z INFO webdriver: BIDI RESULT {"id":9,"result":{"script":"718c918b-e04a-4a31-867c-7b914c66b931"},"type":"success"}
[0-0] 2025-09-03T16:12:51.412Z INFO webdriver: RESULT 192271C9341A88CCD806E6AE87C9F2E7
[0-0] 2025-09-03T16:12:52.022Z DEBUG @wdio/utils:shim: Finished to run "beforeStep" hook in 1ms
[0-0] 2025-09-03T16:12:52.023Z INFO webdriver: BIDI COMMAND session.subscribe {"events":["network.beforeRequestSent","network.responseStarted"]}
[0-0] 2025-09-03T16:12:52.057Z INFO webdriver: BIDI RESULT {"id":10,"result":{"subscription":"fe134df5-6016-44e9-853c-05987b139197"},"type":"success"}
[0-0] 2025-09-03T16:12:52.057Z INFO WebDriverInterception: subscribed to network events
[0-0] 2025-09-03T16:12:52.057Z INFO webdriver: BIDI COMMAND network.addIntercept {"phases":["beforeRequestSent","responseStarted"],"urlPatterns":[{"type":"pattern","protocol":"https","hostname":"webapp.privatedomain.co.uk","pathname":"/api/features","port":"443"}]}
[0-0] 2025-09-03T16:12:52.090Z INFO webdriver: BIDI RESULT {"id":11,"result":{"intercept":"f2d0fd8f-8387-46ff-b883-bed28382f2c2"},"type":"success"}
[0-0] 2025-09-03T16:12:52.091Z INFO webdriver: BIDI COMMAND browsingContext.navigate {"context":"192271C9341A88CCD806E6AE87C9F2E7","url":"https://webapp.privatedomain.co.uk/","wait":"complete"}

Code of Conduct

  • I agree to follow this project's Code of Conduct

Is there an existing issue for this?

  • I have searched the existing issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions