Переглянути джерело

refactor: 缓存处理添加超时限制

wytfy 3 місяців тому
батько
коміт
30ae0f3167
1 змінених файлів з 17 додано та 4 видалено
  1. 17 4
      hpc-security-hardener.sh

+ 17 - 4
hpc-security-hardener.sh

@@ -73,11 +73,24 @@ fix_dirty_frag() {
     done
 }
 
-# --- Function: Memory Sanitization ---
+# --- Function: Memory Sanitization with Timeout ---
 clear_caches() {
-    log_info "Dropping system caches to ensure memory purity..."
-    sync
-    echo 3 > /proc/sys/vm/drop_caches
+    log_info "Attempting to drop system caches safely..."
+
+    # 1. First, try to sync dirty buffers to disk with a timeout.
+    # If sync hangs, it indicates heavy I/O or storage issues.
+    if ! timeout 15s sync; then
+        log_warn "System sync timed out. There might be heavy I/O load. Skipping drop_caches."
+        return 0
+    fi
+
+    # 2. Write to drop_caches with a timeout.
+    # We use a subshell to prevent the main script from hanging if the kernel is unresponsive.
+    if ! timeout 5s sh -c "echo 3 > /proc/sys/vm/drop_caches" 2>/dev/null; then
+        log_warn "drop_caches operation timed out. Kernel is likely busy reclaiming Slabs."
+    else
+        log_info "Caches dropped successfully."
+    fi
 }
 
 # --- Main Execution ---