Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix infinite wait when invoking "bats -j5" instead of "bats -j 5" #657

Merged
merged 3 commits into from Oct 5, 2022

Conversation

marc-hb
Copy link
Contributor

@marc-hb marc-hb commented Sep 25, 2022

bats's option parser translates "-j5" to "-j -5". This caused a negative number of "slots" (threads) to be available and the test to silently hang forever. Not nice.

Add a $num_jobs sanity check to fail immediately like this:

Invalid number of jobs: -5
tests.bats
   bats warning: Executed 0 instead of expected 3 tests

3 tests, 0 failures, 3 not run

Non-numeric arguments (e.g.: -jf) evaluate to zero which also fails the check.

Signed-off-by: Marc Herbert marc.herbert@intel.com

bats's option parser translates "-j5" to "-j -5". This caused a negative
number of "slots" (threads) to be available and the test to silently
hang forever. Not nice.

Add a $num_jobs sanity check to fail immediately like this:

```
Invalid number of jobs: -5
tests.bats
   bats warning: Executed 0 instead of expected 3 tests

3 tests, 0 failures, 3 not run
```

Non-numeric arguments (e.g.: -jf) evaluate to zero which also fails the
check.

Signed-off-by: Marc Herbert <marc.herbert@intel.com>
@marc-hb marc-hb marked this pull request as ready for review September 25, 2022 06:57
@marc-hb marc-hb requested a review from a team as a code owner September 25, 2022 06:57
@martin-schulze-vireso
Copy link
Member

Thanks fior you report and suggested fix. This is a problem with the short flag unpacker and will cause trouble with other value arguments like -f as well. I believe we should also fix this for good.

I can take care of that. Meanwhile, can you please add a test for your fix?

@marc-hb
Copy link
Contributor Author

marc-hb commented Sep 25, 2022

This is a problem with the short flag unpacker and will cause trouble with other value arguments like -f as well. I believe we should also fix this for good.

Makes sense, indeed I thought about looking at the options parser. Then I remembered option parsers are always a complicated and ambiguous mess no matter how hard you try whereas I found this quick sanity check was dead simple to add (once I root-caused the issue). Also, I think a little bit of "defensive programming" is always good when infinite loops are involved!

I can take care of that.

Good! :-)

Meanwhile, can you please add a test for your fix?

Depends how long it would take, BATS is completely new to me and I don't have a lot of spare time... Any starting point, similar test you can point me at?

@martin-schulze-vireso martin-schulze-vireso merged commit e3bd116 into bats-core:master Oct 5, 2022
@marc-hb marc-hb deleted the fix-infinite-j branch October 5, 2022 23:14
Copy link
Contributor Author

@marc-hb marc-hb 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 test! Post-merge review...


@test "Short form typo does not run endlessly" {
unset BATS_NO_PARALLELIZE_ACROSS_FILES
run bats -j2 "$FIXTURE_ROOT/../bats/passing.bats"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you really want to test the check that was added in this PR then I think it should be:

Suggested change
run bats -j2 "$FIXTURE_ROOT/../bats/passing.bats"
run bats -j -2 "$FIXTURE_ROOT/../bats/passing.bats"

Otherwise this test will switch to covering a completely different part of the code the moment you refactor the options parser.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Additional test submitted in #693

marc-hb added a commit to marc-hb/bats-core that referenced this pull request Jan 24, 2023
Before commit 48b0d26, -j2 and -j -2 both triggered an infinite
loop. That commit fixed the infinite loop but -j2 and -j -2 are still
parsed the same. In the corresponding code review bats-core#657 Martin expressed
an intent to change the parser and parse these differently in the
future.

In commit 0ec3619 Martin added a "no infinite loop" test for
"-j2". However this test will stop testing negative arguments when the
parser changes (the test will also have to be adjusted when the parser
changes).

Add one additional test with a "-j -3" argument that will test a
negative argument forever.

Signed-off-by: Marc Herbert <marc.herbert@intel.com>
martin-schulze-vireso pushed a commit to marc-hb/bats-core that referenced this pull request Mar 4, 2023
Before commit 48b0d26, -j2 and -j -2 both triggered an infinite
loop. That commit fixed the infinite loop but -j2 and -j -2 are still
parsed the same. In the corresponding code review bats-core#657 Martin expressed
an intent to change the parser and parse these differently in the
future.

In commit 0ec3619 Martin added a "no infinite loop" test for
"-j2". However this test will stop testing negative arguments when the
parser changes (the test will also have to be adjusted when the parser
changes).

Add one additional test with a "-j -3" argument that will test a
negative argument forever.

Signed-off-by: Marc Herbert <marc.herbert@intel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants