@@ -116,6 +116,7 @@ class IpcMessageObjectImpl final : public IpcObject
116116 {
117117 int32_t ownerPid;
118118 int32_t ownerId;
119+ std::atomic_uint8_t alive;
119120#ifdef IPC_MESSAGE_USE_SHARED_SIGNAL
120121 IpcSharedSignal receiverSignal;
121122 IpcSharedSignal senderSignal;
@@ -140,6 +141,7 @@ class IpcMessageObjectImpl final : public IpcObject
140141 {
141142 header->ownerPid = (int ) getpid ();
142143 header->ownerId = ++IPC_MESSAGE_COUNTER;
144+ header->alive = 1 ;
143145
144146#ifdef IPC_MESSAGE_USE_SHARED_SIGNAL
145147 new (&header->receiverSignal ) IpcSharedSignal ();
@@ -320,6 +322,9 @@ inline void IpcMessageReceiver<Message>::disconnect()
320322 {
321323 disconnected = true ;
322324 std::lock_guard mutexLock (mutex);
325+
326+ const auto header = ipc.sharedMemory .getHeader ();
327+ header->alive = 0 ;
323328 }
324329}
325330
@@ -347,6 +352,7 @@ inline std::optional<Message> IpcMessageReceiver<Message>::receive(std::function
347352 }
348353
349354 ipc.receiverSignal ->reset ();
355+ header->receiverFlag .store (0 , std::memory_order_release);
350356
351357 std::optional<Message> messageOpt;
352358
@@ -375,10 +381,7 @@ inline std::optional<Message> IpcMessageReceiver<Message>::receive(std::function
375381 memcpy (span.data (), header->messageBuffer , span.size ());
376382 }
377383
378- header->receiverFlag .store (0 , std::memory_order_release);
379-
380384 ipc.senderSignal ->signal ();
381-
382385 header->senderFlag .store (1 , std::memory_order_release);
383386
384387 return messageOpt;
@@ -430,6 +433,9 @@ inline bool IpcMessageSender<Message>::send(const Message& message, std::functio
430433
431434 while (!guard.tryLock (IPC_MESSAGE_TIMEOUT))
432435 {
436+ if (!header->alive .load (std::memory_order_relaxed))
437+ disconnected = true ;
438+
433439 if (disconnected)
434440 return false ;
435441
@@ -458,14 +464,16 @@ inline bool IpcMessageSender<Message>::send(const Message& message, std::functio
458464 memcpy (header->messageBuffer , span.data (), span.size ());
459465 }
460466
461- header->receiverFlag .store (1 , std::memory_order_release);
462-
463467 ipc.receiverSignal ->signal ();
468+ header->receiverFlag .store (1 , std::memory_order_release);
464469
465470 while (header->senderFlag .load (std::memory_order_acquire) == 0 )
466471 {
467472 if (!ipc.senderSignal ->wait (IPC_MESSAGE_TIMEOUT))
468473 {
474+ if (!header->alive .load (std::memory_order_relaxed))
475+ disconnected = true ;
476+
469477 if (disconnected)
470478 return false ;
471479
@@ -475,7 +483,6 @@ inline bool IpcMessageSender<Message>::send(const Message& message, std::functio
475483 }
476484
477485 ipc.senderSignal ->reset ();
478-
479486 header->senderFlag .store (0 , std::memory_order_release);
480487
481488 return true ;
0 commit comments