From 1f587062485dc7c69e9783cdb8bb187842eafc8a Mon Sep 17 00:00:00 2001 From: Dan Engelbrecht Date: Wed, 25 Sep 2024 09:48:15 +0200 Subject: exception safety when writing block (#168) * make sure we always clear writing block from m_ActiveWriteBlocks even if we have an exception --- src/zenstore/blockstore.cpp | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/zenstore/blockstore.cpp b/src/zenstore/blockstore.cpp index 00a38c3b6..7c6677052 100644 --- a/src/zenstore/blockstore.cpp +++ b/src/zenstore/blockstore.cpp @@ -539,16 +539,15 @@ BlockStore::WriteChunk(const void* Data, uint64_t Size, uint32_t Alignment, cons Ref WriteBlock = m_WriteBlock; m_ActiveWriteBlocks.push_back(WriteBlockIndex); InsertLock.ReleaseNow(); + auto _ = MakeGuard([this, WriteBlockIndex]() { + RwLock::ExclusiveLockScope _(m_InsertLock); + m_ActiveWriteBlocks.erase(std::find(m_ActiveWriteBlocks.begin(), m_ActiveWriteBlocks.end(), WriteBlockIndex)); + }); WriteBlock->Write(Data, ChunkSize, AlignedInsertOffset); m_TotalSize.fetch_add(AlignedWriteSize, std::memory_order::relaxed); Callback({.BlockIndex = WriteBlockIndex, .Offset = AlignedInsertOffset, .Size = ChunkSize}); - - { - RwLock::ExclusiveLockScope _(m_InsertLock); - m_ActiveWriteBlocks.erase(std::find(m_ActiveWriteBlocks.begin(), m_ActiveWriteBlocks.end(), WriteBlockIndex)); - } } void @@ -615,6 +614,10 @@ BlockStore::WriteChunks(std::span Datas, uint32_t Alignment, const Wri Ref WriteBlock = m_WriteBlock; m_ActiveWriteBlocks.push_back(WriteBlockIndex); InsertLock.ReleaseNow(); + auto _ = MakeGuard([this, WriteBlockIndex]() { + RwLock::ExclusiveLockScope _(m_InsertLock); + m_ActiveWriteBlocks.erase(std::find(m_ActiveWriteBlocks.begin(), m_ActiveWriteBlocks.end(), WriteBlockIndex)); + }); { MutableMemoryView WriteBuffer(Buffer.data(), RangeSize); @@ -639,11 +642,6 @@ BlockStore::WriteChunks(std::span Datas, uint32_t Alignment, const Wri } Callback(Locations); - { - RwLock::ExclusiveLockScope _(m_InsertLock); - m_ActiveWriteBlocks.erase(std::find(m_ActiveWriteBlocks.begin(), m_ActiveWriteBlocks.end(), WriteBlockIndex)); - } - Offset += Count; } } -- cgit v1.2.3