Skip to content

Commit

Permalink
Fix for occasionally freezes when enable the profiler
Browse files Browse the repository at this point in the history
  • Loading branch information
ko1nksm committed Jul 20, 2019
1 parent 5eedd3a commit ea3e5d8
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 24 deletions.
26 changes: 16 additions & 10 deletions contrib/various_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,19 @@ set -eu

: "${SH:=sh}"

$SH shellspec --banner
$SH shellspec --no-banner --skip-message quiet -j 3
$SH shellspec --no-banner --skip-message quiet $($SH shellspec --list specfiles | head -n 5)
$SH shellspec --no-banner --skip-message quiet $($SH shellspec --list examples:lineno | head -n 5)
$SH shellspec --no-banner --skip-message quiet spec/general_spec.sh:40:60:80:100
$SH shellspec --no-banner --skip-message quiet --profile
$SH shellspec --syntax-check
$SH shellspec --count
$SH shellspec --task
$SH shellspec --task hello:shellspec
shellspec() {
set -- $SH shellspec --shell "$SH" "$@"
echo "\$ $@"
"$@"
}

shellspec --banner
shellspec --no-banner --skip-message quiet -j 3
shellspec --no-banner --skip-message quiet $($SH shellspec --list specfiles | head -n 5)
shellspec --no-banner --skip-message quiet $($SH shellspec --list examples:lineno | head -n 5)
shellspec --no-banner --skip-message quiet spec/general_spec.sh:40:60:80:100
shellspec --no-banner --skip-message quiet --profile
shellspec --syntax-check
shellspec --count
shellspec --task
shellspec --task hello:shellspec
4 changes: 2 additions & 2 deletions lib/core/dsl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ shellspec_abort() {

if [ "$SHELLSPEC_PROFILER" ]; then
shellspec_profile_start() {
shellspec_signal -USR1 "$SHELLSPEC_PROFILER_PID"
shellspec_signal -USR1 "$SHELLSPEC_PROFILER_PID" ||:
}
shellspec_profile_end() {
shellspec_signal -USR1 "$SHELLSPEC_PROFILER_PID"
shellspec_signal -USR1 "$SHELLSPEC_PROFILER_PID" ||:
}
else
shellspec_profile_start() { :; }
Expand Down
4 changes: 2 additions & 2 deletions lib/general.sh
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,10 @@ shellspec_chomp() {

if $SHELLSPEC_KILL -0 $$ 2>/dev/null; then
shellspec_signal() {
"$SHELLSPEC_KILL" "$1" "$2"
"$SHELLSPEC_KILL" "$1" "$2" 2>/dev/null
}
else
shellspec_signal() {
"$SHELLSPEC_KILL" -s "${1#-}" "$2"
"$SHELLSPEC_KILL" -s "${1#-}" "$2" 2>/dev/null
}
fi
2 changes: 1 addition & 1 deletion lib/libexec/reporter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# shellcheck source=lib/libexec.sh
. "${SHELLSPEC_LIB:-./lib}/libexec.sh"
use import reset_params constants sequence replace each padding trim
use import reset_params constants sequence replace each padding trim signal

count() {
count_specfiles=0 count_examples=0
Expand Down
20 changes: 17 additions & 3 deletions libexec/shellspec-profiler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,29 @@

set -eu

# Workaround. An unknown error has occurred in zsh 4.3.1, 5.0.2
#
# /shellspec/libexec/shellspec-profiler.sh:47: command not found: 123
#
# 123 is last word? of handler. zsh tries to call 123 somehow.
#shellcheck disable=SC2123
PATH='' # Avoid unexpected program execution
handler() {
eval "counter${index}=$counter"
index=$(($index + 1))
eval "counter${index}=$counter index=$(($index + 1))"
#shellcheck disable=SC2034
__=: # Forces the execution of a harmless colon
}
__() { :; }

terminate() {
i=0 start='' end=''
while [ $i -lt $index ]; do
while [ "$i" -lt "$index" ]; do
eval "start=\$counter${i} end=\${counter$(($i + 1)):-}"
[ "$end" ] || break
echo "$(($end - $start))" >> "$SHELLSPEC_PROFILER_LOG"
i=$(($i + 2))
done
finished
exit
}

Expand All @@ -36,6 +46,10 @@ echo "$pid" > "$SHELLSPEC_TMPBASE/profiler.pid"
[ "$pid" ] || exit 1

index=0 counter=0

while :; do
counter=$(($counter + 1))
done

echo "Profiler aborted" >&2
exit 1
6 changes: 5 additions & 1 deletion libexec/shellspec-reporter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,18 @@ each_line() {
}
parse_lines

healthcheck_profiler() {
signal -0 "$1" && [ ! -e "$SHELLSPEC_TMPBASE/profiler.done" ]
}

callback() { [ -e "$SHELLSPEC_TIME_LOG" ] || sleep 0; }
sequence callback 1 10
time_real=''
read_time_log "time" "$SHELLSPEC_TIME_LOG"

if [ "$SHELLSPEC_PROFILER" ] && [ "$SHELLSPEC_PROFILER_LOG" ]; then
mkdir -p "$SHELLSPEC_REPORTDIR"
sleep_wait 1000 [ ! -e "$SHELLSPEC_TMPBASE/profiler.done" ] ||:
sleep_wait healthcheck_profiler "$SHELLSPEC_PROFILER_PID" ||:
callback() {
eval "profiler_tick$1=\$2 profiler_time$1=\$3" \
"profiler_line=\$profiler_line$1 profiler_count=$(($1 + 1))"
Expand Down
16 changes: 11 additions & 5 deletions libexec/shellspec-runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ start_profiler() {

stop_profiler() {
[ "$SHELLSPEC_PROFILER_PID" ] || return 0
signal -TERM "$SHELLSPEC_PROFILER_PID" 2>/dev/null ||:
sleep_wait 1000 [ ! -e "$SHELLSPEC_TMPBASE/profiler.done" ] ||:
SHELLSPEC_PROFILER_PID=''
profiler_pid=$SHELLSPEC_PROFILER_PID && SHELLSPEC_PROFILER_PID=''
signal -TERM "$profiler_pid" 2>/dev/null ||:
sleep_wait healthcheck_profiler "$profiler_pid" ||:
}

healthcheck_profiler() {
# Workaround for bosh. Monitor profiler.done as processes can not be killed
signal -0 "$1" && [ ! -e "$SHELLSPEC_TMPBASE/profiler.done" ]
}

cleanup() {
if (trap - INT) 2>/dev/null; then trap '' INT; fi
[ "$SHELLSPEC_TMPBASE" ] || return 0
stop_profiler
tmpbase="$SHELLSPEC_TMPBASE" && SHELLSPEC_TMPBASE=''
[ -f "$SHELLSPEC_KCOV_IN_FILE" ] && rm "$SHELLSPEC_KCOV_IN_FILE"
[ "$SHELLSPEC_KEEP_TEMPDIR" ] || rmtempdir "$tmpbase"
Expand All @@ -38,7 +42,7 @@ interrupt() {
stop_profiler
reporter_pid=''
read_pid_file reporter_pid "$SHELLSPEC_TMPBASE/reporter.pid" 0
[ "$reporter_pid" ] && sleep_wait signal -0 "$reporter_pid" 2>/dev/null
[ "$reporter_pid" ] && sleep_wait signal -0 "$reporter_pid"
signal -TERM 0
cleanup
exit 130
Expand Down Expand Up @@ -122,6 +126,8 @@ start_profiler
) 3>&1 4>&2 &&:
exit_status=$?

stop_profiler

case $exit_status in
0) ;; # Running specs exit with successfully.
$SHELLSPEC_SPEC_FAILURE_CODE) ;; # Running specs exit with failure.
Expand Down

0 comments on commit ea3e5d8

Please sign in to comment.