Skip to content

Commit efc88a7

Browse files
committed
[textinput] handle Unix SIGTERM to save and close files before exit
Fixes #13300 [core] Implement public static functions allowing save and close [io] only SaveAndClose TFiles and derived TSystemFile, in general, does not implemente Write and Close methods [io] Do not write/close files open in READ mode [core] use TDirectory instead TMethodCall
1 parent 2a6eaa1 commit efc88a7

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

core/base/inc/TROOT.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ friend TROOT *ROOT::Internal::GetROOT2();
344344
Int_t Timer() const { return fTimer; }
345345

346346
//---- static functions
347+
static void CleanUpROOTAtExit();
347348
static Int_t DecreaseDirLevel();
348349
static Int_t GetDirLevel();
349350
static const char *GetMacroPath();
@@ -356,6 +357,7 @@ friend TROOT *ROOT::Internal::GetROOT2();
356357
static Int_t ConvertVersionCode2Int(Int_t code);
357358
static Int_t ConvertVersionInt2Code(Int_t v);
358359
static Int_t RootVersionCode();
360+
static void WriteCloseAllFiles();
359361
static const std::vector<std::string> &AddExtraInterpreterArgs(const std::vector<std::string> &args);
360362
static const char**&GetExtraInterpreterArgs();
361363

core/base/src/TROOT.cxx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ FARPROC dlsym(void *library, const char *function_name)
119119
#include "TClass.h"
120120
#include "TClassEdit.h"
121121
#include "TClassGenerator.h"
122+
#include "TDirectory.h"
122123
#include "TDataType.h"
123124
#include "TStyle.h"
124125
#include "TObjectTable.h"
@@ -226,10 +227,33 @@ static Int_t ITIMQQ(const char *time)
226227
return 100*hh + mm;
227228
}
228229

230+
////////////////////////////////////////////////////////////////////////////////
231+
/// Write and Close all open writable TFiles, useful to be called when SIGTERM is caught.
232+
233+
void TROOT::WriteCloseAllFiles()
234+
{
235+
if (gROOT) {
236+
R__LOCKGUARD(gROOTMutex);
237+
238+
if (gROOT->GetListOfFiles()) {
239+
TIter next(gROOT->GetListOfFiles());
240+
while (TObject *obj = next()) {
241+
if (obj && obj->InheritsFrom(TClass::GetClass("TFile", kFALSE, kTRUE))) {
242+
auto fobj = static_cast<TDirectory *>(obj);
243+
if (fobj->IsWritable()) {
244+
fobj->Write();
245+
fobj->Close();
246+
}
247+
}
248+
}
249+
}
250+
}
251+
}
252+
229253
////////////////////////////////////////////////////////////////////////////////
230254
/// Clean up at program termination before global objects go out of scope.
231255

232-
static void CleanUpROOTAtExit()
256+
void TROOT::CleanUpROOTAtExit()
233257
{
234258
if (gROOT) {
235259
R__LOCKGUARD(gROOTMutex);

core/textinput/src/textinput/TerminalConfigUnix.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include <stdio.h>
2525
#include <cstring>
2626

27+
#include <TROOT.h>
28+
2729
using namespace textinput;
2830
using std::memcpy;
2931
using std::signal;
@@ -104,6 +106,11 @@ TerminalConfigUnix::HandleSignal(int signum) {
104106
}
105107
}
106108

109+
// gentle save and close if SIGTERM
110+
if (signum == SIGTERM) {
111+
TROOT::WriteCloseAllFiles();
112+
TROOT::CleanUpROOTAtExit();
113+
}
107114
// No previous handler found, re-raise to get default handling:
108115
signal(signum, SIG_DFL); // unregister ourselves
109116
raise(signum); // terminate through default handler

0 commit comments

Comments
 (0)