Skip to content

Conversation

rruuaanng
Copy link
Contributor

@rruuaanng rruuaanng commented Sep 20, 2024

For the specific reasoning, please refer to #124265.
If there are any issues with my reasoning, I will close this PR.

@bedevere-app
Copy link

bedevere-app bot commented Sep 20, 2024

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@rruuaanng rruuaanng changed the title Remove redundant if statements in traceback.c gh-124265: Remove redundant if statements in traceback.c Sep 20, 2024
/* Trampoline frame */
frame = frame->previous;
}
if (frame == NULL) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why, but why do you think that frame->previous on L981 is not NULL?

Copy link
Contributor Author

@rruuaanng rruuaanng Sep 20, 2024

Choose a reason for hiding this comment

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

If frame is part of a chain and there might be a situation where it’s traversed backwards, then at that point in the code

frame = frame->previous;

It should break out. That’s what I think.
I’m not sure if my description is accurate. it’s just how I imagine the situation.

Copy link
Contributor Author

@rruuaanng rruuaanng Sep 20, 2024

Choose a reason for hiding this comment

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

static void
dump_traceback(int fd, PyThreadState *tstate, int write_header)
{
    if (write_header) {
        PUTS(fd, "Stack (most recent call first):\n");
    }

    if (tstate_is_freed(tstate)) {
        PUTS(fd, "  <tstate is freed>\n");
        return;
    }

    _PyInterpreterFrame *frame = tstate->current_frame;
    if (frame == NULL) {
        PUTS(fd, "  <no Python frame>\n");
        return;
    }

    unsigned int depth = 0;
    while (1) {
        if (MAX_FRAME_DEPTH <= depth) {
            if (MAX_FRAME_DEPTH < depth) {
                PUTS(fd, "plus ");
                _Py_DumpDecimal(fd, depth);
                PUTS(fd, " frames\n");
            }
            break;
        }
        dump_frame(fd, frame);
        frame = frame->previous;
        if (frame == NULL) {
            break;
        }
        if (frame->owner == FRAME_OWNED_BY_CSTACK) {
            /* Trampoline frame */
            frame = frame->previous;
        }
        // if (frame == NULL) {
        //     break;
        // }
        /* Can't have more than one shim frame in a row */
        assert(frame->owner != FRAME_OWNED_BY_CSTACK);
        depth++;
    }
}

I hid the targets I needed to modify by commenting them out.

cpython/Python/traceback.c

Lines 983 to 985 in 7a2d77c

if (frame == NULL) {
break;
}

Then run PCbuild/build.bat, I then ran the tests using ./python Lib\test\test_traceback.py.
It seemed to run well as it passed all 347 tests in about 8.3 seconds.

PS E:\code\cc\cpython\alpha\cpython-main> ./python .\Lib\test\test_traceback.py
Running Debug|x64 interpreter...
...........................................................................................................................................................................................................................................................................................................................................................
----------------------------------------------------------------------
Ran 347 tests in 8.303s

OK

But I can't understand why it fails on CI. Maybe I ran the test incorrectly.

@skirpichev
Copy link
Contributor

Hmm, failures seems to be related to the pr.

@bedevere-app
Copy link

bedevere-app bot commented Sep 20, 2024

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@rruuaanng
Copy link
Contributor Author

Hmm, failures seems to be related to the pr.

Well, maybe my guess is wrong.

@rruuaanng
Copy link
Contributor Author

rruuaanng commented Sep 20, 2024

I ran the test file which gave an error on ci, but the result seemed to be correct.

PS E:\code\cc\cpython\alpha\cpython-main> ./python .\Lib\test\test_faulthandler.py -v
Running Debug|x64 interpreter...
test_cancel_later_without_dump_traceback_later (__main__.FaultHandlerTests.test_cancel_later_without_dump_traceback_later) ... ok
test_disable (__main__.FaultHandlerTests.test_disable) ... ok
test_disable_windows_exc_handler (__main__.FaultHandlerTests.test_disable_windows_exc_handler) ... ok
test_disabled_by_default (__main__.FaultHandlerTests.test_disabled_by_default) ... ok
test_dump_ext_modules (__main__.FaultHandlerTests.test_dump_ext_modules) ... ok
test_dump_traceback (__main__.FaultHandlerTests.test_dump_traceback) ... ok
test_dump_traceback_fd (__main__.FaultHandlerTests.test_dump_traceback_fd) ... skipped "subprocess doesn't support pass_fds on Windows"
test_dump_traceback_file (__main__.FaultHandlerTests.test_dump_traceback_file) ... ok
test_dump_traceback_later (__main__.FaultHandlerTests.test_dump_traceback_later) ... ok
test_dump_traceback_later_cancel (__main__.FaultHandlerTests.test_dump_traceback_later_cancel) ... ok
test_dump_traceback_later_fd (__main__.FaultHandlerTests.test_dump_traceback_later_fd) ... skipped "subprocess doesn't support pass_fds on Windows"
test_dump_traceback_later_file (__main__.FaultHandlerTests.test_dump_traceback_later_file) ... ok
test_dump_traceback_later_repeat (__main__.FaultHandlerTests.test_dump_traceback_later_repeat) ... ok
test_dump_traceback_later_twice (__main__.FaultHandlerTests.test_dump_traceback_later_twice) ... ok
test_dump_traceback_threads (__main__.FaultHandlerTests.test_dump_traceback_threads) ... ok
test_dump_traceback_threads_file (__main__.FaultHandlerTests.test_dump_traceback_threads_file) ... ok
test_enable_fd (__main__.FaultHandlerTests.test_enable_fd) ... skipped "subprocess doesn't support pass_fds on Windows"
test_enable_file (__main__.FaultHandlerTests.test_enable_file) ... ok
test_enable_single_thread (__main__.FaultHandlerTests.test_enable_single_thread) ... ok
test_env_var (__main__.FaultHandlerTests.test_env_var) ... ok
test_fatal_error (__main__.FaultHandlerTests.test_fatal_error) ... ok
test_fatal_error_c_thread (__main__.FaultHandlerTests.test_fatal_error_c_thread) ... ok
test_fatal_error_without_gil (__main__.FaultHandlerTests.test_fatal_error_without_gil) ... ok
test_gc (__main__.FaultHandlerTests.test_gc) ... ok
test_gil_released (__main__.FaultHandlerTests.test_gil_released) ... ok
test_ignore_exception (__main__.FaultHandlerTests.test_ignore_exception) ... ok
test_is_enabled (__main__.FaultHandlerTests.test_is_enabled) ... ok
test_raise_exception (__main__.FaultHandlerTests.test_raise_exception) ... ok
test_raise_nonfatal_exception (__main__.FaultHandlerTests.test_raise_nonfatal_exception) ... ok
test_read_null (__main__.FaultHandlerTests.test_read_null) ... ok
test_register (__main__.FaultHandlerTests.test_register) ... skipped 'need faulthandler.register'
test_register_chain (__main__.FaultHandlerTests.test_register_chain) ... skipped 'need faulthandler.register'
test_register_fd (__main__.FaultHandlerTests.test_register_fd) ... skipped "subprocess doesn't support pass_fds on Windows"
test_register_file (__main__.FaultHandlerTests.test_register_file) ... skipped 'need faulthandler.register'
test_register_threads (__main__.FaultHandlerTests.test_register_threads) ... skipped 'need faulthandler.register'
test_sigabrt (__main__.FaultHandlerTests.test_sigabrt) ... ok
test_sigbus (__main__.FaultHandlerTests.test_sigbus) ... skipped 'need signal.SIGBUS'
test_sigfpe (__main__.FaultHandlerTests.test_sigfpe) ... skipped 'SIGFPE cannot be caught on Windows'
test_sigill (__main__.FaultHandlerTests.test_sigill) ... ok
test_sigsegv (__main__.FaultHandlerTests.test_sigsegv) ... ok
test_stack_overflow (__main__.FaultHandlerTests.test_stack_overflow) ... skipped 'need faulthandler._stack_overflow()'
test_stderr_None (__main__.FaultHandlerTests.test_stderr_None) ... ok
test_sys_xoptions (__main__.FaultHandlerTests.test_sys_xoptions) ... ok
test_truncate (__main__.FaultHandlerTests.test_truncate) ... ok
test_unregister (__main__.FaultHandlerTests.test_unregister) ... skipped 'need faulthandler.register'

@skirpichev
Copy link
Contributor

I ran the test file which gave an error on ci, but the result seemed to be correct.

I got locally same result as for hypothesis tests, maybe it depends on the system:

$ ./python -m test test_faulthandler
Using random seed: 1615341603
0:00:00 load avg: 2.46 Run 1 test sequentially in a single process
0:00:00 load avg: 2.46 [1/1] test_faulthandler
test test_faulthandler failed -- multiple errors occurred; run in verbose mode for details
test_faulthandler failed (16 failures)

== Tests result: FAILURE ==

1 test failed:
    test_faulthandler

Total duration: 7.0 sec
Total tests: run=45 failures=16 skipped=5
Total test files: run=1/1 failed=1
Result: FAILURE

Sorry, but your patch clearly triggers CI failures.

@skirpichev skirpichev closed this Sep 20, 2024
@skirpichev skirpichev removed their request for review September 20, 2024 08:21
@rruuaanng rruuaanng deleted the 124265 branch September 20, 2024 08:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants