Skip to content

Commit

Permalink
[Core] Race Conditions: Protect CDataWriterSHM m_memory_file_vec
Browse files Browse the repository at this point in the history
Co-authored-by: Rex Schilasky <[email protected]>
  • Loading branch information
KerstinKeller and rex-schilasky committed Jan 18, 2024
1 parent d8632e8 commit c70d5fd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
31 changes: 25 additions & 6 deletions ecal/core/src/readwrite/shm/ecal_writer_shm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,10 @@ namespace eCAL
if (!m_created) return false;
m_created = false;

m_memory_file_vec.clear();
{
const std::lock_guard<std::mutex> lock(m_memory_file_vec_mtx);
m_memory_file_vec.clear();
}

return true;
}
Expand All @@ -101,6 +104,8 @@ namespace eCAL

bool CDataWriterSHM::SetBufferCount(size_t buffer_count_)
{
const std::lock_guard<std::mutex> lock(m_memory_file_vec_mtx);

// no need to adapt anything
if (m_memory_file_vec.size() == buffer_count_) return true;

Expand Down Expand Up @@ -160,11 +165,15 @@ namespace eCAL
ret_state |= true;
}

// adapt write index if needed
m_write_idx %= m_memory_file_vec.size();

// check size and reserve new if needed
ret_state |= m_memory_file_vec[m_write_idx]->CheckSize(attr_.len);
{
const std::lock_guard<std::mutex> lock(m_memory_file_vec_mtx);

// adapt write index if needed
m_write_idx %= m_memory_file_vec.size();

// check size and reserve new if needed
ret_state |= m_memory_file_vec[m_write_idx]->CheckSize(attr_.len);
}

return ret_state;
}
Expand All @@ -173,6 +182,9 @@ namespace eCAL
{
if (!m_created) return false;

// protect m_memory_file_vec
const std::lock_guard<std::mutex> lock(m_memory_file_vec_mtx);

// write content
const bool force_full_write(m_memory_file_vec.size() > 1);
const bool sent = m_memory_file_vec[m_write_idx]->Write(payload_, attr_, force_full_write);
Expand All @@ -188,6 +200,9 @@ namespace eCAL
{
if (!m_created) return;

// protect m_memory_file_vec
const std::lock_guard<std::mutex> lock(m_memory_file_vec_mtx);

for (auto& memory_file : m_memory_file_vec)
{
memory_file->Connect(process_id_);
Expand All @@ -201,6 +216,10 @@ namespace eCAL
{
// starting from eCAL version > 5.8.13/5.9.0 the ConnectionParameter is defined as google protobuf
eCAL::pb::ConnnectionPar connection_par;

// protect m_memory_file_vec
const std::lock_guard<std::mutex> lock(m_memory_file_vec_mtx);

for (auto& memory_file : m_memory_file_vec)
{
connection_par.mutable_layer_par_shm()->add_memory_file_list(memory_file->GetName());
Expand Down
4 changes: 4 additions & 0 deletions ecal/core/src/readwrite/shm/ecal_writer_shm.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "io/shm/ecal_memfile_sync.h"

#include <memory>
#include <mutex>
#include <string>

namespace eCAL
Expand Down Expand Up @@ -59,7 +60,10 @@ namespace eCAL
size_t m_write_idx = 0;
size_t m_buffer_count = 1;
SSyncMemoryFileAttr m_memory_file_attr = {};

std::mutex m_memory_file_vec_mtx;
std::vector<std::shared_ptr<CSyncMemoryFile>> m_memory_file_vec;

static const std::string m_memfile_base_name;
};
}

1 comment on commit c70d5fd

@FlorianReimold
Copy link
Member

Choose a reason for hiding this comment

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

Part of PR #1312

Please sign in to comment.