Skip to content

AirflowTaskTimeout should inherit BaseException instead of Exception? #35644

Closed

Description

@potiuk

Discussed in #35643

Originally posted by hterik November 14, 2023

Apache Airflow version

2.7.3

What happened

Assume following code runs inside airflow task:

errors = []
for item in items_to_process:
    try: 
        process(item)   # This can take several minutes/hours each
    except Exception as e:
        errors.append(e)
if errors:
    raise Exception(......)

If the Airflow task time out, what happens is that it injects an AirflowTaskTimeout exception where the code is currently running now.
If the code is designed to capture exceptions, it will capture the timeout and potentially continue running for several hours anyway.

What you think should happen instead

I think it would be better if AirflowTaskTimeout was treated similarly to KeyboardInterrupt, so it need explicit except block to capture.
Moving it outside of the Exception inheritance tree to inherit directly from BaseException would solve that.
See https://docs.python.org/3/library/exceptions.html#exception-hierarchy
Timeouts are generally in place to be last resort of aborting process by the higher level system if the lower level code has bugs in it, so it's safer to assume that code that doesn't deal with catching timeout should be aborted.

How to reproduce

See above

Operating System

NA

Versions of Apache Airflow Providers

apache-airflow==2.7.1

Deployment

Official Apache Airflow Helm Chart

Deployment details

No response

Anything else

No response

Are you willing to submit PR?

  • Yes I am willing to submit a PR!

Code of Conduct

Activity

  1. added
    kind:bug
    needs-triage
    kind:feature
    and removed
    kind:bug
    on Nov 15, 2023
  2. potiuk commented on Nov 15, 2023

    @potiuk
    MemberAuthor

    Yes. good idea. Might be worth implementing it.
    BTW. There is another problem with tasks running low-level C-code that are not interrupted by signal (and should be handled in yet another way) - see #35474 - but indeed when code is catching all exceptions, chaning timeout to BaseException should work well. Would you like to implement it @hterik ?

  3. Taragolis commented on Nov 15, 2023

    @Taragolis
    Contributor

    That is exactly what a Option 1 propose 😄

  4. hterik commented on Nov 15, 2023

    @hterik
    Contributor

    Proposed change here #35653

  5. potiuk commented on Nov 15, 2023

    @potiuk
    MemberAuthor

    That is exactly what a Option 1 propose 😄

    Yeah. But it's not the same issue (or at least I believe it's not) - I believe the original issue was connected with SIGALRM not being properly handled by long-running low-level c-code. Catching and ignoring exceptions is a bad programming habit by anyone who run task in a loop from example described hy @hterik. You should generally ignore known exceptions not all of them if you want to add them to the loop in the fashon described here.

    We should still make the change to AirflowTaskTimeout inheriting from BaseException to handle this "bad programming" case of course - (we do not want our users to shoot themselves in a foot by not knowing that they are doing something wrong) - but I think Kafka case from #35474 was somethign entirely different (only loosely related). I highly doubt somone in Kafka code handles all exceptions this way - my guess is that SIGALRM was not handled there :)

  6. Taragolis commented on Nov 15, 2023

    @Taragolis
    Contributor

    Yeah. But it's not the same issue (or at least I believe it's not) - I believe the original issue was connected with SIGALRM not being properly handled by long-running low-level c-code.

    To be honest we don't know what the reason in particular this cases. It could be both:

    1. Redefine SIGALRM handlers in upstream code
    2. To broad catch exceptions in libraries or user code

    In second case when Airflow task SIGALARM works, we do not control in which place in Main thread code returns

    In this case it will break Airflow timeout, and in addition it might breaks something else

    try
        ...  # <- returns here
    except Exception:
        pass

    In case if handler return in this code block, then change inheritance wouldn't help at all

    try
        ...  # <- returns here
    except:  # or except BaseException:
        pass
  7. 8 remaining items

  8. potiuk commented on Nov 29, 2023

    @potiuk
    MemberAuthor

    So ? Any more concerns :) ?

  9. jscheffl commented on Dec 31, 2023

    @jscheffl
    Contributor

    The bug report and discussion seems to be a bit stale. Just looking through the PR it looks good and reasonable for me. There might be side effects but we can handle this via a newsfragment I think.
    To be on the "safer" side we might put this to a "functional" release, meaning 2.9.0 so that people just upgrading for fixes do catch less "surprises".

  10. removed
    needs-triage
    on Dec 31, 2023
  11. added a commit that references this issue on Feb 21, 2024
    41a6c6e
  12. added a commit that references this issue on Feb 21, 2024
    581e2e4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions