Repository: microsoft/botframework-sdk

Issue #2716: [Session Issue] session.userData.lastAccessTime seems not able to searlize inside the setTimeout function
Created: 2017-05-07T13:16:38Z
I've the following code proactively check if any user input has been idle  for certain time, then close the bot session but find that the  access time storing in the session.userData can't be updated inside setTimeout function.  Here's is the code
bot.use({
    botbuilder: function (session, next) { 
        var date = new Date()
        session.userData.lastAccess = date.valueOf()
        console.log("Current acess time: ", session.userData.lastAccess)
        setTimeout(function () {
            console.log("Last user access time: ", session.userData.lastAccess)
            var idleTime = Date.now() - lastAccessTime
            var shouldExpire = (session) => idleTime > 30000
             if (shouldExpire(session)) {
                    session.endConversation('Session idle over 30 sec. Session end....' + idleTime)
                    session.clearDialogStack()
                    session.reset()
                    session.userData.firstRun = false
              } else {
                    console.log("idleTime less than max of 30 sec, keep the session")
               } 
        }, 30000) 

Expected Behavior
If I've 2 user inputs, I should have got the session.userData.lastAccess overwritten by the 2nd last access time which is 1494162055469
Current access time:  1494162049089
Current access time:  1494162055469
But when the 2 setTimeout functions time up and checking the session.userData.lastAccess time, I got the following instead?
Last user access time:  1494162049089
Last user access time:  1494162055469
What's wrong with the session.userData inside the setTimeout function? I expect both should last access time should be 1494162055469, not an individual copy.   Can anyone point me out. please

Duplicate issue #2733: Reopen issue #2716 [Session Issue] session.userData.lastAccessTime seems not able to searlize inside the setTimeout function
Created: 2017-05-09T16:51:30Z
I've the following code proactively check if any user input has been idle for certain time, then close the bot session but find that the access time storing in the session.userData can't be updated inside setTimeout function.  fI using sessionState.lastAccess will return the same timestamp in the 1st and 2nd call.  And when the setTiemOut timeup, it has 2 copies of the of the lastAccess tiemstamp like what I have in the userData
I believe it's the the problem of the session not able to searailize? or referencing issue!.  But if  I replace the session to  a global variable, "lastAccessTime",  I can solve it but I need its per user session not as a global state.  Please help how to get the session state update inside the setTimeout function.  Please advise.
You can try this simple code and see it yourself
var lastAccessTime = null
bot.use({
    botbuilder: function (session, next) {
        session.userData.lastAccess = Date.now()
        lastAccessTime = session.userData.lastAccess
        console.log("userData.lastAccess: ", session.userData.lastAccess, " sessionState.lastAccess: ", session.sessionState.lastAccess, "session.lastSendTime", session.lastSendTime, "lastAccessTime: ", lastAccessTime)
        setTimeout(function () {
            console.log("userData.lastAccess: ", session.userData.lastAccess, " sessionState.lastAccess: ", session.sessionState.lastAccess, "session.lastSendTime", session.lastSendTime)
        }, 15000) // 15 sec


Result:
userData.lastAccess:  1494292505983  sessionState.lastAccess:  1494292505977 session.lastSendTime 1494292505977 lastAccessTime:  1494292505983
userData.lastAccess:  1494292519934  sessionState.lastAccess:  1494292505977 session.lastSendTime 1494292519933 lastAccessTime:  1494292519934
setTimeOut timeup:
userData.lastAccess:  1494292505983  sessionState.lastAccess:  1494292505977 session.lastSendTime 1494292505977 lastAccessTime:  1494292519934
userData.lastAccess:  1494292519934  sessionState.lastAccess:  1494292519933 session.lastSendTime 1494292519933 lastAccessTime:  1494292519934

Closing comment (2017-05-09T17:36:03Z): duplicate of #2716