SessionNotCreatedException: New session request timeout error executing behave from pipeline
Asked by Daymi Morales Vega:

Hi, community!

I'm new in bitbucket pipelines and also in behave, so any help I really appreciated.

I'm running behave BDD in bitbucket pipelines to execute 2 different features and is failing particularly the second one, in before_scenario method seems like it cannot create the driver or cannot open another session for the second time.

Is there any special code that I should include in the python script to run remotely when you want to execute more than one feature?

When I run only 1 feature the same pipeline script works perfectly.

This is the error:  
HOOK-ERROR in before_scenario: SessionNotCreatedException: Message: Could not start a new session. New session request timed out  
My pipeline looks like this:  
> pipelines:  
> default:  
> - step:  
> name: TestBehave  
> image: python:3.7.3  
> script:  
> - apt-get install unzip  
> - pip install flask selenium requests behave  
> - pip install webdriver-manager  
> - behave -D env=test  
> services:  
> - selenium-chrome  
> definitions:  
> services:  
> selenium-chrome:  
> image: selenium/standalone-chrome  
> environment:  
> HUB_PORT_4444_TCP_ADDR: localhost  
> HUB_PORT_4444_TCP_PORT: "4444"  
> ports:  
- "4444:4444"  
Thanks in advance!


Answers:
author: Patrik S
body: Hello [@Daymi Morales Vega](/t5/user/viewprofilepage/user-id/4981247) ,

Welcome to Atlassian Community!

The syntax you have used to define the service in your pipelines is slightly incorrect, as we currently don't support custom port mapping in service, we just currently support passing environment variables. The supported syntax and examples of defininig services in bitbucket pipelines can be checked in [this documentation](https://support.atlassian.com/bitbucket-cloud/docs/databases-and-service-containers/).

Having said that, I'm not very familiar with selenium itself, but from my research it seems that selenium by default allows only 1 concurrent session, and if you have more than one request, this request will be queued and timeout after a given amount of time if no slot becomes available.

Accordingly to [selenium documentation](https://github.com/SeleniumHQ/docker-selenium#increasing-session-concurrency-per-container), you can increase the session concurrency in the container, overriding the maximum limit, by setting both *SE_NODE_MAX_SESSIONS* to a desired number and *SE_NODE_OVERRIDE_MAX_SESSIONS* to **true.**

Alternatively, you also have the option to [increase the time selenium waits before timing out](https://github.com/SeleniumHQ/docker-selenium#session-request-timeout) when waiting for a slot. By default, a request will stay in the queue up to 300 seconds before a timeout is reached and an attempt to process the request is done every 5 seconds (by default). To change that you need to set the variables *SE_SESSION_REQUEST_TIMEOUT* and *SE_SESSION_RETRY_INTERVAL*

Please find below an example of bitbucket-pipelines.yml defining the selenium service to allow 2 concurrent sessions :

```
pipelines:

  default:

      - step:

          name: TestBehave     

          script:                

            - sleep 15 # sleeps 15 seconds for the selenium service to start
            - echo "This is a test to change selenium concurrency sessions"

          services:

            - selenium-chrome

definitions:

  services:

    selenium-chrome:

      image: selenium/standalone-chrome  

      variables:

        SE_NODE_OVERRIDE_MAX_SESSIONS: 'true'

        SE_NODE_MAX_SESSIONS: '2'
```

This should make the selenium service to have 2 concurrent sessions available.

You can adjust the example above with your use case, and you also have the option to include the variables to change the session request timeout.

Hope that helps! Let me know in case you have any questions.

Thank you, [@Daymi Morales Vega](/t5/user/viewprofilepage/user-id/4981247) .

Kind regards,

Patrik S

comments: author: Daymi Morales Vega
body: Hi [@Patrik S](/t5/user/viewprofilepage/user-id/4603309)

Thanks for answering. It is a great explanation for the problem and you go deeper in the answer with an example. I also noticed that the port is not supported and I removed it from de pipeline, but thanks for highlighting that.

I will accept your answer because is well elaborated and It makes sense to fix the Session time-out issue by changing the Selenium setup.

However, in my case, I had already solved it in another way. I finally found a solution by slightly changing the behave implementation. I read deeply in the behavior [environmental control](https://behave.readthedocs.io/en/stable/tutorial.html#environmental-controls) documentation and I notice that there are methods that run only once per environment, and I put the driver creation there. With this change, Selenium does not need to create more than one session because all the tests are running in the same driver/browser. Basically, I had the driver creation wrongly in the before_scenario method instead of in before_all method.

Thank you again, I'm sure this will help others if they run into the same error.

Kind regards,

Daymi M.


author: Patrik S
body: Hey [@Daymi Morales Vega](/t5/user/viewprofilepage/user-id/4981247) ,

You're very welcome!

And thank you very much for sharing the solution that worked for you Happy to know that your selenium setup is working now :)

Kind regards,

Patrik S.


author: Victoriano Eco
body: [@Daymi Morales Vega](/t5/user/viewprofilepage/user-id/4981247) can you please post how you solved the issue and what you've adjusted in your pipeline? I'm also having the same issue. Thanks


author: elena_skorodilo
body: [@Patrik S](/t5/user/viewprofilepage/user-id/4603309) your solution worked perfectly for me! I did manage to increase max sessions and all 5 tests PASSED on a bitbucket pipeline! While before it was always 1 passed and others failed to launch! Thanks a lot!


Tags: bitbucket-cloud

bitbucket-pipelines

cloud