From 1e8f2bb52aca47385c33bddeab293f7a6c22a462 Mon Sep 17 00:00:00 2001
From: Volodymyr Yurchenko <volodymyr.yurchenko@cern.ch>
Date: Thu, 9 Mar 2023 10:29:00 +0100
Subject: [PATCH] Correctly set previous session state to enable cleaner
 session

---
 ReleaseNotes.md                    |  1 +
 tapeserver/daemon/DriveHandler.cpp | 11 ++++++-----
 2 files changed, 7 insertions(+), 5 deletions(-)

diff --git a/ReleaseNotes.md b/ReleaseNotes.md
index 93a433951e..d768358f82 100644
--- a/ReleaseNotes.md
+++ b/ReleaseNotes.md
@@ -13,6 +13,7 @@
 - cta/CTA#310 - Trigger cleanup session if taped child process did not exit with success code
 - cta/CTA#320 - CTA No Oracle is failing
 - cta/CTA#322 - Queues with cleanup heartbeat above zero are not being picked for cleanup
+- cta/CTA#329 - Correctly set previous session state to enable cleaner session
 
 # v4.8.5-1
 
diff --git a/tapeserver/daemon/DriveHandler.cpp b/tapeserver/daemon/DriveHandler.cpp
index 99682456f0..fa48f3cb64 100644
--- a/tapeserver/daemon/DriveHandler.cpp
+++ b/tapeserver/daemon/DriveHandler.cpp
@@ -304,8 +304,10 @@ SubprocessHandler::ProcessingStatus DriveHandler::processEvent() {
     if (message.reportingstate()) {
       // Log a session state change
       if (m_sessionState != static_cast<SessionState>(message.sessionstate())) {
-        scoped.add("PreviousState", session::toString(m_sessionState))
-              .add("PreviousType", session::toString(m_sessionType))
+        m_previousState = m_sessionState;
+        m_previousType = m_sessionType;
+        scoped.add("PreviousState", session::toString(m_previousState))
+              .add("PreviousType", session::toString(m_previousType))
               .add("NewState", session::toString(static_cast<SessionState>(message.sessionstate())))
               .add("NewType", session::toString(static_cast<SessionType>(message.sessiontype())));
         m_processManager.logContext().log(log::INFO, "In DriveHandler::processEvent(): changing session state");
@@ -585,7 +587,6 @@ SubprocessHandler::ProcessingStatus DriveHandler::processFatal(serializers::Watc
   // shut down (central storage unavailable).
   log::ScopedParamContainer params(m_processManager.logContext());
   params.add("tapeDrive", m_configLine.unitName);
-  m_sessionVid = "";
   m_processingStatus.shutdownRequested = true;
   m_processManager.logContext().log(log::CRIT,
                                     "In DriveHandler::processFatal(): shutting down after fatal failure.");
@@ -1158,8 +1159,8 @@ SubprocessHandler::ProcessingStatus DriveHandler::shutdown() {
 
   std::set<SessionState> statesRequiringCleaner = { SessionState::Mounting,
     SessionState::Running, SessionState::Unmounting };
-  if (statesRequiringCleaner.count(m_sessionState)) {
-    if (!m_sessionVid.size()) {
+  if (statesRequiringCleaner.count(m_previousState)) {
+    if (m_sessionVid.empty()) {
       lc.log(log::ERR, "In DriveHandler::shutdown(): Should run cleaner but VID is missing. Do nothing.");
     }
     else {
-- 
GitLab