-
Notifications
You must be signed in to change notification settings - Fork 513
fix: Avoid use_state race conditions. Remove key argument to use_state
#868
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
Conversation
Remove key argument from use_state and hardcode it instead. Add unit tests for race conditions. Add lock when accessing the cache or kvs to prevent race conditions.
It is pretty uggly without Barrier.
|
the PR title should be informative about what the change does, "align with JS version" is not gonna tell much to our users, they don't know the JS version |
|
May not be related, but I observed flaky behavior in state handling here |
I will check it. It is supposed to be autosaved when specific event happens. It is supposed to happen every 50ms, so 1s seemed like a long enough reserve. Maybe there is some hard block in some cases and no amount of reserve time is enough? |
use_state race conditions. Remove key argument to use_state.
Mantisus
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
janbuchar
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems legit, but I want to re-read the tests after my comments are resolved.
| assert await autosaved_within_deadline(key=key_name, expected_value={'hello': 'new_world'}) | ||
|
|
||
|
|
||
| async def test_get_auto_saved_value_auto_save_race_conditions(key_value_store: KeyValueStore) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this reliably reproduce the error if you remove the lock?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried stress testing it 1000x times and it failed with old implementation all the time. But I acknowledge that it is not guaranteed and in theory it can pass. Same as in reality if such delay in kvs access occurs, it will most likely end in race condition, but in theoretical case it does not have to.
I did try coming up with test without such weakness, but I failed to find better approach.
This test should not have any false positives and should not be flaky, but has theoretical chance for false negative. In CI we run the same test for various Python implementations which makes already purely theoretical chance completely negligible.
use_state race conditions. Remove key argument to use_state.use_state race conditions. Remove key argument to use_state
Co-authored-by: Vlada Dusek <[email protected]>
This comment was marked as outdated.
This comment was marked as outdated.
janbuchar
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Description
Align Python implementation of
use_statewirth JSAdd lock when accessing the cache or kvs to prevent race conditions.
Add unit tests for race conditions.
Breaking
Remove key argument from
use_stateand hard code it toBasicCrawler.CRAWLEE_STATE_KEY.(Change in experimental feature is not marked as hard breaking with
!.)Issues