Skip to content
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

Fix potential deadlock in the table manager #5472

Conversation

MasslessParticle
Copy link
Contributor

@MasslessParticle MasslessParticle commented Feb 24, 2022

I was looking at how the tablemanager worked and noticed that getOrCreateTable had a deadlock. I've included a test that illustrates the issue and fixed it.

@MasslessParticle MasslessParticle requested a review from a team as a code owner February 24, 2022 18:49
}

table = NewTable(tableName, filepath.Join(tm.cfg.CacheDir, tableName), tm.indexStorageClient, tm.boltIndexClient, tm.metrics)
tm.tables[tableName] = table
Copy link
Contributor

Choose a reason for hiding this comment

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

don't you need to write lock here for this ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I missed that we're using RLock at the top. I've changed it to Lock

tm.tablesMtx.RLock()
defer tm.tablesMtx.RUnlock()
Copy link
Member

@owen-d owen-d Feb 25, 2022

Choose a reason for hiding this comment

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

I think we're fixing a deadlock and introducing a new one. We use RLock first as a lower-cost way to check if the table exists, but must RUnlock it after (the previous PR did this). We cannot defer it here because we need to Lock it later if the table wasn't found. I think we can introduce a 1 line fix:defer tm.tablesMtx.Unlock() immediately after the write lock is acquired, which will ensure we release the write lock correctly in all cases it's acquired.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I missed the Rlock vs. Lock

The latest commit just defers tm.tablesMtx.Unlock() after it's created.

Copy link
Contributor

@sandeepsukhani sandeepsukhani left a comment

Choose a reason for hiding this comment

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

Good catch! Thanks for fixing it!

})

t.Run("it doesn't deadlock when table create fails", func(t *testing.T) {
tempDir := os.TempDir()
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a nit, lets use t.TempDir like other places.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry, I pushed this change and resolved the merge conflict because I wanted to cut a new release with this fix.

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

Successfully merging this pull request may close these issues.

None yet

4 participants