Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Fix race condition due to mutated _getDBs/_putDBs #28

Merged
merged 1 commit into from
Mar 14, 2018

Conversation

federicobond
Copy link
Contributor

This fixes part of the issues described in trufflesuite/ganache#417.

The asyncFirstSeries function called inside Trie#getRaw was iterating over an array that could be mutated by the checkpoint _enterCpMode/_exitCpMode functions, causing the familiar TypeError: Cannot read property 'get' of undefined.

I replaced the mutations with reassignments.

Note that the tests written in trufflesuite/ganache#41 still fail, now both with the same error: TypeError: Cannot read property 'pop' of undefined.

@coveralls
Copy link

Coverage Status

Coverage remained the same at 81.149% when pulling ffa0c17 on federicobond:fix-typerror into d3baf12 on ethereumjs:master.

@holgerd77
Copy link
Member

Sorry, this was left aside a bit. Will have a look in the upcoming days (maybe next week).

@holgerd77
Copy link
Member

Hi @federicobond, trying to grab my head around this for review but I really have problems to understand why mutation and reassignment to self behaves differently. Will continue to dig into this but if you are around a note on this would be helpful.

There also was a second PR https://github.com/ethereumjs/merkle-patricia-tree/pull/30/files after this one fixing the same issue as here, but also touching the _putDBs function. I would also look into this if this fix also applies to this function, if you have a quick opinion on this, this also would be helpful.

Thanks!

@holgerd77
Copy link
Member

Anyhow, since you guys are discussing this for ages (just stumbled on the discussion on the bounty website and this appears to be a widely accepted patch, I will merge.

A late explanation would be nice anyhow.

Also if you see a chance to put this into some test like this from test/index.js would be nice, did you try or is this too un-reproducable due to the race condition nature?

it('should commit another nested checkpoint', function (t) {
    trie.checkpoint()
    var root
    trie.put('test', 'something else', function () {
      root = trie.root
      trie.checkpoint()
      trie.get('test', function (err, val) {
        trie.put('the feels', 'emotion', function () {
          trie.commit(function () {
            console.log(val)
            t.end()
          })
        })
      })
    })
  })

Copy link
Member

@holgerd77 holgerd77 left a comment

Choose a reason for hiding this comment

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

Will approve, see discussion.

@holgerd77 holgerd77 merged commit 049f908 into ethereumjs:master Mar 14, 2018
@holgerd77
Copy link
Member

Just released this together with another bug fix as v2.3.1 on npm: https://github.com/ethereumjs/merkle-patricia-tree/releases/tag/v2.3.1

@federicobond
Copy link
Contributor Author

federicobond commented Mar 14, 2018

trying to grab my head around this for review but I really have problems to understand why mutation and reassignment to self behaves differently

In case of mutation, an async iteration that started before may see the array in an inconsistent state. By reassigning we are letting the previous procedure keep a copy of the original list and only affect future requests.

Also if you see a chance to put this into some test like this from test/index.js would be nice, did you try or is this too un-reproducable due to the race condition nature?

I think it may be possible to write a test, but AFAIK depends on the specific event processing order and it's quite tricky to get right. I haven't attempted it though.

@federicobond federicobond deleted the fix-typerror branch March 14, 2018 21:33
@federicobond
Copy link
Contributor Author

Thank you @holgerd77!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants