# Session Lifecycle

**URL:** <https://openvidu.discourse.group/t/session-lifecycle/103>\
**Category:** Issues developing apps\
**Tags:** v2\
**Created:** [January 29, 2020, 8:08pm UTC](https://openvidu.discourse.group/t/session-lifecycle/103 "2020-01-29T20:08:27Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![melvingilberto](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/melvingilberto/32/25_2.png) [@melvingilberto](https://openvidu.discourse.group/u/melvingilberto)\
**Post date:** [January 29, 2020, 8:08pm UTC](https://openvidu.discourse.group/t/session-lifecycle/103/1 "2020-01-29T20:08:27Z")

</div>

What is the life cycle of a session? Openvidu deletes sessions if there is no transmission?  
Openvidu eliminates transmissions at some point?

---

<div class="post-metadata">

**Author:** ![pabloFuente](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/pablofuente/32/2536_2.png) [@pabloFuente](https://openvidu.discourse.group/u/pabloFuente)\
**Post date:** [January 30, 2020, 11:02am UTC](https://openvidu.discourse.group/t/session-lifecycle/103/2 "2020-01-30T11:02:05Z")

</div>

Hi,

OpenVidu automatically closes a session after last participant connected to it leaves the session.  
So: you create a session using API REST (POST /api/sessions). That session is ready to produce tokens (POST /api/tokens), that then can be used by users to connect to it. After first user connects to it with a token, we can say that the session is “active”. More users can connect to it, recordings can be started…

Consulting sessions with API REST (GET /api/sessions) will return all created sessions (sessions with no connected users yet, and “active” sessions with connected users).

After last of the users leaves (that is, the number of users connected goes from \>=1 to 0), the session is automatically closed. Except if it is currently being recorded. If so, a countdown of 2 minutes starts, and if it ellapses without new users connecting to it, the recording is stopped and the session if finally closed (the countdown time can be configrued with a global parameter. You can learn more about this [here](https://docs.openvidu.io/en/stable/advanced-features/recording/#automatic-stop-of-recordings).

If this post is related to [this one](https://openvidu.discourse.group/t/api-api-sessions-created-sessions-or-active-sessions/102/2), OpenVidu 2.11.0 and below has a known bug that may cause some sessions to enter a “ghost” state in which the are not eliminated once last user disconnects. This has been addressed for release 2.12.0 (coming very soon) and luckly it will be solved.

That being said, if you have any information on how to actively reproduce the error in 2.11.0, it would be very useful to check that it doesn’t happen anymore in the current dev branch.

Best regards!

---

<div class="post-metadata">

**Author:** ![melvingilberto](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/melvingilberto/32/25_2.png) [@melvingilberto](https://openvidu.discourse.group/u/melvingilberto)\
**Post date:** [January 30, 2020, 3:08pm UTC](https://openvidu.discourse.group/t/session-lifecycle/103/3 "2020-01-30T15:08:51Z")

</div>

Perfect, thank you very much, that helped me a lot

---

<div class="post-metadata">

**Author:** ![rohit.roy](https://avatars.discourse-cdn.com/v4/letter/r/eada6e/32.png) [@rohit.roy](https://openvidu.discourse.group/u/rohit.roy)\
**Post date:** [September 6, 2020, 5:34am UTC](https://openvidu.discourse.group/t/session-lifecycle/103/4 "2020-09-06T05:34:21Z")

</div>

@pabloFuente, I faced the situation where Session is not getting closed automatically however I face this with some of the sessions only.

There was 1 Publisher and approx 50 subscribers in a session and recording was ON in Individual Mode. Once Teacher ended the session, Teacher and all subscribers got disconnected expect 1 subscriber.  
When i checked it from inspector panel i found it was showing _**1 connection (Without any Role i.e. Publisher, Subscribers e.t.c)**_ . I have set Recording auto stop time to 10 Minutes.  
I waited for approx 50 minutes but neither Recording nor the Session stopped. I had to disconnect this users manually.  
**Please explain when and how this session will get destroyed?** Because if session does not get destroy automatically then Recording won’t be generated in these case.

---

<div class="post-metadata">

**Author:** ![mihailj](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/mihailj/32/406_2.png) [@mihailj](https://openvidu.discourse.group/u/mihailj)\
**Post date:** [September 7, 2020, 10:08am UTC](https://openvidu.discourse.group/t/session-lifecycle/103/5 "2020-09-07T10:08:00Z")

</div>

Hello,

You can force the closing of the session when the Teacher ends the session using this REST API method: [https://docs.openvidu.io/en/2.15.0/reference-docs/REST-API/#delete-apisessionsltsession\_idgt](https://docs.openvidu.io/en/2.15.0/reference-docs/REST-API/#delete-apisessionsltsession_idgt)

Cheers!

---

<div class="post-metadata">

**Author:** ![rohit.roy](https://avatars.discourse-cdn.com/v4/letter/r/eada6e/32.png) [@rohit.roy](https://openvidu.discourse.group/u/rohit.roy)\
**Post date:** [September 9, 2020, 4:36am UTC](https://openvidu.discourse.group/t/session-lifecycle/103/6 "2020-09-09T04:36:38Z")

</div>

@mihailj Yes, Fore closing option is there but i faced the situation where Delete Session API sometimes return 500 Internal server error and does not close the session. This is one of the Random problem which i’ve faced so far with Openvidu, any idea about the issue which i have raised here

> [@Getting 500 error code while deleting Session from Inspector](https://openvidu.discourse.group/t/getting-500-error-code-while-deleting-session-from-inspector/1980):
>
> I faced some strange issue on OpenVidu Pro, Neither Session nor Recording closed. Recording generated with 0 byte before the actual session close and in the same time session was going on and Recording Status was ON in the Inspector panel, to reconfirm whether recording file is being generated on Media node or not, i checked on Media node and found Recording file was there for this ongoing session. This session was for 2 hours, after 2 hours when Teacher ended the session. Session didn’t close…

---

<div class="post-metadata">

**Author:** ![bettyboop](https://yyz2.discourse-cdn.com/free1/user_avatar/openvidu.discourse.group/bettyboop/32/964_2.png) [@bettyboop](https://openvidu.discourse.group/u/bettyboop)\
**Post date:** [September 12, 2020, 5:54pm UTC](https://openvidu.discourse.group/t/session-lifecycle/103/7 "2020-09-12T17:54:09Z")

</div>

It seems like if a session is created and no tokens are requested, it will stay open indefinitely.

However, once a single token has been added, if nothing happens, the token will time out which looks like the participant has left–triggering the session to be closed.

Is that correct? It feels like this could be happening in my testing.
