Repository navigation
fix(gateway): retire a timed-out child session; Modal VM log forwarder follows every container (FD-7164) #992
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
base: main
Are you sure you want to change the base?
Changes from all commits
b907f8a
8b4725b
2681311
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -83,38 +83,47 @@ | |
| # The sandbox MAIN process. Two decoupled parts, both for robustness: | ||
| # 1. A best-effort log forwarder run as a *child* of this process — NOT a separate `exec`. | ||
| # Modal reaps an exec'd process tree when the exec call returns (that killed an earlier | ||
| # nohup'd forwarder); a child of the everlasting main process is not. It polls | ||
| # `docker ps` and, per container, streams `docker logs -f` (name-prefixed, `sed -u` | ||
| # unbuffered) to THIS process's stdout. Because the forwarder is a child it inherits | ||
| # that stdout, which Modal captures into the dashboard — so logs reach Modal with no | ||
| # intermediate file. There is therefore no second on-disk copy: Docker's own json-file | ||
| # logs (what `docker logs` reads) are the single on-disk copy, and they are size-capped | ||
| # at dockerd start (see _ensure_dockerd_running) so they can't fill the disk. Uses | ||
| # `docker ps -aq` (not `-q`) so containers that have already exited are also followed: | ||
| # `docker logs` persists after exit, and `docker logs -f` on an exited container just | ||
| # dumps its logs and returns. That keeps startup/crash logs of a short-lived or failed | ||
| # service (often the ones you need) instead of dropping them. New containers (compose | ||
| # services / the agent-api container) are picked up by the poll loop as they appear. | ||
| # nohup'd forwarder); a child of the everlasting main process is not. Per container it | ||
| # streams `docker logs -f` (name-prefixed, `sed -u` unbuffered) to THIS process's stdout, | ||
| # which the child inherits and Modal captures into the dashboard — so logs reach Modal with | ||
| # no intermediate file. Docker's own json-file logs (what `docker logs` reads) are the single | ||
| # on-disk copy, size-capped at dockerd start (see _ensure_dockerd_running). | ||
| # It waits for dockerd, follows the containers that already exist (`docker ps -a`: an | ||
| # exited one's logs are dumped once, which keeps a failed service's crash output), then | ||
| # follows every container *start* from `docker events`, with `--since` the start time. A | ||
| # follower ends when its container stops, so following on each start is what catches: | ||
| # compose creating every service before starting it, `restart:` policies, recreated | ||
| # containers, and short-lived ones (their logs outlive them). The earlier form polled | ||
| # `docker ps -a` and followed each container id once: at boot compose had created but not | ||
| # yet started all but the first service, those followers exited at once, and only the | ||
| # servicedb's logs ever reached Modal (FD-7164). A start event in the same second as the | ||
| # first scan, for a container the scan already followed, is skipped so it isn't doubled. | ||
| # 2. A keepalive that never exits, so liveness is fully decoupled from logging: if the | ||
| # forwarder child dies, this loop keeps the sandbox alive — equivalent to the previous | ||
| # `sleep infinity` main process. Logging can never take the env down. | ||
| _MAIN_SCRIPT = r''' | ||
| ( | ||
| seen="" | ||
| until docker info >/dev/null 2>&1; do sleep 1; done | ||
| follow() { | ||
| sh -c 'docker logs -f --timestamps "$@" 2>&1 | sed -u "s/^/[$0] /"' "$@" </dev/null & | ||
| } | ||
| since=$(date +%s) | ||
| existing=$(echo $(timeout 20 docker ps -aq --no-trunc 2>/dev/null)) | ||
| for c in $existing; do | ||
| n=$(timeout 10 docker inspect -f '{{.Name}}' "$c" 2>/dev/null | sed 's#^/##') | ||
|
Comment on lines
+111
to
+113
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new log forwarder embeds its Rule Used: Store magic numbers as class or instance variables with descriptive names rather than using them inline in the code. (source) Learned From Prompt To Fix With AIThis is a comment left during a code review.
Path: src/agent_env/providers/sandbox_providers/modal_vm_sandbox.py
Line: 111-113
Comment:
**Log limits lack names**
The new log forwarder embeds its `20`-second scan timeout and `10`-second inspect timeout directly in shell commands. The repository requires magic numbers to be stored as class or instance variables with descriptive names. Give these limits named settings before merging so maintainers can find and understand them without reading the shell commands.
**Rule Used:** Store magic numbers as class or instance variables with descriptive names rather than using them inline in the code. ([source](https://app.greptile.com/-/switch-tenant?tenant=scale-ai&next=%2Fcustom-context%3Fmemory%3D002e0051-41ad-46c1-9098-47433c580150))
**Learned From**
[scaleapi/scaleapi#126388](https://github.com/scaleapi/scaleapi/pull/126388)
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||
| follow "${n:-$c}" --tail 200 "$c" | ||
| done | ||
| while true; do | ||
| for c in $(docker ps -aq 2>/dev/null); do | ||
| case " $seen " in | ||
| *" $c "*) ;; | ||
| *) | ||
| seen="$seen $c" | ||
| n=$(docker inspect -f '{{.Name}}' "$c" 2>/dev/null | sed 's#^/##') | ||
| ( docker logs -f --tail 200 --timestamps "$c" 2>&1 | sed -u "s/^/[$n] /" ) & | ||
| ;; | ||
| esac | ||
| done | ||
| sleep 3 | ||
| docker events --since "$since" --filter type=container --filter event=start \ | ||
| --format '{{.Actor.ID}} {{.Time}} {{index .Actor.Attributes "name"}}' 2>/dev/null | | ||
| while read -r c t n; do | ||
| if [ "$t" -le "$since" ]; then case " $existing " in *" $c "*) continue ;; esac; fi | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Track whether a follower is still running rather than skipping every scanned ID. Add a same-second test; the current test puts start times five seconds ahead. Prompt To Fix With AIThis is a comment left during a code review.
Path: src/agent_env/providers/sandbox_providers/modal_vm_sandbox.py
Line: 120
Comment:
**Same-second starts lose logs**
`existing` includes containers that have been created but not started. If the first log follower exits before one starts, and the start happens in the same second as `since`, this check discards its only start event. That service's logs then stay missing from Modal until another restart.
Track whether a follower is still running rather than skipping every scanned ID. Add a same-second test; the current test puts start times five seconds ahead.
---
For each issue above, determine whether it is valid and should be fixed. If so, fix it directly. |
||
| follow "${n:-$c}" --since "$t" "$c" | ||
| done | ||
| since=$(date +%s); existing="" | ||
| sleep 1 | ||
| done | ||
| ) & | ||
| ) </dev/null & | ||
| while true; do sleep 3600; done | ||
| ''' | ||
| _KEEPALIVE_CMD = ("sh", "-c", _MAIN_SCRIPT) | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.