[Bug]: Possible race condition when paper in async mode
See original GitHub issueWhat happened?
I’m hitting a bug in which the paper is trying to sort elements that were recently removed from the graph.
The code in which I think the bug is located:
sortViewsExact: function() {
// Run insertion sort algorithm in order to efficiently sort DOM elements according to their
// associated model `z` attribute.
var $cells = $(this.cells).children('[model-id]');
var cells = this.model.get('cells');
sortElements($cells, function(a, b) {
var cellA = cells.get(a.getAttribute('model-id'));
var cellB = cells.get(b.getAttribute('model-id'));
var zA = cellA.attributes.z || 0; <--- "cellA" will be undefined
var zB = cellB.attributes.z || 0;
return (zA === zB) ? 0 : (zA < zB) ? -1 : 1;
});
},
This is caused by the fact that $cells
contains the elements that are still in the DOM, while cells
contains the elements that are in the graph. When the paper is in async
mode, a race condition might happen in such a way that a given element can be removed from the graph and this function can be triggered before the element has been removed form the DOM.
This will lead to the sortElements
function trying to sort elements that don’t exist.
Repro example: https://codesandbox.io/s/jointjs-possible-race-condition-embed-remove-link-tofront-pttdim
Proposed solution:
var $cells = $(this.cells).children('[model-id]');
var cells = this.model.get('cells');
+ var ids = cells.map(c => c.id);
+ $cells = $cells.filter((_, c) => ids.includes(c.getAttribute("model-id")))
sortElements($cells, function(a, b) {
Version
3.5.5
What browsers are you seeing the problem on?
Chrome
What operating system are you seeing the problem on?
Mac
Issue Analytics
- State:
- Created a year ago
- Comments:6 (6 by maintainers)
Top Results From Across the Web
Async race condition or bug? - c++ - Stack Overflow
The code that reproduces your unexpected output is BAD (the object is being deleted in one thread while still being used in another...
Read more >[Concurrency] Fixing race conditions in async/await example
I've read async/await proposal, and I'm thrilled by the possibilities. Here's what I consider the canonical example: @IBAction func ...
Read more >require-atomic-updates false positive · Issue #11899 - GitHub
Possible race condition : `someObj.bar` might be reassigned based on an outdated ... In paper rule is great and should prevent many bugs, ......
Read more >Race condition - Wikipedia
A race condition or race hazard is the condition of an electronics, software, ... It becomes a bug when one or more of...
Read more >balance between UI responsiveness and avoiding race ...
In extreme cases, you may use the producer-consumer pattern, combined with the command pattern. Basically, put user actions in a queue, ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Feel free to join this discussion 😃
This should be closed as it’s an expected behavior (sort of).