Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active August 2, 2022 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scivision/cfd6a8613c3de9b0d9393b670b841071 to your computer and use it in GitHub Desktop.
Save scivision/cfd6a8613c3de9b0d9393b670b841071 to your computer and use it in GitHub Desktop.
Detect if CTest already running, to avoid unwanted concurrent CTest/CDash runs
function(ctest_once_only)
# check if CTest already running, error if so
if(WIN32)
find_program(tasklist NAMES tasklist REQUIRED)
execute_process(COMMAND ${tasklist} /fi "Imagename eq ctest.exe" /fo csv
TIMEOUT 10
RESULT_VARIABLE ret
OUTPUT_VARIABLE out
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(NOT ret EQUAL 0)
message(FATAL_ERROR "Could not check if CTest already running")
endif()
string(REGEX MATCHALL "(ctest.exe)" mat "${out}")
list(LENGTH mat L)
if(NOT L EQUAL 1)
message(FATAL_ERROR "CTest already running ${L} times, not starting again.
${mat}")
endif()
else()
find_program(pgrep NAMES pgrep REQUIRED)
execute_process(COMMAND ${pgrep} ctest
TIMEOUT 10
RESULT_VARIABLE ret
OUTPUT_VARIABLE out
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(ret EQUAL 1)
message(VERBOSE "No CTest found running, proceeding.")
return()
elseif(NOT ret EQUAL 0)
message(FATAL_ERROR "Could not check if CTest already running")
endif()
string(REGEX MATCHALL "([0-9]+)" mat "${out}")
list(LENGTH mat L)
if(NOT L EQUAL 1)
message(FATAL_ERROR "CTest already running ${L} times, not starting again.
${mat}")
endif()
endif()
endfunction(ctest_once_only)
@scivision
Copy link
Author

Uses regex to count number of times ctest is running. We expect only one instance.

This is used in CTest CDash mode to avoid unexpectedly long-running scripts to "pile up" on subsequent unwanted concurrent CTest runs, eventually filling swap memory and crashing the computer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment