A session is either terminated or not started

Hello,
I have two tests, one after one and I run with two devices parallel with the TestNG,
Between the two tests I get the error as attached,

    2022-05-26 10:13:57:481 [W3C (0a205b0a)] Encountered internal error running command: NoSuchDriverError: A session is either terminated or not started
2022-05-26 10:13:57:481 [W3C (0a205b0a)]     at asyncHandler (/usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/protocol/protocol.js:243:15)
2022-05-26 10:13:57:481 [W3C (0a205b0a)]     at /usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/protocol/protocol.js:423:15
2022-05-26 10:13:57:481 [W3C (0a205b0a)]     at Layer.handle [as handle_request] (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/layer.js:95:5)
2022-05-26 10:13:57:481 [W3C (0a205b0a)]     at next (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/route.js:137:13)
2022-05-26 10:13:57:481 [W3C (0a205b0a)]     at Route.dispatch (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/route.js:112:3)
2022-05-26 10:13:57:481 [W3C (0a205b0a)]     at Layer.handle [as handle_request] (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/layer.js:95:5)
2022-05-26 10:13:57:481 [W3C (0a205b0a)]     at /usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:281:22
2022-05-26 10:13:57:481 [W3C (0a205b0a)]     at param (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:354:14)
2022-05-26 10:13:57:481 [W3C (0a205b0a)]     at param (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:365:14)
2022-05-26 10:13:57:481 [W3C (0a205b0a)]     at Function.process_params (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:410:3)
2022-05-26 10:13:57:482 [W3C (0a205b0a)]     at next (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:275:10)
2022-05-26 10:13:57:482 [W3C (0a205b0a)]     at logger (/usr/local/lib/node_modules/appium/node_modules/morgan/index.js:144:5)
2022-05-26 10:13:57:482 [W3C (0a205b0a)]     at Layer.handle [as handle_request] (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/layer.js:95:5)
2022-05-26 10:13:57:482 [W3C (0a205b0a)]     at trim_prefix (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:317:13)
2022-05-26 10:13:57:482 [W3C (0a205b0a)]     at /usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:284:7
2022-05-26 10:13:57:482 [W3C (0a205b0a)]     at Function.process_params (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:335:12)
2022-05-26 10:13:57:482 [HTTP] <-- POST /wd/hub/session/0a205b0a-7d1c-4057-8a54-152020cbc93d/element 404 9 ms - 1886
2022-05-26 10:13:57:482 [HTTP] 
2022-05-26 10:13:57:484 [HTTP] --> GET /wd/hub/session/0a205b0a-7d1c-4057-8a54-152020cbc93d/window/rect
2022-05-26 10:13:57:484 [HTTP] {}
2022-05-26 10:13:57:485 [W3C (0a205b0a)] Encountered internal error running command: NoSuchDriverError: A session is either terminated or not started
2022-05-26 10:13:57:485 [W3C (0a205b0a)]     at asyncHandler (/usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/protocol/protocol.js:243:15)
2022-05-26 10:13:57:485 [W3C (0a205b0a)]     at /usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/protocol/protocol.js:423:15
2022-05-26 10:13:57:485 [W3C (0a205b0a)]     at Layer.handle [as handle_request] (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/layer.js:95:5)
2022-05-26 10:13:57:485 [W3C (0a205b0a)]     at next (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/route.js:137:13)
2022-05-26 10:13:57:485 [W3C (0a205b0a)]     at Route.dispatch (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/route.js:112:3)
2022-05-26 10:13:57:485 [W3C (0a205b0a)]     at Layer.handle [as handle_request] (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/layer.js:95:5)
2022-05-26 10:13:57:485 [W3C (0a205b0a)]     at /usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:281:22
2022-05-26 10:13:57:485 [W3C (0a205b0a)]     at param (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:354:14)
2022-05-26 10:13:57:485 [W3C (0a205b0a)]     at param (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:365:14)
2022-05-26 10:13:57:485 [W3C (0a205b0a)]     at Function.process_params (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:410:3)
2022-05-26 10:13:57:485 [W3C (0a205b0a)]     at next (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:275:10)
2022-05-26 10:13:57:485 [W3C (0a205b0a)]     at logger (/usr/local/lib/node_modules/appium/node_modules/morgan/index.js:144:5)
2022-05-26 10:13:57:486 [W3C (0a205b0a)]     at Layer.handle [as handle_request] (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/layer.js:95:5)
2022-05-26 10:13:57:486 [W3C (0a205b0a)]     at trim_prefix (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:317:13)
2022-05-26 10:13:57:486 [W3C (0a205b0a)]     at /usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:284:7
2022-05-26 10:13:57:486 [W3C (0a205b0a)]     at Function.process_params (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:335:12)
2022-05-26 10:13:57:487 [HTTP] <-- GET /wd/hub/session/0a205b0a-7d1c-4057-8a54-152020cbc93d/window/rect 404 2 ms - 1886
2022-05-26 10:13:57:487 [HTTP] 
2022-05-26 10:13:57:497 [HTTP] --> GET /wd/hub/session/0a205b0a-7d1c-4057-8a54-152020cbc93d/screenshot
2022-05-26 10:13:57:497 [HTTP] {}
2022-05-26 10:13:57:497 [W3C (0a205b0a)] Encountered internal error running command: NoSuchDriverError: A session is either terminated or not started
2022-05-26 10:13:57:497 [W3C (0a205b0a)]     at asyncHandler (/usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/protocol/protocol.js:243:15)
2022-05-26 10:13:57:497 [W3C (0a205b0a)]     at /usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/protocol/protocol.js:423:15
2022-05-26 10:13:57:497 [W3C (0a205b0a)]     at Layer.handle [as handle_request] (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/layer.js:95:5)
2022-05-26 10:13:57:497 [W3C (0a205b0a)]     at next (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/route.js:137:13)
2022-05-26 10:13:57:497 [W3C (0a205b0a)]     at Route.dispatch (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/route.js:112:3)
2022-05-26 10:13:57:498 [W3C (0a205b0a)]     at Layer.handle [as handle_request] (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/layer.js:95:5)
2022-05-26 10:13:57:498 [W3C (0a205b0a)]     at /usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:281:22
2022-05-26 10:13:57:498 [W3C (0a205b0a)]     at param (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:354:14)
2022-05-26 10:13:57:498 [W3C (0a205b0a)]     at param (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:365:14)
2022-05-26 10:13:57:498 [W3C (0a205b0a)]     at Function.process_params (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:410:3)
2022-05-26 10:13:57:498 [W3C (0a205b0a)]     at next (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:275:10)
2022-05-26 10:13:57:498 [W3C (0a205b0a)]     at logger (/usr/local/lib/node_modules/appium/node_modules/morgan/index.js:144:5)
2022-05-26 10:13:57:498 [W3C (0a205b0a)]     at Layer.handle [as handle_request] (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/layer.js:95:5)
2022-05-26 10:13:57:498 [W3C (0a205b0a)]     at trim_prefix (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:317:13)
2022-05-26 10:13:57:498 [W3C (0a205b0a)]     at /usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:284:7
2022-05-26 10:13:57:498 [W3C (0a205b0a)]     at Function.process_params (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:335:12)
2022-05-26 10:13:57:498 [HTTP] <-- GET /wd/hub/session/0a205b0a-7d1c-4057-8a54-152020cbc93d/screenshot 404 1 ms - 1886
2022-05-26 10:13:57:498 [HTTP] 
2022-05-26 10:13:57:501 [HTTP] --> GET /wd/hub/session/0a205b0a-7d1c-4057-8a54-152020cbc93d/screenshot
2022-05-26 10:13:57:501 [HTTP] {}
2022-05-26 10:13:57:502 [W3C (0a205b0a)] Encountered internal error running command: NoSuchDriverError: A session is either terminated or not started
2022-05-26 10:13:57:502 [W3C (0a205b0a)]     at asyncHandler (/usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/protocol/protocol.js:243:15)
2022-05-26 10:13:57:502 [W3C (0a205b0a)]     at /usr/local/lib/node_modules/appium/node_modules/appium-base-driver/lib/protocol/protocol.js:423:15
2022-05-26 10:13:57:502 [W3C (0a205b0a)]     at Layer.handle [as handle_request] (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/layer.js:95:5)
2022-05-26 10:13:57:502 [W3C (0a205b0a)]     at next (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/route.js:137:13)
2022-05-26 10:13:57:502 [W3C (0a205b0a)]     at Route.dispatch (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/route.js:112:3)
2022-05-26 10:13:57:502 [W3C (0a205b0a)]     at Layer.handle [as handle_request] (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/layer.js:95:5)
2022-05-26 10:13:57:502 [W3C (0a205b0a)]     at /usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:281:22
2022-05-26 10:13:57:502 [W3C (0a205b0a)]     at param (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:354:14)
2022-05-26 10:13:57:502 [W3C (0a205b0a)]     at param (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:365:14)
2022-05-26 10:13:57:502 [W3C (0a205b0a)]     at Function.process_params (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:410:3)
2022-05-26 10:13:57:502 [W3C (0a205b0a)]     at next (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:275:10)
2022-05-26 10:13:57:502 [W3C (0a205b0a)]     at logger (/usr/local/lib/node_modules/appium/node_modules/morgan/index.js:144:5)
2022-05-26 10:13:57:502 [W3C (0a205b0a)]     at Layer.handle [as handle_request] (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/layer.js:95:5)
2022-05-26 10:13:57:502 [W3C (0a205b0a)]     at trim_prefix (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:317:13)
2022-05-26 10:13:57:502 [W3C (0a205b0a)]     at /usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:284:7
2022-05-26 10:13:57:503 [W3C (0a205b0a)]     at Function.process_params (/usr/local/lib/node_modules/appium/node_modules/express/lib/router/index.js:335:12)
2022-05-26 10:13:57:503 [HTTP] <-- GET /wd/hub/session/0a205b0a-7d1c-4057-8a54-152020cbc93d/screenshot 404 2 ms - 1886
2022-05-26 10:13:57:503 [HTTP] 

What cause that?
Thank you

It’s a timing issue. The first Appium server has not finished exiting.

Solutions:

  1. Run the Server on a different port: https://stackoverflow.com/questions/36623334/change-device-port-appium
  2. Get the previous Appium process id and kill it before you begin the new session.
  3. Don’t run them back to back. Add a wait in there of several minutes before running second server. May need to experiment with how long to wait.

If you intend to add more runs like these, you should check out Selenium Grid: https://appiumpro.com/editions/54-using-appium-with-selenium-grid

1 Like

Thank you @wreed,
The situation is that I run parallel from the testNG , and yes as I saw, each thread is run on the same Appium port but as you wrote it looks like that one Appium server is finish the test and closed and the other server still looking for the elements and we have race condition.
The solutions that you offer is good (I just run on MAC)
so the first solution is good and just need to add ports for each test (each device)
Thte second one will not work for me because I want to run in parallel and programatically so no option to close it before and to start the other session, about the thred option I tried to add wait after the test is finish but I don’t know exactly which device is finish before the other one 100%…so again no solution, I think that the best one is just to add ports,

The selenium Grid is looks also like good solution for that, it’s actually add ports isn’t it?
Thank you

@wreed, Small update,
So I did tried with the grid and I didn’t saw that that is other instance of the server for each device.
So the question now, if I will mark the appiumURL=http://127.0.0.1:4723/wd/hub and in each device I will mark other port, is it will be new instance of the server for each device?
Thank you

I believe, that it’s pretty much the same mechanics as selenium. Not many people go parallel, it’s often cheaper to buy or re-use other machines, since the pain can often be greater. Basically you need to understand and track ports in use, and dynamically deal with it when a server fails to give up a port and then automatically hop to a new free port. Having good logging separation and being clever about things will help. Moreover, you will want to be sure to limit or throttle the number of instances on one machine so as to prevent slowdown side effects on time-sensitive test cases as usual.

You would have to read the documentation on Selenium Grid for this. I actually do not use it.

OK, than now I will try to play with the ports and see if that will help in such case