`@timeout` stays armed after the step raises, so it can fire while the failed task is persisted
@timeout arms signal.alarm in task_pre_step and only clears it in task_post_step, which only runs when the step returns. if the step raises, the alarm is still armed while task.py runs the task_exception hooks and output.persist. so if persisting takes longer than what's left of the timeout, the alarm goes off in the middle of it:
- with
@catch, that attempt dies, the fallback attempt runs, and@catchrecords "Your code did not raise an exception..." instead of the real error - without
@catch, the console says the step timed out andTask.exceptionisNone
repro: the step raises right away with a 2s timeout, and pickling its artifact takes 5s (stand-in for a big artifact):
import time
from metaflow import FlowSpec, catch, step, timeout
class SlowToPickle(object):
def __reduce__(self):
time.sleep(5)
return (SlowToPickle, ())
class TimeoutCatchFlow(FlowSpec):
@catch(var="err", print_exception=False)
@timeout(seconds=2)
@step
def start(self):
self.payload = SlowToPickle()
raise ValueError("boom")
self.next(self.end)
@step
def end(self):
print("caught by @catch:", repr(self.err.exception))
if __name__ == "__main__":
TimeoutCatchFlow()
python timeout_catch_repro.py --no-pylint run on master (bda5e774) and 2.19.41, trimmed:
[.../start/1 (pid 3056)] Step start timed out after 0 hours, 0 minutes, 2 seconds
...
[.../start/1 (pid 3056)] > File ".../metaflow/task.py", line 1002, in run_step
[.../start/1 (pid 3056)] > output.persist(self.flow)
...
[.../start/1 (pid 3056)] Task failed.
[.../start/1 (pid 3059)] Task fallback is starting to handle the failure.
[.../end/2 (pid 3062)] caught by @catch: 'Task execution kept failing over 1 attempts. Your code did not raise an exception. Something in the execution environment caused the failure.'
expected caught by @catch: 'boom' on the first attempt. (Python 3.12 on Linux, local runtime.)
the fix is small: clear the alarm in a task_exception hook too, like the success path already does before persisting. I have it with a test, happy to open a PR if that sounds right.
used claude code to help with this.
Hi, is this still available to pick up? I'd like to work on a fix.
hey, I already have the fix and a test for this on a branch (mentioned at the bottom of the issue). just waiting on a maintainer to ok the approach before I open the PR.