From 65a2f11a94beeeb193c6b46d284cfeaf28eba54b Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Mon, 10 Jul 2017 17:12:03 +1000 Subject: [PATCH] bugfix, in startjob, we cannot modify sessionmanager's data, as it has race condition. --- sessionManager.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/sessionManager.go b/sessionManager.go index 2d2428e..75445ee 100644 --- a/sessionManager.go +++ b/sessionManager.go @@ -14,8 +14,9 @@ type openIDSession struct { } type workDone struct { - openID string //which user - consumed int //job done + openID string //which user + consumed int //job done + data openIDSessionData //modified, updated sessiondata } //SessionManager manage all sessions @@ -89,10 +90,9 @@ func (m *SessionManager) createSession(openID string) openIDSession { func (m *SessionManager) clearJobDone(d workDone) { s, found := m.sessions[d.openID] log.Println("SessionMgr/clearJobDone: start clearning jobs ... ") - log.Println(s) - log.Println(d) if found { s.count -= d.consumed + s.data = d.data //updated session data m.sessions[d.openID] = s //update if s.count == 0 { //no job to do //remove from memory @@ -126,23 +126,23 @@ func (m *SessionManager) destroySession(openID string) { //worker thread, cannot change session info func (m *SessionManager) startJob(openID string) { log.Println("SessionMgr/startJob: enter...") - jobFinished := workDone{openID, 0} + s := m.sessions[openID] + jobFinished := workDone{openID, 0, openIDSessionData{}} //process all jobs in the channel hasJob := true for hasJob { select { case v := <-m.sessions[openID].jobs: m.checkOpenID(openID, v) - s := m.sessions[openID] s.data.incomingMsg(v) //<=== main logic for processing each incoming message - log.Printf("after incoming message %s", s.data.Procedure) - m.sessions[openID] = s + log.Printf("procedure after incoming message %s", s.data.Procedure) jobFinished.consumed++ default: hasJob = false } } - m.done <- jobFinished //notify parent that we have done + jobFinished.data = s.data //update session data + m.done <- jobFinished //notify parent that we have done return }