Skip to content

Conversation

cdce8p
Copy link
Collaborator

@cdce8p cdce8p commented Aug 27, 2025

@cdce8p cdce8p marked this pull request as draft August 27, 2025 23:52
@cdce8p cdce8p added the topic-mypyc mypyc bugs label Aug 27, 2025
@cdce8p cdce8p force-pushed the mypyc-fix-object-finalization branch from cf3dd49 to 5b92c8b Compare August 28, 2025 00:30
Comment on lines 853 to 856
emitter.emit_line("if (PyErr_Occurred() != NULL) {")
emitter.emit_line("PyErr_WriteUnraisable((PyObject *)self);")
emitter.emit_line("}")
emitter.emit_line("PyErr_Restore(type, value, traceback);")
Copy link
Member

@ZeroIntensity ZeroIntensity Aug 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this code to the done label below.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to work unfortunately. If I move it, other tests start to fail. E.g. mypyc/test/test_run.py::TestRun::run-exceptions.test::testTryExcept with:

  Traceback (most recent call last): (diff)
    File "testutil.py", line 46, in assertRaises (diff)
      yield (diff)
    File "driver.py", line 42, in <module> (diff)
      list(iter_exception()) (diff)
      ~~~~^^^^^^^^^^^^^^^^^^ (diff)
  SystemError: error return without exception set (diff)
   (diff)
  During handling of the above exception, another exception occurred: (diff)
   (diff)
  Traceback (most recent call last): (diff)
    File "driver.py", line 41, in <module> (diff)
      with assertRaises(IndexError): (diff)
           ~~~~~~~~~~~~^^^^^^^^^^^^ (diff)
    File "/Users/marc/Develop/01_temp/cpython/Lib/contextlib.py", line 162, in __exit__ (diff)
      self.gen.throw(value) (diff)
      ~~~~~~~~~~~~~~^^^^^^^ (diff)
    File "testutil.py", line 48, in assertRaises (diff)
      assert type(e) is typ, f"{e!r} is not a {typ.__name__}" (diff)
             ^^^^^^^^^^^^^^ (diff)
  AssertionError: SystemError('error return without exception set') is not a IndexError (diff)

So I think PyErr_Restore needs to happen earlier.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, that's not making sense to me. Is the done label not getting hit in all cases?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is, but some cleanup functions from mypyc seem to require that the exception is set properly.
Just the goto done; call is never used AFAICT.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that's a little messy. As long as they don't overwrite the exception, I guess it's fine.

@cdce8p
Copy link
Collaborator Author

cdce8p commented Aug 28, 2025

Hmm, not quite sure why the exit code is -11.

@ZeroIntensity
Copy link
Member

That's the segfault exit code.

@cdce8p
Copy link
Collaborator Author

cdce8p commented Aug 28, 2025

That's the segfault exit code.

I can remove the PyErr_WriteUnraisable call and the test case itself would be fixed. However, multiple other tests would fail with -6 error code then.

@cdce8p cdce8p force-pushed the mypyc-fix-object-finalization branch from 025423f to f62a220 Compare September 2, 2025 12:12
@cdce8p cdce8p marked this pull request as ready for review September 2, 2025 12:26
@cdce8p
Copy link
Collaborator Author

cdce8p commented Sep 2, 2025

Hmm, not quite sure why the exit code is -11.

Got it fixed. If an error occurred and PyErr_WriteUnraisable(self) is called, PyObject_GC_UnTrack(self) should be skipped as well.

@cdce8p cdce8p requested a review from JukkaL September 2, 2025 12:32
Copy link
Collaborator

@JukkaL JukkaL left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix! I verified that this fixes the segfault on macOS and Python 3.13.

@JukkaL JukkaL merged commit 7e7d7a7 into python:master Sep 2, 2025
13 checks passed
@cdce8p cdce8p deleted the mypyc-fix-object-finalization branch September 2, 2025 15:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic-mypyc mypyc bugs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Object finalization isn't handled correctly

3 participants