Fix nohup commands not returning immediately with timeout wrapper #329
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix nohup commands not returning immediately with timeout wrapper
Problem
When
nohupcommands with background execution (&) are run through the timeout wrapper in Docker/Kubernetes terminals without TTY, thetimeoutcommand doesn't return immediately. This caused the requests package setup in SWE-Bench to hang for 300 seconds when starting gunicorn servers.Root Cause - Bash Job Control in Non-TTY Mode
timeout 300 /bin/bash -c 'nohup gunicorn ... > /dev/null 2>&1 &'In non-TTY mode:
timeoutcommand monitors the bash shell it launches&, bash's job control tracks the backgrounded processsetsidWhy TTY matters:
Solution - Use Subshell
(...)The fix uses bash subshells
(...)to create a subprocess that exits immediately after backgrounding:Before (insufficient):
After (correct):
Why this works:
(...)creates a subshell that is a separate processsh -cwith equivalent behaviorChanges
debug_gym/gym/envs/swe_bench.py(...)subshell wrapper for 2 nohup gunicorn commandstests/gym/terminals/test_docker.pytests/gym/terminals/test_kubernetes.pyCommit history:
setsidapproach (still had 5+ second delay due to bash job control)sh -capproach (3+ second delay, improved but still not optimal)(...)subshell + warmup commands to exclude startup overheadTesting
Added comprehensive tests verifying
(...)subshell withnohupworks in non-TTY Docker/Kubernetes exec:Test:
test_*_nohup_with_subshell_returns_immediately(nohup ... > /dev/null 2>&1 &)returns in < 2 secondsTest:
test_*_nohup_without_redirection_may_timeoutEach test:
time.time()pgreppkillImpact
sh -corsetsidTechnical Note
This approach uses bash subshells for clean process separation:
(...)- Creates subshell that exits immediately after backgroundingnohup- Ignores SIGHUP signals> /dev/null 2>&1- Closes stdout/stderr file descriptors&- Backgrounds the process within the subshellThe key insight is that subshells
(...)create separate processes that don't participate in the parent shell's job control, allowing immediate return.Related
Fixes timeout issue reported in #325 (screenshot shows the 300s timeout being triggered in Kubernetes exec without TTY)
🤖 Generated with Claude Code