From 687e118b58479b8293c6d21ad729ed6de2e0d60c Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Thu, 10 Jul 2025 18:47:31 -0500 Subject: [PATCH] Fix ulimit script (#19095) * Fix ulimit script * still try to set soft limit --- .../rootfs/usr/local/ulimit/set_ulimit.sh | 50 +++++++++++++++---- 1 file changed, 40 insertions(+), 10 deletions(-) diff --git a/docker/main/rootfs/usr/local/ulimit/set_ulimit.sh b/docker/main/rootfs/usr/local/ulimit/set_ulimit.sh index e5d1800a0..fd172735f 100755 --- a/docker/main/rootfs/usr/local/ulimit/set_ulimit.sh +++ b/docker/main/rootfs/usr/local/ulimit/set_ulimit.sh @@ -11,17 +11,47 @@ current_hard_limit=$(ulimit -Hn) TARGET_SOFT_LIMIT=65536 TARGET_HARD_LIMIT=65536 -if [ "$current_soft_limit" -lt "$TARGET_SOFT_LIMIT" ]; then - # Attempt to set both soft and hard limits to the new value - # This requires sufficient privileges (e.g., running as root in the container) - if ulimit -Hn "$TARGET_HARD" && ulimit -Sn "$TARGET_SOFT"; then - new_soft_limit=$(ulimit -Sn) - new_hard_limit=$(ulimit -Hn) +echo "Current file limits - Soft: $current_soft_limit, Hard: $current_hard_limit" - if [ "$new_soft_limit" -ne "$TARGET_SOFT_LIMIT" ] || [ "$new_hard_limit" -ne "$TARGET_HARD_LIMIT" ]; then - echo "Warning: Nofile limits were set, but not to the exact target values." +if [ "$current_soft_limit" -lt "$TARGET_SOFT_LIMIT" ]; then + # Set hard limit first (if it needs to be increased) + hard_limit_success=true + if [ "$current_hard_limit" -lt "$TARGET_HARD_LIMIT" ]; then + if ! ulimit -Hn "$TARGET_HARD_LIMIT"; then + echo "Warning: Failed to set hard limit to $TARGET_HARD_LIMIT" + echo "Current hard limit is $current_hard_limit, will try to set soft limit anyway" + hard_limit_success=false fi - else - echo "Warning: Failed to set new nofile limits." fi + + # Determine what soft limit to use + if [ "$hard_limit_success" = true ] || [ "$current_hard_limit" -ge "$TARGET_SOFT_LIMIT" ]; then + # We can try to set the target soft limit + target_soft=$TARGET_SOFT_LIMIT + else + # Hard limit is too low, set soft limit to current hard limit + target_soft=$current_hard_limit + echo "Setting soft limit to current hard limit ($current_hard_limit) since hard limit couldn't be increased" + fi + + # Set soft limit + if ! ulimit -Sn "$target_soft"; then + echo "Warning: Failed to set soft limit to $target_soft" + exit 1 + fi + + # Verify the new limits + new_soft_limit=$(ulimit -Sn) + new_hard_limit=$(ulimit -Hn) + echo "New limits - Soft: $new_soft_limit, Hard: $new_hard_limit" + + if [ "$new_soft_limit" -eq "$TARGET_SOFT_LIMIT" ] && [ "$new_hard_limit" -eq "$TARGET_HARD_LIMIT" ]; then + echo "Successfully set file limits to target values" + elif [ "$new_soft_limit" -gt "$current_soft_limit" ]; then + echo "Successfully increased soft limit from $current_soft_limit to $new_soft_limit" + else + echo "Warning: Soft limit may not have been increased as expected" + fi +else + echo "Soft limit is already sufficient ($current_soft_limit >= $TARGET_SOFT_LIMIT)" fi \ No newline at end of file