fix(code-runtime-python): close the child stdin handle and def-time capture the frame decode primitives

Addresses the review's two remaining items:
- The host closes the child's stdin write handle immediately after spawn. The
  program is an async body that reads nothing from fd 0; a live pipe would hold
  a host-side handle open past the run, so a setsid-escaped descendant
  inheriting fd 0 could keep the host process from exiting even after the
  closeDeadline forced settlement. The child (and any descendant) reads EOF on
  fd 0 and no host handle survives.
- read_frame/read_frame_async bind their decode primitives (_decode_json_plain,
  os.read, _READ_CHUNK_BYTES, bytes) as def-time default arguments, and
  _decode_json_plain itself captures json.loads, its two regexes, and len the
  same way, so a __main__ rebind cannot kill the reply pump and strand every
  pending Future to the wall clock. _decode_json_plain and its regexes moved
  before the ProtocolChannel class so the defaults resolve at class-definition
  time. A regression test rebinds _decode_json_plain and asserts a binding reply
  still round-trips.
Note (en + zh) registers both mechanisms; pairings re-recorded.
This commit is contained in:
Chinesezjc
2026-08-31 14:47:57 +08:00
committed by Tianyi Cui
parent ac64039843
commit 40fbf92290
6 changed files with 204 additions and 147 deletions
@@ -989,6 +989,13 @@ export class PythonCodeRuntime extends CodeRuntime {
if (proto === null) {
throw new Error('dsh-code-runtime-python: python subprocess spawned without a fd-3 pipe')
}
// Close the host's stdin write handle immediately: the program is an
// async body that reads nothing from fd 0, and a live pipe here would
// hold a host-side handle open past the run — a setsid-escaped descendant
// inheriting fd 0 would keep the host process from exiting even after the
// closeDeadline forced settlement. The child (and any descendant) reads
// EOF on fd 0 instead, and no host handle survives.
child.stdin.destroy()
} catch (error: unknown) {
try {
rmSync(bootstrapDir, { recursive: true, force: true })