Skip to content

Commit

Permalink
Fix Excessive CPU usage, fix --solo (#974)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuru authored Feb 26, 2025
1 parent 03448f4 commit 3cb2146
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
1 change: 1 addition & 0 deletions rootfs/templates/wrapper-body.sh
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ function use() {
)

if [ "$ONE_SHELL" = "true" ]; then
[ -t 0 ] && DOCKER_EXEC_ARGS+=(-it)
DOCKER_NAME="${DOCKER_NAME}-$(date +%d%H%M%S)"
echo "# Starting single shell ${DOCKER_NAME} session from ${DOCKER_IMAGE}"
echo "# Exposing port ${GEODESIC_PORT}"
Expand Down
20 changes: 11 additions & 9 deletions rootfs/usr/local/sbin/shell-monitor
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ wrapper_pids=()

# Function to count active shell sessions launched by wrapper
# Expensive, so run sparingly. Only needed to find new shells when they launch.
count_shells() {
shells_are_running() {
# As a side effect, update the list of running shells
wrapper_pids=($(list-wrapper-shells))
echo "${#wrapper_pids[@]}"
# Return true if there are any shells running
[[ "${#wrapper_pids[@]}" -gt 0 ]]
}

# Function to check if any registered shell has exited.
Expand All @@ -31,25 +33,25 @@ shell_has_exited() {
# Function to kill all active shell sessions launched by wrapper.
# This is the shutdown procedure, so we do not care about hogging the CPU.
kill_shells() {
for pid in $(list_wrapper_shells); do
for pid in $(list-wrapper-shells); do
kill -HUP $pid
done

for i in {1..4}; do
[ $(count_shells) -eq 0 ] && return 0
shells_are_running || return 0
sleep 1
done

for pid in $(list_wrapper_shells); do
for pid in $(list-wrapper-shells); do
kill -TERM $pid
done

for i in {1..3}; do
[ $(count_shells) -eq 0 ] && return 0
shells_are_running || return 0
sleep 1
done

for pid in $(list_wrapper_shells); do
for pid in $(list-wrapper-shells); do
kill -KILL $pid
done

Expand All @@ -62,7 +64,7 @@ trap 'kill_shells; exit $?' TERM HUP INT QUIT EXIT
# Since we are waiting for something to happen, we can afford burn
# up some CPU in order to be more responsive.
i=0
while [ $(count_shells) -eq 0 ]; do
while ! shells_are_running; do
sleep 0.5
i=$((i + 1))
if [ $i -ge 120 ]; then
Expand All @@ -87,7 +89,7 @@ while true; do
while ! shell_has_exited; do
sleep 0.67
done
[[ $(count_shells) -eq 0 ]] && break
shells_are_running || break
done

# Clean up and exit
Expand Down

0 comments on commit 3cb2146

Please sign in to comment.