Skip to content

Commit

Permalink
[#532] Add error handling in publishers data segment
Browse files Browse the repository at this point in the history
  • Loading branch information
elfenpiff committed Dec 3, 2024
1 parent 2944c27 commit cfd8dc6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
41 changes: 34 additions & 7 deletions iceoryx2/src/port/details/data_segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use iceoryx2_cal::{
SharedMemoryOpenError, ShmPointer,
},
shm_allocator::{
self, pool_allocator::PoolAllocator, AllocationStrategy, PointerOffset, SegmentId,
ShmAllocationError,
self, pool_allocator::PoolAllocator, AllocationError, AllocationStrategy, PointerOffset,
SegmentId, ShmAllocationError,
},
};

Expand Down Expand Up @@ -113,10 +113,27 @@ impl<Service: service::Service> DataSegment<Service> {
}

pub(crate) fn allocate(&self, layout: Layout) -> Result<ShmPointer, ShmAllocationError> {
let msg = "Unable to allocate memory from the data segment";
match &self.memory {
MemoryType::Static(memory) => Ok(fail!(from self, when memory.allocate(layout),
"Unable to allocate memory from the data segment.")),
MemoryType::Dynamic(memory) => Ok(memory.allocate(layout).unwrap()),
"{msg}.")),
MemoryType::Dynamic(memory) => match memory.allocate(layout) {
Ok(ptr) => Ok(ptr),
Err(ResizableShmAllocationError::ShmAllocationError(e)) => {
fail!(from self, with e,
"{msg} caused by {:?}.", e);
}
Err(ResizableShmAllocationError::MaxReallocationsReached) => {
fail!(from self,
with ShmAllocationError::AllocationError(AllocationError::OutOfMemory),
"{msg} since the maxmimum number of reallocations was reached. Try to provide initial_max_slice_len({}) as hint when creating the publisher to have a more fitting initial setup.", layout.size());
}
Err(ResizableShmAllocationError::SharedMemoryCreateError(e)) => {
fail!(from self,
with ShmAllocationError::AllocationError(AllocationError::InternalError),
"{msg} since the shared memory segment creation failed while resizing the memory due to ({:?}).", e);
}
},
}
}

Expand Down Expand Up @@ -201,11 +218,21 @@ impl<Service: service::Service> DataSegmentView<Service> {
Ok(Self { memory })
}

pub(crate) fn register_and_translate_offset(&self, offset: PointerOffset) -> usize {
pub(crate) fn register_and_translate_offset(
&self,
offset: PointerOffset,
) -> Result<usize, SharedMemoryOpenError> {
match &self.memory {
MemoryViewType::Static(memory) => offset.offset() + memory.payload_start_address(),
MemoryViewType::Static(memory) => Ok(offset.offset() + memory.payload_start_address()),
MemoryViewType::Dynamic(memory) => unsafe {
memory.register_and_translate_offset(offset).unwrap() as usize
match memory.register_and_translate_offset(offset) {
Ok(ptr) => Ok(ptr as usize),
Err(e) => {
fail!(from self, with e,
"Failed to register and translate pointer due to a failure while opening the corresponding shared memory segment ({:?}).",
e);
}
}
},
}
}
Expand Down
19 changes: 13 additions & 6 deletions iceoryx2/src/port/subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,19 @@ impl<Service: service::Service, Payload: Debug + ?Sized, UserHeader: Debug>
origin: connection.publisher_id,
};

Ok(Some((
details,
connection
.data_segment
.register_and_translate_offset(offset),
)))
let offset = match connection
.data_segment
.register_and_translate_offset(offset)
{
Ok(offset) => offset,
Err(e) => {
fail!(from self, with SubscriberReceiveError::ConnectionFailure(ConnectionFailure::UnableToMapPublishersDataSegment(e)),
"Unable to register and translate offset from publisher {:?} since the received offset {:?} could not be registered and translated.",
connection.publisher_id, offset);
}
};

Ok(Some((details, offset)))
}
},
Err(ZeroCopyReceiveError::ReceiveWouldExceedMaxBorrowValue) => {
Expand Down

0 comments on commit cfd8dc6

Please sign in to comment.