Skip to content

Commit

Permalink
Optionally disable pipeline halt on SIGINT (#154)
Browse files Browse the repository at this point in the history
Optional argument `signal_halt` for the `G3Pipeline.Run()` method.  By default, this is set to True to enable current behavior, namely that a pipeline is halted (via the `halt_processing` flag) and terminates after the current frame reaches the end of the pipeline.  If False, this signal handling is disabled, and it is then up to the user to ensure that a running pipeline is properly halted on interrupt.
  • Loading branch information
arahlin authored Apr 2, 2024
1 parent d8cc90b commit 96a6364
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
2 changes: 1 addition & 1 deletion core/include/core/G3Pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class G3Pipeline {

// Run the pipeline to completion. If profile is set to true, will print
// statistics for each module at completion.
void Run(bool profile = false, bool graph = false);
void Run(bool profile = false, bool graph = false, bool signal_halt = true);

// If there is stored graph information get it
std::string GetGraphInfo() const {
Expand Down
19 changes: 11 additions & 8 deletions core/src/G3Pipeline.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ G3Pipeline::siginfo_catcher(int)
#endif

void
G3Pipeline::Run(bool profile, bool graph)
G3Pipeline::Run(bool profile, bool graph, bool signal_halt)
{
struct rusage last_rusage;
std::vector<G3Pipeline_mod_data> mods;
Expand All @@ -239,12 +239,14 @@ G3Pipeline::Run(bool profile, bool graph)
getrusage(RUSAGE_THREAD, &last_rusage);
#endif

// Catch SIGINT
sigint_catcher.sa_handler = &G3Pipeline::sigint_catcher;
sigint_catcher.sa_flags = SA_RESTART | SA_RESETHAND;
sigemptyset(&sigint_catcher.sa_mask);
sigaddset(&sigint_catcher.sa_mask, SIGINT);
sigaction(SIGINT, &sigint_catcher, &oldsigint);
if (signal_halt) {
// Catch SIGINT
sigint_catcher.sa_handler = &G3Pipeline::sigint_catcher;
sigint_catcher.sa_flags = SA_RESTART | SA_RESETHAND;
sigemptyset(&sigint_catcher.sa_mask);
sigaddset(&sigint_catcher.sa_mask, SIGINT);
sigaction(SIGINT, &sigint_catcher, &oldsigint);
}

#ifdef SIGINFO
if (profile) {
Expand Down Expand Up @@ -288,7 +290,8 @@ G3Pipeline::Run(bool profile, bool graph)

// Restore old handler
G3Pipeline::halt_processing = false;
sigaction(SIGINT, &oldsigint, &sigint_catcher);
if (signal_halt)
sigaction(SIGINT, &oldsigint, &sigint_catcher);
#ifdef SIGINFO
if (profile)
sigaction(SIGINFO, &oldsiginfo, &siginfo_catcher);
Expand Down
11 changes: 7 additions & 4 deletions core/src/python.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -578,17 +578,20 @@ SPT3G_PYTHON_MODULE(core)
"\t processing if returned by first module. Equivalent to [].\n")
.def("_Add_", &G3Pipeline::Add, bp::arg("name")="")
.def("Run", &G3Pipeline::Run,
(bp::arg("profile")=false, bp::arg("graph")=false),
(bp::arg("profile")=false, bp::arg("graph")=false,
bp::arg("signal_halt")=true),
"Run pipeline. If profile is True, print execution time "
"statistics for each module when complete. If graph is True, "
"stores control flow data that can be processed with GraphViz "
"once retrieved using GetGraphInfo().")
"once retrieved using GetGraphInfo(). If signal_halt is True "
"(default), the pipeline will stop processing new frames when "
"SIGINT is sent to this process. Equivalent to what happens when "
"halt_processing() is called.")
.def("GetGraphInfo", &G3Pipeline::GetGraphInfo,
"Get stored control flow information from Run(graph=True)")
.def("halt_processing", &G3Pipeline_halt_processing,
"Halts all running pipelines after they flush all currently "
"in-flight frames. Equivalent to what happens when SIGINT is "
"sent to this process. Once set, the first module will not be "
"in-flight frames. Once set, the first module will not be "
"called again.")
.staticmethod("halt_processing")
.def_readonly("last_frame",
Expand Down

0 comments on commit 96a6364

Please sign in to comment.