Chrome issue allows two tabs to acquire the lock. #1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I'm not sure that the browser test side of this PR is necessarily ready for merge, but I wanted to share it in it's current state to get a fix for this moving along.
There's a bug in fast-mutex in (at least) chrome, which allows two tabs to obtain the lock at the same time.
Chrome doesn't "commit" localStorage changes to "disk" until the current execution stack has finished. This means in the current sequence:
tab one will always read "x" as one, even though tab two has changed it.
Inside fast-mutex, this allows two tabs to acquire the lock concurrently. To handle this, we need to separate set and get by a settimeout call.
To demonstrate the issue (and fix) I have created a browser test page. (Run it with
npm run test-browser
)Open it in two tabs (I would recommend putting the tabs in separate windows side-by-side, otherwise chrome has a tendency to not call timeouts more frequently than 1/per second, which confuses things).
This test triggers synchronous runs every :00/:15/:30/:45 seconds past the minute in each tab. In each tab we acquire a lock, and increment a counter. When all tabs are finished, we should see the counter equal the number of open tabs, otherwise there's been a failure (either two tabs acquired the lock concurrently, or one tab failed to complete).
Here is an example of a run before the fix (in sha 67cc985):
With two tabs open, both acquire the lock within 1ms of each other.