Skip to content

stacktrace dumping shell logic is causing test flakes #9685

Closed

Description

@bparees

the stack trace dumping logic is actually causing excess evaluations of commands, which means logic that loops while expect a certain result never sees that result.

in this case, we loop trying to create a key. we expect to see "key created" come back, once the app is available and responsive.

Instead, during one of the failed POST attempts (because the app isn't up yet) the stack trace logic gets invoked, and the stack trace logic itself ends up evaluating the curl command and doing another POST. That stack trace logic gets the "key created" response, which is useless.

Then the main loop continues and tries the POST again, but now it gets "key updated" and fails the test.

see bash debug output here:
https://gist.github.com/bparees/5cd82c622dbfc9e6725653102110bd64

the test logic starts in
end-to-end/core.sh:46 (POST to create key 1337)

then enters the retry loop at
hack/util.sh:283 and ends up in the stack trace logic as a result of the eval on line 284.

If you look at the gist output, you can see we enter the stack trace logic at line 11 due to an RC=7 from curl.

then line line 39-41 of the gist you can see the curl command gets evaluated (still within the stack trace handling logic) and gets the "key created" response back.

i assume the issue is faulty quoting on line 39 where the eval within the stack trace handling logic occurs.

Activity

  1. added
    kind/test-flake
    on Jul 2, 2016
  2. bparees commented on Jul 2, 2016

    @bparees
    ContributorAuthor

    @stevekuznetsov in the interest of deflaking things as rapidly as possible, my preference is to disable the stack dumping logic until this is sorted out.

  3. stevekuznetsov commented on Jul 2, 2016

    @stevekuznetsov
    Contributor

    @bparees I do not agree with your diagnosis of the issue -- the stacktrace logic does not evaluate the command itself, it does:

    set +o nounset
    local -r last_command_with_vars="$( eval "echo \"${last_command}\"" )"
    set -o nounset

    as can be seen (with accompanying explanation) here.

    If the Bash logic here is breaking, it is definitely because the trap is getting triggered and returning a different return code than that which originally triggered it -- I'll look into that, it's definitely a bug.

    I would not suggest your approach. Simply refactor your loop to do this:

    # if the eval doesn't succeed...
    if ! eval ${cmd}; then
        ...
    fi
    
    # if it does...
    if eval ${cmd}; then
        ...
    fi

    In all of the relevant cases.

  4. stevekuznetsov commented on Jul 2, 2016

    @stevekuznetsov
    Contributor

    In addition to cleaning up that Bash, the fix for this (which I won't be able to implement until I'm back on Tuesday) is to apply the following diff here:

         if [[ -z "${errexit_set}" ]]; then
             # if errexit wasn't set in the shell when the ERR signal was issued, then we can ignore the signal
             # as this is not cause for failure
    -        return 0
    +        return "${return_code}"
         fi
  5. bparees commented on Jul 2, 2016

    @bparees
    ContributorAuthor

    @stevekuznetsov there's no question that the curl command is getting invoked from within the stack trace/error handler logic, look at:
    https://gist.github.com/bparees/5cd82c622dbfc9e6725653102110bd64#file-gistfile1-txt-L39-L42

    in which we are clearly within the error handler and the curl command is clearly evaluated because we see the result of "key created" which is the expected (and non-reentrant) return value from the curl command.

    we came into the error handling because the original curl command got an RC=7 (could not connect) which is expected since we are looping to wait for the app to be up. Then within the error handling, the curl command gets invoked again, which it should not.

  6. stevekuznetsov commented on Jul 2, 2016

    @stevekuznetsov
    Contributor

    @bparees Oh, I see... the tricksy embedded subshell. Sorry, you are correct. I had not looked far enough into the gist.

    On some further thought, my fix above might not be applicable since it would cause a recursive error condition, and since it wouldn't change the eval, anyway. I think it's most likely impossible to have an undetectable trap (i.e. one that returns with the same code as the command that triggered it) in this case, I think that's OK for two reasons, nevermind, Bash has magic that makes this possible, but in either case :

    1. our use of set +e blocks is bad form and should be refactored ASAP
    2. the eval && [[ $? = 0]] code is also not idiomatic and fragile, whereas if ! eval ${cmd}; then or while ! eval ${cmd}; do are idiomatic and more robust

    However, I don't think there's a good argument for arguing that the stacktrace should ever actually evaluate any code, and against the use of a construct like [[ "$( some_cmd )" = "expected_output" ]]. Therefore, I think the eval to determine variables in the failing command feature should be removed or reconsidered. @smarterclayton you won't appreciate that but it's unfortunately necessary.

  7. smarterclayton commented on Jul 2, 2016

    @smarterclayton
    Contributor
  8. stevekuznetsov commented on Jul 2, 2016

    @stevekuznetsov
    Contributor

    I think it's most likely impossible to have an undetectable trap (i.e. one that returns with the same code as the command that triggered it) in this case

    To clarify this statement:

    • In the current scenario, we have the stacktrace as code that lives trapped on the ERR signal. ERR is a pseudo-signal that Bash generates. A trap on ERR is fired in the same set of cases where set -o errexit would trigger an EXIT pseudo-signal and exit behavior.
    • Since our code has blocks that do and blocks that do not use set -o errexit, we only honor the ERR trap if set -o errexit is on, as we only consider an error a failure in those environments.
    • Since any code executing in the trap ERR block will have a return code, it will overwrite the previous $?.
    • Since we have something sitting in trap ERR, we can't return in our error trap with any non-zero code. as it will re-trigger the trap infinitely until Bash segfaults by overflowing it's stack.
    • Therefore, there cannot exist an ERR trap that does not change the exit code. This isn't terrible since Bash seemingly has some magic by which the original error code is persisted through the trap (as we can see here [maybe @Miciah can explain what, exactly, happens]).

    In the future, as we move away from blocks that use set +o errexit or set +e, we can actually move to never need set -o errexit as our trap ERR block will always be called when previously the errexit would have triggered an exit. In the current state, we're actually not getting errexit to send the EXIT signal anyway, I don't think, since a complete run of os::log::stacktrace will literally exit at the end and trigger the EXIT signal that way.

  9. bparees commented on Jul 2, 2016

    @bparees
    ContributorAuthor

    as part of fixing this, please re-enable the stack tracing behavior that's being disabled in: #9676

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area/testskind/test-flakepriority/P2

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions