Fix traceback circular reference detection to pass test_raise.py#5995
Closed
Fix traceback circular reference detection to pass test_raise.py#5995
Conversation
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Remove
Fix traceback circular reference detection to pass test_raise.py
Jul 17, 2025
@unittest.expectedFailure from test_raise.py 's TestTracebackType.test_attrs. Run the test and fix the errors by editing traceback.rs
youknowone
reviewed
Jul 17, 2025
vm/src/builtins/traceback.rs
Outdated
Comment on lines
+68
to
+82
| if let Some(ref new_tb) = value { | ||
| // Check for direct loop (tb.tb_next = tb) | ||
| if std::ptr::eq(self as *const _, new_tb.deref().deref() as *const _) { | ||
| return Err(vm.new_value_error("circular reference in traceback chain".to_owned())); | ||
| } | ||
|
|
||
| // Check for indirect loops by walking the chain | ||
| let mut current = new_tb.tb_next(); | ||
| while let Some(ref tb) = current { | ||
| if std::ptr::eq(self as *const _, tb.deref().deref() as *const _) { | ||
| return Err(vm.new_value_error("circular reference in traceback chain".to_owned())); | ||
| } | ||
| current = tb.tb_next(); | ||
| } | ||
| } |
Member
There was a problem hiding this comment.
This is too ad-hoc. You must create a more general logic to detect circular reference in tb_next until the end.
Contributor
Author
There was a problem hiding this comment.
Implemented a more general cycle detection algorithm using Floyd's cycle detection (tortoise and hare). The new approach simulates the assignment and detects any cycles in the resulting chain rather than only checking for specific patterns. See commit ce5fe45.
Replace ad-hoc circular reference checking with Floyd's cycle detection algorithm for more comprehensive cycle detection. Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes the traceback
tb_nextsetter to properly detect and prevent circular references, enablingtest_raise.py::TestTracebackType.test_attrsto pass.Problem
The test
TestTracebackType.test_attrsintest_raise.pywas marked with@unittest.expectedFailurebecause the traceback implementation was missing circular reference detection. The test expects:But the current implementation allowed these assignments without validation.
Solution
Added circular reference detection to the
set_tb_nextmethod invm/src/builtins/traceback.rs:selfselfdoesn't appear anywhere in the chainstd::ptr::eqwith correct dereferencing to compare the underlyingPyTracebackobjectsThe implementation raises
ValueError("circular reference in traceback chain")when a circular reference is detected, matching CPython's behavior.Changes
@unittest.expectedFailuredecorator fromTestTracebackType.test_attrsPyTraceback::set_tb_next()std::ops::Derefimport for proper object dereferencingTesting
TestTracebackType.test_attrsnow passestest_raise.pycontinue to passThe fix is minimal and surgical, only adding the necessary validation without changing any existing working functionality.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.