@@ -733,6 +733,7 @@ def optimize(
733733 show_console = show_console ,
734734 timeout = timeout ,
735735 )
736+ runset .raise_for_timeouts ()
736737
737738 if not runset ._check_retcodes ():
738739 msg = 'Error during optimization: {}' .format (runset .get_err_msgs ())
@@ -1167,6 +1168,8 @@ def sample(
11671168 sys .stdout .write ('\n ' )
11681169 get_logger ().info ('CmdStan done processing.' )
11691170
1171+ runset .raise_for_timeouts ()
1172+
11701173 get_logger ().debug ('runset\n %s' , repr (runset ))
11711174
11721175 # hack needed to parse CSV files if model has no params
@@ -1348,6 +1351,7 @@ def generate_quantities(
13481351 timeout = timeout ,
13491352 )
13501353
1354+ runset .raise_for_timeouts ()
13511355 errors = runset .get_err_msgs ()
13521356 if errors :
13531357 msg = (
@@ -1518,6 +1522,7 @@ def variational(
15181522 show_console = show_console ,
15191523 timeout = timeout ,
15201524 )
1525+ runset .raise_for_timeouts ()
15211526
15221527 # treat failure to converge as failure
15231528 transcript_file = runset .stdout_files [dummy_chain_id ]
@@ -1606,7 +1611,15 @@ def _run_cmdstan(
16061611 universal_newlines = True ,
16071612 )
16081613 if timeout :
1609- timer = threading .Timer (timeout , proc .terminate )
1614+
1615+ def _timer_target ():
1616+ # Abort if the process has already terminated.
1617+ if proc .poll () is not None :
1618+ return
1619+ proc .terminate ()
1620+ runset ._set_timeout_flag (idx , True )
1621+
1622+ timer = threading .Timer (timeout , _timer_target )
16101623 timer .setDaemon (True )
16111624 timer .start ()
16121625 else :
@@ -1623,11 +1636,9 @@ def _run_cmdstan(
16231636
16241637 stdout , _ = proc .communicate ()
16251638 retcode = proc .returncode
1639+ runset ._set_retcode (idx , retcode )
16261640 if timer :
16271641 timer .cancel ()
1628- if retcode == - 15 :
1629- retcode = 60
1630- runset ._set_retcode (idx , retcode )
16311642
16321643 if stdout :
16331644 fd_out .write (stdout )
0 commit comments