Skip to content

Segfault with deep recursion into object().__dir__ #78111

Closed

Description

@a-j-buxton
BPO 33930
Nosy @nascheme, @rhettinger, @terryjreedy, @ronaldoussoren, @vstinner, @taleinat, @benjaminp, @methane, @ambv, @serhiy-storchaka, @pablogsal, @miss-islington, @augustogoulart, @iritkatriel
PRs
  • bpo-33930: Fix segfault with deep recursion when cleaning method objects #27678
  • [3.10] bpo-33930: Fix segfault with deep recursion when cleaning method objects (GH-27678) #27719
  • [3.9] bpo-33930: Fix segfault with deep recursion when cleaning method objects (GH-27678) #27720
  • [3.8] bpo-33930: Fix segfault with deep recursion when cleaning method objects (GH-27678) #27721
  • bpo-33930: Fix typo in the test name. #27733
  • [3.9] bpo-33930: Fix typo in the test name. (GH-27733) #27734
  • [3.8] bpo-33930: Fix typo in the test name. (GH-27733) #27735
  • [3.10] bpo-33930: Fix typo in the test name. #27736
  • Files
  • bpo-33930.patch
  • 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 2021-08-13.09:44:37.226>
    created_at = <Date 2018-06-21.11:27:57.218>
    labels = ['interpreter-core', '3.10', '3.9', 'type-crash', '3.11']
    title = 'Segfault with deep recursion into object().__dir__'
    updated_at = <Date 2021-08-13.09:45:17.009>
    user = 'https://bugs.python.org/a-j-buxton'

    bugs.python.org fields:

    activity = <Date 2021-08-13.09:45:17.009>
    actor = 'miss-islington'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-08-13.09:44:37.226>
    closer = 'lukasz.langa'
    components = ['Interpreter Core']
    creation = <Date 2018-06-21.11:27:57.218>
    creator = 'a-j-buxton'
    dependencies = []
    files = ['47952']
    hgrepos = []
    issue_num = 33930
    keywords = ['patch']
    message_count = 38.0
    messages = ['320167', '320263', '320413', '320418', '320458', '320478', '320504', '320509', '329839', '329846', '330516', '330520', '330563', '330579', '330635', '330776', '330780', '330785', '399125', '399265', '399357', '399366', '399373', '399374', '399375', '399402', '399403', '399404', '399408', '399409', '399411', '399421', '399425', '399429', '399430', '399431', '399511', '399512']
    nosy_count = 15.0
    nosy_names = ['nascheme', 'rhettinger', 'terry.reedy', 'ronaldoussoren', 'vstinner', 'taleinat', 'benjamin.peterson', 'methane', 'lukasz.langa', 'serhiy.storchaka', 'pablogsal', 'miss-islington', 'augustogoulart', 'a-j-buxton', 'iritkatriel']
    pr_nums = ['27678', '27719', '27720', '27721', '27733', '27734', '27735', '27736']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'crash'
    url = 'https://bugs.python.org/issue33930'
    versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

    Activity

    1. a-j-buxton commented on Jun 21, 2018

      a-j-buxtonmannequin
      MannequinAuthor

      The following small snippet of code will crash 3.6.5 with a segfault.

      The crash occurs at cleanup, so it won't happen in the interactive interpreter (until you exit).

      # --- code ---

      o = object()
      for x in range(1000000):
          o = o.__dir__
          print(x, id(o.__dir__))

      # --- end code ---

    2. added
      interpreter-core
      type-crash
      on Jun 21, 2018
    3. terryjreedy commented on Jun 22, 2018

      @terryjreedy
      Member

      Serhiy, is this one of the cases we decided to leave as won't fix?

    4. ronaldoussoren commented on Jun 25, 2018

      @ronaldoussoren
      Contributor

      The crash is likely caused by recursion during the clean-up of the object().__dir__.__dir__.... chain.

      The trashcan API (see Py_TRASHCAN_SAFE_BEGIN/Py_TRASHCAN_SAFE_END in Include/object.h) can help here.

      Those do some real work and have a cost, I can't predict if that cost is substational enough to affect benchmarks (method objects are destroyed a lot).

    5. serhiy-storchaka commented on Jun 25, 2018

      @serhiy-storchaka
      Member

      The trashcan mechanism is used for slot wrappers. The following code doesn't crash:

      o = object()
      for x in range(1000000):
          o = o.__hash__
    6. rhettinger commented on Jun 26, 2018

      @rhettinger
      Contributor

      Alistair, how did you happen upon this case?

    7. vstinner commented on Jun 26, 2018

      @vstinner
      Member

      C traceback in gdb:

      Program received signal SIGSEGV, Segmentation fault.
      0x00000000004812a8 in meth_dealloc (
      m=<error reading variable: Cannot access memory at address 0x7fffff7fefd8>)
      at Objects/methodobject.c:86
      86 {
      (gdb) where
      #0 0x00000000004812a8 in meth_dealloc (
      m=<error reading variable: Cannot access memory at address 0x7fffff7fefd8>)
      at Objects/methodobject.c:86
      #1 0x0000000000488d9f in _Py_Dealloc (
      op=<built-in method __dir__ of builtin_function_or_method object at remote 0x7fffe3973490>) at Objects/object.c:1931
      #2 0x00000000004813c4 in meth_dealloc (m=0x7fffe3973580)
      at Objects/methodobject.c:91
      #3 0x0000000000488d9f in _Py_Dealloc (
      op=<built-in method __dir__ of builtin_function_or_method object at remote 0x7fffe3973508>) at Objects/object.c:1931
      #4 0x00000000004813c4 in meth_dealloc (m=0x7fffe39735f8)
      at Objects/methodobject.c:91
      #5 0x0000000000488d9f in _Py_Dealloc (
      op=<built-in method __dir__ of builtin_function_or_method object at remote 0x7fffe3973580>) at Objects/object.c:1931
      #6 0x00000000004813c4 in meth_dealloc (m=0x7fffe3973670)
      at Objects/methodobject.c:91
      #7 0x0000000000488d9f in _Py_Dealloc (
      op=<built-in method __dir__ of builtin_function_or_method object at remote 0x7fffe39735f8>) at Objects/object.c:1931
      #8 0x00000000004813c4 in meth_dealloc (m=0x7fffe39736e8)
      at Objects/methodobject.c:91
      (...)

    8. a-j-buxton commented on Jun 26, 2018

      a-j-buxtonmannequin
      MannequinAuthor

      I found this while chatting on IRC about the implementation details of objects - specifically, since objects have a dict of attributes, and dict subclasses object, it seems like every dict will have another dict inside it. (Eventually someone explained that base object() doesn't have any state at all, as a result dict doesn't have a dict of attributes, and there is no cycle.)

      Anyway, as I was trying to explain my question to the other users, I had intended to post this snippet and ask "why doesn't this crash python?" Ironically it turned out that it does crash python.

    9. ronaldoussoren commented on Jun 26, 2018

      @ronaldoussoren
      Contributor

      @victor: thanks for confirming my analysis.

      The trashcan API does fix this issue, although I did have to change "_PyObject_GC_UNTRACK(m);" to "PyObject_GC_UnTrack(m);" as well. I haven't checked yet why that's necessary.

    10. augustogoulart commented on Nov 13, 2018

      augustogoulartmannequin
      Mannequin

      ronaldoussoren, I have tested (Debian 9) substituting "_PyObject_GC_UNTRACK(m);" to "PyObject_GC_UnTrack(m);", and although the tests pass, I'm can still see the segfault error.

      diff --git a/Objects/methodobject.c b/Objects/methodobject.c
      index cfea8cf410..cb8e279c7d 100644
      --- a/Objects/methodobject.c
      +++ b/Objects/methodobject.c
      @@ -84,7 +84,7 @@ PyCFunction_GetFlags(PyObject *op)
       static void
       meth_dealloc(PyCFunctionObject *m)
       {
      -    _PyObject_GC_UNTRACK(m);
      +    PyObject_GC_UnTrack(m);
           if (m->m_weakreflist != NULL) {
               PyObject_ClearWeakRefs((PyObject*) m);
           }
    11. augustogoulart commented on Nov 13, 2018

      augustogoulartmannequin
      Mannequin

      Sorry, I forgot to include the trashcan API. I'll double check and update the issue.

    12. augustogoulart commented on Nov 27, 2018

      augustogoulartmannequin
      Mannequin

      After a couple of rounds, this is the benchmark result I got for the attached patch. It seems to be a simple, non-performance degrading fix.

      python -m perf compare_to origin.json patch.json -G --min-speed=2
      Slower (1):

      • sqlalchemy_declarative: 213 ms +- 4 ms -> 219 ms +- 9 ms: 1.03x slower (+3%)

      Faster (9):

      • logging_format: 15.1 us +- 0.3 us -> 14.5 us +- 0.3 us: 1.05x faster (-5%)
      • hexiom: 15.0 ms +- 0.5 ms -> 14.5 ms +- 0.2 ms: 1.03x faster (-3%)
      • json_loads: 39.6 us +- 0.7 us -> 38.5 us +- 0.5 us: 1.03x faster (-3%)
      • json_dumps: 17.2 ms +- 0.4 ms -> 16.7 ms +- 0.2 ms: 1.03x faster (-3%)
      • fannkuch: 634 ms +- 4 ms -> 619 ms +- 6 ms: 1.02x faster (-2%)
      • django_template: 183 ms +- 3 ms -> 178 ms +- 5 ms: 1.02x faster (-2%)
      • float: 153 ms +- 2 ms -> 150 ms +- 1 ms: 1.02x faster (-2%)
      • raytrace: 761 ms +- 19 ms -> 745 ms +- 8 ms: 1.02x faster (-2%)
      • regex_effbot: 3.69 ms +- 0.06 ms -> 3.61 ms +- 0.05 ms: 1.02x faster (-2%)

      Benchmark hidden because not significant (47): 2to3, chaos, crypto_pyaes, deltablue, dulwich_log, go, html5lib, logging_silent, logging_simple, mako, meteor_contest, nbody, nqueens, pathlib, pickle, pickle_dict, pickle_list, pickle_pure_python, pidigits, python_startup, python_startup_no_site, regex_compile, regex_dna, regex_v8, richards, scimark_fft, scimark_lu, scimark_monte_carlo, scimark_sor, scimark_sparse_mat_mult, spectral_norm, sqlalchemy_imperative, sqlite_synth, sympy_expand, sympy_integrate, sympy_sum, sympy_str, telco, tornado_http, unpack_sequence, unpickle, unpickle_list, unpickle_pure_python, xml_etree_parse, xml_etree_iterparse, xml_etree_generate, xml_etree_process

    13. vstinner commented on Nov 27, 2018

      @vstinner
      Member
      • _PyObject_GC_UNTRACK(m);
        + PyObject_GC_UnTrack(m);

      Can someone please explain me why this change has any impact? Does it mean that _PyObject_GC_UNTRACK() macro has a bug?

      The function is quite straighforward :-)

      void
      PyObject_GC_Track(void *op)
      {
          _PyObject_GC_TRACK(op);
      }
    14. ronaldoussoren commented on Nov 28, 2018

      @ronaldoussoren
      Contributor

      I don’t understand this either. For some reason the patch works when using the function and not when using the macro.

      I haven’t looked into this yet, and am currently traveling. I should have time to look into this starting December 5th when I’m back home.

    15. 18 remaining items

    16. reopened this on Aug 11, 2021
    17. ambv commented on Aug 11, 2021

      @ambv
      Contributor

      Interestingly, I can't reproduce the refleaks on main either, only on 3.10.

    18. ambv commented on Aug 11, 2021

      @ambv
      Contributor

      I gotta run for the day. The shortest way for me to reproduce is to focus on the tests that were added in 3.10 and shortening the repetitions a little:

      $ time ./python.exe -E -Wd -m test test_exceptions -R3:3 -m 'test_attribute*' -m 'test_getattr*' -m 'test_lineno*' -m 'test_name_error*' -m 'test_recursion*' -m 'test_no_hang*'

      0:00:00 load avg: 5.92 Run tests sequentially
      0:00:00 load avg: 5.92 [1/1] test_exceptions
      beginning 6 repetitions
      123456
      ......
      test_exceptions leaked [2, 2, 2] references, sum=6
      test_exceptions leaked [2, 2, 2] memory blocks, sum=6
      test_exceptions failed (reference leak)

      == Tests result: FAILURE ==

      1 test failed:
      test_exceptions

      1 re-run test:
      test_exceptions

      Total duration: 11.8 sec
      Tests result: FAILURE
      12.13 real 11.44 user 0.61 sys

    19. ambv commented on Aug 11, 2021

      @ambv
      Contributor

      (BTW, with this command, it is indeed possible to trigger the ref leak on main as well, which makes it somewhat less scary.)

    20. iritkatriel commented on Aug 11, 2021

      @iritkatriel
      Member

      It seems to have been introduced by
      #27626

      I don't see how the code change could have introduced a leak. Maybe the new tests exposed a pre-existing leak?

    21. ambv commented on Aug 11, 2021

      @ambv
      Contributor

      Entirely possible, Irit. We've had just this sort of case just last week! See: https://bugs.python.org/issue44524#msg399157

    22. iritkatriel commented on Aug 12, 2021

      @iritkatriel
      Member

      I've narrowed it down a bit more and created bpo-44895.

    23. benjaminp commented on Aug 12, 2021

      @benjaminp
      Contributor

      New changeset f08e6d1 by Benjamin Peterson in branch 'main':
      bpo-33930: Fix typo in the test name. (bpo-27733)
      f08e6d1

    24. benjaminp commented on Aug 12, 2021

      @benjaminp
      Contributor

      New changeset d7f5796 by Miss Islington (bot) in branch '3.8':
      bpo-33930: Fix typo in the test name. (GH-27735)
      d7f5796

    25. miss-islington commented on Aug 12, 2021

      @miss-islington
      Contributor

      New changeset f7635f0 by Miss Islington (bot) in branch '3.9':
      [3.9] bpo-33930: Fix typo in the test name. (GH-27733) (GH-27734)
      f7635f0

    26. ambv commented on Aug 13, 2021

      @ambv
      Contributor

      Since the refleak investigation moved to its own issue, I'm re-closing this one for clarity.

    27. miss-islington commented on Aug 13, 2021

      @miss-islington
      Contributor

      New changeset ef36dfe by Benjamin Peterson in branch '3.10':
      [3.10] bpo-33930: Fix typo in the test name. (GH-27736)
      ef36dfe

    28. 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

      3.10 (EOL)3.113.9 (EOL)interpreter-coretype-crash

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions