Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Excessive CPU usage, fix --solo #974

Merged
merged 3 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
23 changes: 14 additions & 9 deletions rootfs/usr/local/sbin/shell-monitor
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,21 @@ 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.
# Super efficient, so run as often as needed.
shell_has_exited() {
local pid

trap 'set +x' EXIT
set -x

# If there are no shells left, then they have all exited.
[[ "${#wrapper_pids[@]}" -eq 0 ]] && return 0

Expand All @@ -31,25 +36,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 +67,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 +92,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
Loading