Skip to content

Commit

Permalink
force termination after 5 interrupt signals
Browse files Browse the repository at this point in the history
  • Loading branch information
svigerske committed Jul 3, 2024
1 parent d0e5e73 commit c3b717e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/Apps/AmplSolver/AmplTNLP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ void AmplTNLP::gutsOfConstructor(
}
}

if( checkinterrupt_ && !RegisterInterruptHandler(NULL, &interrupted_) )
if( checkinterrupt_ && !RegisterInterruptHandler(NULL, &interrupted_, 5) )
{
jnlst_->Printf(J_STRONGWARNING, J_MAIN, "Could not register handler for interrupt signals.\n");
}
Expand Down
26 changes: 11 additions & 15 deletions src/Common/IpUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ Number WallclockTime()
}

static bool registered_handler = false;
static unsigned int abortcountdown_ = std::numeric_limits<unsigned int>::max();
static void (*handle_interrupt_)(void) = NULL;
static bool* interrupt_flag_ = NULL;

Expand All @@ -210,26 +211,26 @@ static void sighandler(
{
(*handle_interrupt_)();
}

if( --abortcountdown_ == 0 )
{
fputs("Ipopt sighandler: Too many interrupt signals. Forcing termination.\n", stderr);
exit(1);
}
}

/** register handler for interrupt signals
*
* On POSIX systems, catches SIGHUP and SIGINT signals.
* On Windows, catches SIGTERM, SIGABRT, SIGBREAK, and SIGINT signals.
*
* @return whether registering the handler was successful
* @since 3.14.17
*/
bool RegisterInterruptHandler(
void (*handle_interrupt)(void), /**< function to call when interrupted by signal, if not NULL */
bool* interrupt_flag /**< variable to set to true when interrupted by signal, if not NULL */
void (*handle_interrupt)(void),
bool* interrupt_flag,
unsigned int abortlimit
)
{
if( registered_handler )
{
return false;
}
registered_handler = true;
abortcountdown_ = abortlimit;

handle_interrupt_ = handle_interrupt;
interrupt_flag_ = interrupt_flag;
Expand Down Expand Up @@ -260,11 +261,6 @@ bool RegisterInterruptHandler(
return true;
}

/** unregister previously registered handler for interrupt signals
*
* @return whether registering the handler was successful
* @since 3.14.17
*/
bool UnregisterInterruptHandler(void)
{
if( !registered_handler )
Expand Down
5 changes: 3 additions & 2 deletions src/Common/IpUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ IPOPTLIB_EXPORT Number WallclockTime();
* @since 3.14.17
*/
IPOPTLIB_EXPORT bool RegisterInterruptHandler(
void (*handle_interrupt)(void), /**< function to call when interrupted by signal, if not NULL */
bool* interrupt_flag /**< variable to set to true when interrupted by signal, if not NULL */
void (*handle_interrupt)(void), /**< function to call when interrupted by signal, if not NULL */
bool* interrupt_flag, /**< variable to set to true when interrupted by signal, if not NULL */
unsigned int abortlimit = std::numeric_limits<unsigned int>::max() /**< if interrupt signal has been send this many times, then exit(1) */
);

/** unregister previously registered handler for interrupt signals
Expand Down

0 comments on commit c3b717e

Please sign in to comment.