Skip to content

RecursionError not caught explicitly may crash with "Aborted (core dumped)" #81546

Closed

Description

@Delgan
mannequin
BPO 37365
Nosy @ned-deily, @Delgan
Superseder
  • bpo-18075: Infinite recursion tests triggering a segfault
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-06-21.18:35:45.627>
    created_at = <Date 2019-06-21.16:08:01.315>
    labels = ['type-crash']
    title = 'RecursionError not caught explicitly may crash with "Aborted (core dumped)"'
    updated_at = <Date 2019-06-22.08:12:39.116>
    user = 'https://github.com/Delgan'

    bugs.python.org fields:

    activity = <Date 2019-06-22.08:12:39.116>
    actor = 'Delgan'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-06-21.18:35:45.627>
    closer = 'ned.deily'
    components = []
    creation = <Date 2019-06-21.16:08:01.315>
    creator = 'Delgan'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37365
    keywords = []
    message_count = 3.0
    messages = ['346232', '346242', '346271']
    nosy_count = 2.0
    nosy_names = ['ned.deily', 'Delgan']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '18075'
    type = 'crash'
    url = 'https://bugs.python.org/issue37365'
    versions = []

    Activity

    1. Delgan commented on Jun 21, 2019

      Delganmannequin
      MannequinAuthor

      This is an issue that I first saw repoted by @remi.lapeyre on bpo-36272: https://bugs.python.org/issue36272#msg337843
      It also be reported some years ago: https://stackoverflow.com/a/54171640/2291710
      I searched but I did not find any existing issue, so I open a new one, I hope this is not a duplicate.

      It's easily reproducible:

      def rec():
          try:
              rec()
          except Exception:
              rec()
      rec()

      It will kill the CPython interpreter (@remi.lapeyre provided explanation in the issue I linked).

      Of course, this is a contrived example. Still, I guess CPython should avoid to crash in any situation. This is a tricky edge case: by doing `except Exception:` one could expect to correctly handle all kind of errors without being aware that in some situation the "RecursionError" need to be managed too.

      Particularly, the fix in bpo-36272 solely patches "StreamHandler":

      def emit(self, record):
      """
      Emit a record.
      If a formatter is specified, it is used to format the record.
      The record is then written to the stream with a trailing newline. If
      exception information is present, it is formatted using
      traceback.print_exception and appended to the stream. If the stream
      has an 'encoding' attribute, it is used to determine how to do the
      output to the stream.
      """
      try:
      msg = self.format(record)
      stream = self.stream
      # issue 35046: merged two stream.writes into one.
      stream.write(msg + self.terminator)
      self.flush()
      except RecursionError: # See issue 36272
      raise
      except Exception:
      self.handleError(record)

      There is many other handlers that are still affected, every time you see "except Exception" in "emit()": https://github.com/python/cpython/blob/65f64b1903ae85b97a30f514bbc1b7ce940c3af2/Lib/logging/handlers.py

      Finally, it should also be noted that the "except RecursionError" workaround implies that loggers are not longer able to catch recursive erros in "write()" or "format()" without re-raising the RecursionError and (probably) halting the program.
      It may happen for example that a faulty implementation of a custom Handler or LogRecord attribute cause a RecursionError. Thanks to the "raiseExceptions = false" logging option, this would usually be caught and a message printed on stderr without killing the app.

    2. added
      type-crash
      on Jun 21, 2019
    3. ned-deily commented on Jun 21, 2019

      @ned-deily
      Member

      Thank you for the report. The issue of the interpreter not being able to gracefully handle a stack overflow error is an old one: see, for example bpo-18075 or bpo-35542. So far, no one has proposed a solution and, as can be seen in bpo-18075, workarounds someimes produce unexpected side-effects that can be more problematic than the stack overflow segfault. I'm going to close this as a duplicate of bpo-18075. If anyone has further suggestions for solutions, please continue the discussion there.

    4. Delgan commented on Jun 22, 2019

      Delganmannequin
      MannequinAuthor

      Thanks to your answer, @ned.deily.

      Sorry to insist, but are you sure this is a duplicate of bpo-18075 / bpo-35542?

      According to the discussion in bpo-36272, it seems this is not related to stack exhaustion nor to bpo-18075.

      Also, @remi.lapeyre suggested that a fix could be possible by modifying ceval.c. (re-raise RecursionError if it has been cleared).

      bpo-18075 and bpo-35542 are both of status "closed", I would expect this issue to be duplicate to an "open" issue because there is a clear reproducible "bug" which crashes the interpreter.

      There is also the problems still remaining in logging Handlers that I linked.

      I think this is an issue with how recursion error are handled in ceval.c, this is related to "tstate->overflowed" and "tstate->recursion_depth > recursion_limit + 50".
      If I understand correctly, this is implemented like this to allow gracefully catch and handle RecursionError from the calling function.
      For example, why not re-raise the exception if the first one could not be handled correctly?

    5. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      type-crash

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions