# Invalid session workaround

**URL:** https://forum.genieacs.com/t/invalid-session-workaround/2660
**Category:** Uncategorized
**Created:** [May 26, 2022, 4:53pm UTC](https://forum.genieacs.com/t/invalid-session-workaround/2660 "2022-05-26T16:53:14Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Andre\_Driemeyer](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Andre\_Driemeyer](https://forum.genieacs.com/u/Andre_Driemeyer)
#### Post date: [May 26, 2022, 4:53pm UTC](https://forum.genieacs.com/t/invalid-session-workaround/2660/1 "2022-05-26T16:53:14Z")

</div>

In my setup I was noticing a strange communication problem in my GenieACS setup. Sometimes, randomly during an inform a GenieACS would raise an “Invalid Session” error.

```auto
2022-05-25T16:24:55.095Z [ERROR] ::ffff:1.2.3.4: Invalid session

```

Digging in the source code I was able to determine this error was happening because GenieACS were unable to recover a session from mongo’s cache. To be more precise, this “cache.pop” function call was returning null when it should be returning the active session.

![image](https://canada1.discourse-cdn.com/flex035/uploads/genieacs/original/1X/b30d6d497c534f66ee1f7a93c0de22ff14b61533.png)

When I manually looked at the database, I was surprised that there was an entry with the correspondent session\_id there, and that function should not be returning null…

This should mean there was a race condition where cache.pop was being called before cache.set could put back the session in the database. Looking at the source again, I found that cache.set is called (for sessions) only when the socket closes, so this means the CPE was initiating a new connection before the previous one closed (on the server side, at least).

To mitigate this problem I edited cache.pop function to add a “second try” query in the cache in case the socket hasn’t finished closing.

![image](https://canada1.discourse-cdn.com/flex035/uploads/genieacs/original/1X/5f092ca25aee4b476bca5f01e5a1b56939d843ba.png)

This seems to have worked, now I have absolutely zero invalid session errors in my setup.

---

<div class="post-metadata">

### Author: ![dcabanaw](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/dcabanaw/32/921_2.png) [@dcabanaw](https://forum.genieacs.com/u/dcabanaw)
#### Post date: [August 16, 2022, 6:42pm UTC](https://forum.genieacs.com/t/invalid-session-workaround/2660/2 "2022-08-16T18:42:21Z")

</div>

I was having a similar issue and tried this. Seems to work, but seems kinda hacky. Wonder if there would be a better way to handle this?

---

<div class="post-metadata">

### Author: ![Andre\_Driemeyer](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Andre\_Driemeyer](https://forum.genieacs.com/u/Andre_Driemeyer)
#### Post date: [September 6, 2022, 8:27pm UTC](https://forum.genieacs.com/t/invalid-session-workaround/2660/3 "2022-09-06T20:27:07Z")

</div>

I have not found this yet. I added this change in my fork of genieacs on github so I don’t lose this modifications when I compile my docker image. Something I noticed later is that the devs implemented this timeout before each cache.pop call, suggesting they must have faced a similar problem in some capacity.

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/genieacs/original/1X/89c987bacc0ab49278060fe91048c6e1e8a303bd.png)
