question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug]: Possible race condition when paper in async mode

See original GitHub issue

What 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:closed
  • Created a year ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
kumilinguscommented, Oct 10, 2022

Feel free to join this discussion 😃

0reactions
alexandernstcommented, Oct 11, 2022

This should be closed as it’s an expected behavior (sort of).

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found