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

Add a delay when testing tree monitor updates #1187

Merged
merged 2 commits into from
Feb 26, 2025

Conversation

edan-bainglass
Copy link
Member

The test is time-dependent due to race conditions. This PR adds a small delay to ensure the main thread spends enough time adding branches to ensure that the monitoring thread will also attempt it.

@edan-bainglass
Copy link
Member Author

@superstar54 can you please review this? 🙏


def delayed_add_branches():
self.tree.trunk._add_branches_recursive()
time.sleep(0.5)
Copy link
Member

Choose a reason for hiding this comment

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

I feel that the time delay after the _add_branches_recursive() will not work as expected. I think it should inside _add_branches_recursive, but I am not sure. Could you explain a bit of detail on the race condition? Especially when and how the race condition happened, on which variable?

Copy link
Member Author

Choose a reason for hiding this comment

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

When a WorkChainTreeNode needs to add branches (sub-workchains, calculations), it calls a wrapper method add_branches, which wraps the recursive branch adding method with the new adding_branches flag. This flag is meant to be observed by any other thread that tries to do the same. This happens when the user expands a branch for the first time. This triggers adding sub branches. If during this action the monitor thread runs an update, since the branch is expanded, the update will also trigger adding branches. But now the monitor thread should see the flag is True and will skip this step. Before, without the flag in place, there was a chance that the monitor thread would process a child node before the main thread added that node's pk to the pks cache, which is checked to avoid duplication. This is how we ended up with duplicate branches. But the flag should prevent this. In the test, I replace this guarding wrapper method with a direct call to the recursive method, then sleep for a bit.

You may be correct. It is during the recursive call that a node may be handled twice without the presence of the flag. But here I sleep after the recursion is complete. This may not capture the event consistently. However, the test passes, which suggest we end up with a duplication as expected without the flag, and no duplication when the flag is there.

Copy link
Member

@superstar54 superstar54 Feb 26, 2025

Choose a reason for hiding this comment

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

However, the test passes

I remember you mentioned the test is not stable previously, some time passed while others time failed. I think the issue is still there in this PR.

@edan-bainglass
Copy link
Member Author

@superstar54 this iteration should be closer to what you have in mind. The idea is to delay WHILE adding a child branch, right before adding its pk to the cache. This in principle allows the monitor thread to add the child BEFORE the pk is added, so the monitor thread will think it should be added.

@edan-bainglass edan-bainglass merged commit 7cdec52 into aiidalab:main Feb 26, 2025
5 checks passed
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.

2 participants