From 85d1c6367ca7f40301ca04ebe2d0e23e1c1e82ba Mon Sep 17 00:00:00 2001 From: Bhaskar Brahma Date: Fri, 4 Oct 2019 13:03:30 -0700 Subject: [PATCH 1/6] Fix for text file busy --- misc/custom-script-shim | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/misc/custom-script-shim b/misc/custom-script-shim index 697f724..22c8e5a 100755 --- a/misc/custom-script-shim +++ b/misc/custom-script-shim @@ -50,6 +50,17 @@ write_status() { fi } +check_binary_write_lock() { + lsof_output="$(lsof ${bin})" + + if [ "$?" -eq 0 ]; then + echo "${HANDLER_BIN} is open by the following processes: " + echo "${lsof_output}" + echo "attempting to kill processes with open file handles to ${HANDLER_BIN}" + lsof -t ${bin} | xargs kill -9 + fi +} + if [ "$#" -ne 1 ]; then echo "Incorrect usage." echo "Usage: $0 " @@ -69,10 +80,12 @@ if [[ "$cmd" == "enable" ]]; then # to detach from the handler process tree to avoid getting terminated # after the 15-minute extension enabling timeout. write_status + check_binary_write_lock set -x nohup "$bin" $@ & else # execute the handler process as a child process + check_binary_write_lock set -x "$bin" $@ fi From ffac03420d9e1174f699f030d12d03b01e9c3df9 Mon Sep 17 00:00:00 2001 From: Bhaskar Brahma Date: Fri, 4 Oct 2019 13:56:51 -0700 Subject: [PATCH 2/6] fixed exit behavior --- misc/custom-script-shim | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/misc/custom-script-shim b/misc/custom-script-shim index 22c8e5a..2bb3027 100755 --- a/misc/custom-script-shim +++ b/misc/custom-script-shim @@ -51,14 +51,16 @@ write_status() { } check_binary_write_lock() { + set +e # disable exit on non-zero return code lsof_output="$(lsof ${bin})" - if [ "$?" -eq 0 ]; then echo "${HANDLER_BIN} is open by the following processes: " echo "${lsof_output}" echo "attempting to kill processes with open file handles to ${HANDLER_BIN}" - lsof -t ${bin} | xargs kill -9 + # suppress output and errors in case process with file-handle is already dead and kill gets called without a process id + lsof -t ${bin} | xargs kill -9 > /dev/null 2>&1 fi + set -e # re-enable exit on non-zero return code } if [ "$#" -ne 1 ]; then From 949818e8c4b34af823be62f2c030a3b74f2467bf Mon Sep 17 00:00:00 2001 From: Bhaskar Brahma Date: Fri, 4 Oct 2019 18:23:16 -0700 Subject: [PATCH 3/6] Added wait and retries before attempting to kill process --- misc/custom-script-shim | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/misc/custom-script-shim b/misc/custom-script-shim index 2bb3027..82a213b 100755 --- a/misc/custom-script-shim +++ b/misc/custom-script-shim @@ -52,12 +52,27 @@ write_status() { check_binary_write_lock() { set +e # disable exit on non-zero return code + local retry_attempts=0 + while (( retry_attempts < 3 )); do + lsof_output="$(lsof ${bin})" + if [ "$?" -eq 0 ]; then + echo "${HANDLER_BIN} is open by the following processes: " + echo "${lsof_output}" + ((++retry_attempts)) + echo "sleeping for 30 seconds before retry, attempt ${retry_attempts} of 3" + sleep 30 + else + set -e + return 0 + fi + done + + # retries over, kill process(es) if needed lsof_output="$(lsof ${bin})" if [ "$?" -eq 0 ]; then - echo "${HANDLER_BIN} is open by the following processes: " - echo "${lsof_output}" + # If the wait timed out we need to write status file + echo "Timed out waiting for lock on ${HANDLER_BIN}" echo "attempting to kill processes with open file handles to ${HANDLER_BIN}" - # suppress output and errors in case process with file-handle is already dead and kill gets called without a process id lsof -t ${bin} | xargs kill -9 > /dev/null 2>&1 fi set -e # re-enable exit on non-zero return code From b09c9b7ba7632da7c11eb96baf1c161bc6d7826a Mon Sep 17 00:00:00 2001 From: Bhaskar Brahma Date: Mon, 7 Oct 2019 11:25:25 -0700 Subject: [PATCH 4/6] Removed code to kill the process that has open file handled to custom-script-extension --- misc/custom-script-shim | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/misc/custom-script-shim b/misc/custom-script-shim index 82a213b..4f18ab5 100755 --- a/misc/custom-script-shim +++ b/misc/custom-script-shim @@ -63,19 +63,10 @@ check_binary_write_lock() { sleep 30 else set -e - return 0 + return 0 #Success path fi done - - # retries over, kill process(es) if needed - lsof_output="$(lsof ${bin})" - if [ "$?" -eq 0 ]; then - # If the wait timed out we need to write status file - echo "Timed out waiting for lock on ${HANDLER_BIN}" - echo "attempting to kill processes with open file handles to ${HANDLER_BIN}" - lsof -t ${bin} | xargs kill -9 > /dev/null 2>&1 - fi - set -e # re-enable exit on non-zero return code + exit 1 } if [ "$#" -ne 1 ]; then From e57d26685daa849610a460a61cb572c79c4d1cbe Mon Sep 17 00:00:00 2001 From: Bhaskar Brahma Date: Mon, 7 Oct 2019 11:31:19 -0700 Subject: [PATCH 5/6] changed retry frequenc, added more error messages for failures. --- misc/custom-script-shim | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/misc/custom-script-shim b/misc/custom-script-shim index 4f18ab5..8f3ab0a 100755 --- a/misc/custom-script-shim +++ b/misc/custom-script-shim @@ -53,19 +53,22 @@ write_status() { check_binary_write_lock() { set +e # disable exit on non-zero return code local retry_attempts=0 - while (( retry_attempts < 3 )); do + while (( retry_attempts < 10 )); do lsof_output="$(lsof ${bin})" if [ "$?" -eq 0 ]; then echo "${HANDLER_BIN} is open by the following processes: " echo "${lsof_output}" ((++retry_attempts)) - echo "sleeping for 30 seconds before retry, attempt ${retry_attempts} of 3" - sleep 30 + echo "sleeping for 3 seconds before retry, attempt ${retry_attempts} of 10" + sleep 3 else set -e return 0 #Success path fi done + echo "Timed out waiting for lock on ${HANDLER_BIN}" + echo "File handle is still open by the following processes: " + echo "${lsof_output}" exit 1 } From 718f98b7b0b7616a776e1150c071e67b7e9dae4c Mon Sep 17 00:00:00 2001 From: Bhaskar Brahma Date: Mon, 7 Oct 2019 14:14:16 -0700 Subject: [PATCH 6/6] Updated extension version --- misc/manifest.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/misc/manifest.xml b/misc/manifest.xml index 7328194..2481afb 100644 --- a/misc/manifest.xml +++ b/misc/manifest.xml @@ -2,7 +2,7 @@ Microsoft.Azure.Extensions CustomScript - 2.1.0 + 2.1.1 VmRole