Skip to content

Conversation

@maflcko
Copy link
Member

@maflcko maflcko commented Aug 7, 2018

Reading a block from disk does not require cs_main, so it can be removed or held shorter in functions that read from disk.

This is the first commit stolen from #11913 without rebase or otherwise touching it.

@gmaxwell
Copy link
Contributor

gmaxwell commented Aug 7, 2018

What happens if pruning removes the block while we're reading it?

@DrahtBot
Copy link
Contributor

DrahtBot commented Aug 9, 2018

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #15159 ([RPC] Update getrawtransaction by amitiuttarwar)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@promag
Copy link
Contributor

promag commented Aug 14, 2018

What happens if pruning removes the block while we're reading it?

@gmaxwell wouldn't the removal fail since the file is opened?

@luke-jr
Copy link
Member

luke-jr commented Aug 27, 2018

@promag At least on Linux ext4, opened files can be removed cleanly. It would just no longer be possible to reopen them from filename.

@promag
Copy link
Contributor

promag commented Aug 27, 2018

So in that case pruning would succeed and in that case LGTM.

If fs::remove throws then the process terminates, otherwise CBlockIndex state turns wrong — which would require restart to fix?

I'll try to reproduce.

@ryanofsky
Copy link
Contributor

It seems there is a race condition if the block is pruned after GetBlockPos is called and before the block file is actually opened:

bitcoin/src/validation.cpp

Lines 1094 to 1099 in 8f46454

{
LOCK(cs_main);
blockPos = pindex->GetBlockPos();
}
if (!ReadBlockFromDisk(block, blockPos, consensusParams))

On unix it should be sufficient to just hold onto cs_main until after the block file is opened but before it is read, but on windows I'm not sure.

if (!GetTransaction(hash, tx, Params().GetConsensus(), hash_block, true, blockindex)) {
std::string errmsg;
if (blockindex) {
LOCK(cs_main);
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: it looks like adding this lock here is just a bugfix, and not directly related to the other changes.

@DrahtBot
Copy link
Contributor

Needs rebase

@maflcko maflcko closed this Jul 11, 2019
@maflcko maflcko deleted the Mf1808-ReadBlockFromDiskCsMain branch July 11, 2019 21:10
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants