44import hashlib
55import json
66import http .client
7+ import os
78import plistlib
89from pathlib import Path
910import subprocess
11+ import signal
1012import sys
1113import tempfile
1214import threading
@@ -527,6 +529,19 @@ def test_controller_error_retains_stderr_and_partial_command(self):
527529 self .assertEqual (receipt ['status' ], 'failed' )
528530 self .assertEqual (receipt ['exitCode' ], 1 )
529531
532+ def test_eof_controller_can_finish_its_native_failure_without_a_signal (self ):
533+ controller = self .controller ('print("native failed",file=sys.stderr,flush=True);os.close(sys.stdout.fileno());sys.stdin.read();sys.exit(1)' )
534+ with self .assertRaises (ValueError ): controller .request ('activate' )
535+ self .assertIsNone (controller .process .poll ())
536+ self .assertEqual (os .getpgid (controller .process .pid ), controller .process .pid )
537+ with patch .object (runner .os , 'killpg' , wraps = runner .os .killpg ) as signal_group :
538+ controller .close ()
539+ signal_group .assert_not_called ()
540+ receipt = json .loads ((self .directory / f'{ controller .prefix } .json' ).read_text ())
541+ self .assertEqual (receipt ['status' ], 'failed' )
542+ self .assertEqual (receipt ['exitCode' ], 1 )
543+ self .assertIn ('native failed' , (self .directory / f'{ controller .prefix } .stderr.log' ).read_text ())
544+
530545 def test_request_preserves_native_failure_when_command_evidence_and_stderr_fail (self ):
531546 for failing_write in ['receipt' , 'stdout' ]:
532547 with self .subTest (failing_write = failing_write ):
@@ -553,29 +568,30 @@ def test_command_evidence_failure_alone_cannot_pass(self):
553568 self .assertEqual (controller .record ['commands' ][0 ]['status' ], 'failed' )
554569
555570 def test_denied_controller_cleanup_is_not_retried_and_still_closes_streams (self ):
556- controller = self .controller ()
571+ controller = self .controller ('print(json.dumps(result),flush=True);time.sleep(30);sys.exit(0)' )
572+ controller .request ('info' )
557573 controller .failed = True
558574 denied = PermissionError (errno .EPERM , 'owned process group signal denied' )
559575 with patch .object (runner .os , 'killpg' , side_effect = denied ) as stop :
560- with self .assertRaises (PermissionError ) as captured :
561- controller .close ()
562- self .assertIs (captured .exception , denied )
576+ with self .assertRaises (subprocess .TimeoutExpired ) as captured :
577+ controller .close (timeout = 0.02 )
563578 self .assertEqual (stop .call_count , 1 )
579+ self .assertEqual (stop .call_args .args , (controller .process .pid , signal .SIGTERM ))
564580 self .assertTrue (controller .process .stdin .closed )
565581 self .assertTrue (controller .process .stdout .closed )
566582 self .assertTrue (controller .stderr .closed )
567- # The real child receives EOF; no permission fallback or second signal.
568- self .assertEqual (controller .process .wait (timeout = 3 ), 0 )
569583 receipt = json .loads ((self .directory / f'{ controller .prefix } .json' ).read_text ())
570584 self .assertEqual (receipt ['status' ], 'failed' )
571- self .assertEqual (receipt ['error' ], str (denied ))
585+ self .assertEqual (receipt ['error' ], str (captured .exception ))
586+ self .assertEqual (receipt ['cleanupErrors' ], [{'stage' : 'controller' , 'error' : str (denied )}])
572587 observation = receipt ['cleanupProcess' ]
573588 self .assertEqual (observation ['pid' ], controller .process .pid )
574589 self .assertEqual (observation ['expectedPgid' ], controller .process .pid )
575590 self .assertEqual (observation ['observedPgid' ], controller .process .pid )
576591 self .assertIsNone (observation ['returnCodeBeforeStop' ])
577592 self .assertEqual (observation ['failure' ]['errno' ], errno .EPERM )
578593 self .assertEqual (observation ['failure' ]['stage' ], 'SIGTERM' )
594+ runner .stop_owned (controller .process )
579595
580596 def test_close_preserves_wait_failure_over_signal_receipt_and_stderr_errors (self ):
581597 controller = self .controller ()
0 commit comments