Christoph Stelz 3 months ago
parent
commit
a3b6495e44
1 changed files with 35 additions and 2 deletions
  1. 35 2
      src/ipc_debug.cpp

+ 35 - 2
src/ipc_debug.cpp

@@ -30,6 +30,17 @@ std::vector<double> float_buffer;
 
 constexpr size_t INITIAL_BUFFER_SIZE = 5 * 1024 * 1024; // 5 MiB
 
+/** 
+ * Additionally to the data necessary to compare between the two processes,
+ * the client also sends a single OK_FLAG to signal that it can continue
+ * execution. If the assertion on the root process does not pass, it will enter
+ * an endless loop, thus causing the write to block on client side as well.
+ *
+ * This is useful to make sure the client stops execution as well if the
+ * assertion does not pass.
+ */
+const char OK_FLAG = 0x42;
+
 void print_backtrace();
 
 void debug_ipc_init() {
@@ -56,9 +67,9 @@ void debug_ipc_init() {
         }
 
         if (debug_ipc_is_root) {
-            debug_ipc_file = fopen(file_env, "r");
+            debug_ipc_file = fopen(file_env, "rb");
         } else {
-            debug_ipc_file = fopen(file_env, "w");
+            debug_ipc_file = fopen(file_env, "wb");
         }
 
         if (debug_ipc_file == nullptr) {
@@ -66,6 +77,8 @@ void debug_ipc_init() {
             exit(-1);
         }
 
+        setvbuf(debug_ipc_file, nullptr, _IONBF, 0);
+
 
         buffer.resize(INITIAL_BUFFER_SIZE);
     }
@@ -102,6 +115,9 @@ void debug_ipc_assert_equal(T value) {
             std::cout << "[IPCDBG] Assertion passed, value = " << value << std::endl;
 #endif
         }
+
+        // Read ok flag so that client can continue
+        char status; fread(&status, 1, 1, debug_ipc_file); assert(status == OK_FLAG);
     } else {
         size_t written = fwrite(&value, expected_size, 1, debug_ipc_file);
 
@@ -109,6 +125,11 @@ void debug_ipc_assert_equal(T value) {
             printf("[IPCDBG] Could not write enough bytes. Error: %s\n", strerror(errno));
             exit(-1);
         }
+
+        fflush(debug_ipc_file);
+        fwrite(&OK_FLAG, 1, 1, debug_ipc_file);
+        fflush(debug_ipc_file);
+
     }
 }
 
@@ -146,8 +167,14 @@ void debug_ipc_assert_equal_vector(std::vector<T> value) {
                 }
             }
         }
+
+        // Read ok flag so that client can continue
+        char status; fread(&status, 1, 1, debug_ipc_file); assert(status == OK_FLAG);
     } else {
         fwrite(value.data(), 1, array_byte_length, debug_ipc_file);
+        fflush(debug_ipc_file);
+        size_t result = fwrite(&OK_FLAG, 1, 1, debug_ipc_file);
+        printf("Result: %lu\n", result);
     }
 }
 
@@ -204,6 +231,9 @@ void debug_ipc_assert_equal_array(void *value, size_t size) {
                 }
             }
         }
+
+        // Read ok flag so that client can continue
+        char status; fread(&status, 1, 1, debug_ipc_file); assert(status == OK_FLAG);
     } else {
         size_t written = fwrite(value, 1, size, debug_ipc_file);
 
@@ -211,6 +241,9 @@ void debug_ipc_assert_equal_array(void *value, size_t size) {
             printf("[IPCDBG] Could not write enough bytes. Error: %s\n", strerror(errno));
             exit(-1);
         }
+
+        fflush(debug_ipc_file);
+        fwrite(&OK_FLAG, 1, 1, debug_ipc_file);
     }
 }