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 flaky subscription smoketests #1536

Merged
merged 6 commits into from
Jul 29, 2024
Merged

Fix flaky subscription smoketests #1536

merged 6 commits into from
Jul 29, 2024

Conversation

bfops
Copy link
Collaborator

@bfops bfops commented Jul 23, 2024

Description of Changes

Fixes #1527. From there:

See e.g. https://github.com/clockworklabs/SpacetimeDB/actions/runs/9977698168/job/27575374257?pr=1479

I believe this is due to these smoketests using the CLI to subscribe to queries. They asynchronously read errors/exceptions out from the CLI output, but the CLI call is wrapped in a synchronous exception handler, so this handler may or may not be triggered properly depending on the timing. For example, in smoketests/tests/permissions.py:

        with self.assertRaises(Exception):
            self.subscribe("SELECT * FROM Secret", n=0)

Those blocks of code would create a subscription (in a background thread), but not necessarily wait for it to complete. I believe there was a small race condition where we could exit the assertRaises block before getting the exception (see the logs in the linked ticket).

I have updated these calls to wait for the subscription to finish, which re-raises any exceptions that occur in the background thread. Note this is due to subscribe using a custom thread class - this re-raising does not happen by default in Python.

API and ABI breaking changes

No

Expected complexity level and risk

1

Testing

The CI currently passes, so it's not obviously-broken.

I was not able to reproduce the failure locally (at least, for the amount of time I was willing to wait). So, my only testing is that the tests still pass. It's probably not more flaky.

If anybody else wants to try, this was the loop I used:

true;
while [[ $? == 0 ]]; do
  python3 -m smoketests;
done;
discord-ping 'test failed';

@bfops bfops changed the title (WIP) Fix flaky smoketests Fix flaky subscription smoketests Jul 26, 2024
@@ -64,7 +64,7 @@ def test_add_then_remove_index(self):
# There are no indices, resulting in an unsupported unindexed join.
self.publish_module(name, clear = False)
with self.assertRaises(Exception):
self.subscribe(self.JOIN_QUERY, n = 0)
self.subscribe(self.JOIN_QUERY, n = 0)()
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

adding this () causes us to wait for the subscription results (which, in this case, will throw an exception).

This is the same for the lines below.

@bfops bfops marked this pull request as ready for review July 26, 2024 02:30
@bfops bfops requested a review from joshua-spacetime July 26, 2024 02:31
@joshua-spacetime
Copy link
Collaborator

I did not actually address the race condition in the subscribe function.

@bfops what's the race condition and why does this change make it so that we're no longer relying on it?

Centril
Centril previously approved these changes Jul 29, 2024
Copy link
Contributor

@Centril Centril left a comment

Choose a reason for hiding this comment

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

Woops, makes sense 👍

@bfops
Copy link
Collaborator Author

bfops commented Jul 29, 2024

I did not actually address the race condition in the subscribe function.

@bfops what's the race condition and why does this change make it so that we're no longer relying on it?

I updated the PR description with the details from the issue as well. Does the current explanation make sense?

tl;dr we were creating a subscription but not waiting for it to complete within the assertRaises block.

@bfops bfops requested review from Centril and removed request for Centril July 29, 2024 16:06
@bfops bfops dismissed Centril’s stale review July 29, 2024 16:07

Dismissing because Josh has outstanding questions and I don't want to accidentally merge without addressing those

@bfops bfops marked this pull request as draft July 29, 2024 17:15
if proc.poll() is not None:
if proc.returncode:
raise subprocess.CalledProcessError(proc.returncode, fake_args)
return lambda timeout=None: []
Copy link
Collaborator Author

@bfops bfops Jul 29, 2024

Choose a reason for hiding this comment

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

This was the source of the race condition - if the CLI process failed quickly enough, we would raise this exception synchronously in the call to subscribe. But if the process took a bit longer to fail, then the block below would be the one to raise the exception, which would only be raised to the caller when the caller actually joined the background thread and waited for the subscription to complete.

By removing this block, we now only get exceptions when we wait for the subscription result. We will never get the exception synchronously. This eliminates the race condition - we must always wait for the subscription to complete in order to see the exception.

I have updated the tests accordingly.

@bfops bfops marked this pull request as ready for review July 29, 2024 17:37
Comment on lines 247 to 249
# Note that we're returning `.join`, not `.join()`; this returns something that the caller can call in order to
# join the thread and wait for the results.
# If the caller does not invoke this returned value, the thread will just run in the background and not be awaited.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Excellent!

Comment on lines +301 to +303
# This is a custom thread class that will propagate an exception to the caller of `.join()`.
# This is required because, by default, threads do not propagate exceptions to their callers,
# even callers who have called `join`.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Excellent!

@bfops bfops enabled auto-merge July 29, 2024 18:04
@bfops bfops added this pull request to the merge queue Jul 29, 2024
Merged via the queue into master with commit e5cfb1e Jul 29, 2024
7 checks passed
@bfops bfops deleted the bfops/fix-flaky-smoketests branch July 29, 2024 21:54
bfops added a commit that referenced this pull request Aug 29, 2024
…ert the always-wait behavior of smoketest from #1536
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.

CI - Fix flaky smoketests that subscribe to queries
3 participants