Widen pexpect close/terminate delay to avoid free-threaded teardown race - #354
Merged
Merged
Conversation
…own race
ptyprocess's close() sleeps delayafterclose (default 0.1s) then checks
isalive() before raising ExceptionPexpect("Could not terminate the
child"). Under the free-threaded (no-GIL) Python build, process reaping
can lag past that window even though the termination signal was already
sent, producing a false "could not terminate" failure during teardown
rather than an actual leaked process.
Widen delayafterclose/delayafterterminate on the pexpect child to 0.5s
in OctaveEngine._create_repl(), giving the kernel more headroom before
isalive() is checked. This benefits every OctaveEngine consumer, not
just this kernel's own _cleanup(), which already tolerates the
exception.
Observed via oct2py's CI on the 3.14t matrix leg:
https://github.com/blink1073/oct2py/actions/runs/30259504522/job/89956026163
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #354 +/- ##
==========================================
+ Coverage 92.90% 92.93% +0.03%
==========================================
Files 6 6
Lines 437 439 +2
==========================================
+ Hits 406 408 +2
Misses 31 31
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
References
Surfaced by oct2py's CI on the
test (3.14t)matrix leg:https://github.com/blink1073/oct2py/actions/runs/30259504522/job/89956026163
Description
On the free-threaded (no-GIL) Python 3.14t build,
OctaveEngineconsumersoccasionally see
pexpect.exceptions.ExceptionPexpect: Could not terminate the childraised out ofREPLWrapper.terminate()during teardown, eventhough the Octave subprocess was in fact terminating normally.
Root cause:
ptyprocess.PtyProcess.close()closes the fd, sleepsdelayafterclose(default 0.1s), then checksisalive()before raising.Under the free-threaded build, process reaping/signal delivery appears to
be racier, so
isalive()can still reportTrueimmediately after thetermination signal was sent — a false "could not terminate", not an actual
leaked process.
This kernel's own
_cleanup()(registered viaatexit) already toleratesany exception from
repl.terminate(), so kernel shutdown itself isunaffected. But any caller that terminates the REPL directly — e.g.
oct2py's
Oct2Py.exit()/restart(), which unregister_cleanupfromatexitand callself._engine.repl.terminate()themselves — sees theexception propagate.
Changes
delayafterclose/delayafterterminateon the pexpect child to0.5s in
OctaveEngine._create_repl(), giving the kernel more headroombefore
isalive()is checked. This is the same spot that alreadyremoves the default
delaybeforesenddelay, so it benefits everyOctaveEngineconsumer rather than requiring each one to reach intorepl.childitself.tests/test_engine.py::TestCreateReplassertingboth delays are set on the constructed REPL's child.
Backwards-incompatible changes
None.
delayafterclose/delayafterterminateonly affect the tail end ofclose()'s wait-then-check sequence during teardown; going from 0.1s to0.5s in the (rare) case where the process is still shutting down does not
change behavior for any caller not currently affected by this race.
Testing
poetry run pytest: 174 passed.just typing: no issues.just pre-commit: all hooks pass.AI usage