1- import { spawn } from "node:child_process" ;
1+ import { spawn , type ChildProcess } from "node:child_process" ;
22import { appendFile , mkdir } from "node:fs/promises" ;
33import path from "node:path" ;
44import { startQaGatewayChild } from "../../../../extensions/qa-lab/api.js" ;
@@ -24,6 +24,54 @@ function logLine(message: string): string {
2424 } ) } \n`;
2525}
2626
27+ function hasExited ( child : ChildProcess ) : boolean {
28+ return child . exitCode !== null || child . signalCode !== null ;
29+ }
30+
31+ function waitForClose ( child : ChildProcess , timeoutMs : number ) : Promise < void > {
32+ if ( hasExited ( child ) ) {
33+ return Promise . resolve ( ) ;
34+ }
35+ return new Promise ( ( resolve , reject ) => {
36+ const onClose = ( ) => {
37+ clearTimeout ( timer ) ;
38+ resolve ( ) ;
39+ } ;
40+ const timer = setTimeout ( ( ) => {
41+ child . off ( "close" , onClose ) ;
42+ reject ( new Error ( "follow child did not close before timeout" ) ) ;
43+ } , timeoutMs ) ;
44+ child . once ( "close" , onClose ) ;
45+ } ) ;
46+ }
47+
48+ async function stopFollowChild ( child : ChildProcess ) : Promise < void > {
49+ if ( hasExited ( child ) ) {
50+ return ;
51+ }
52+ child . kill ( "SIGINT" ) ;
53+ try {
54+ await waitForClose ( child , 5_000 ) ;
55+ } catch {
56+ if ( hasExited ( child ) ) {
57+ return ;
58+ }
59+ child . kill ( "SIGKILL" ) ;
60+ await waitForClose ( child , 5_000 ) ;
61+ }
62+ }
63+
64+ export async function withOwnedFollowChild < T > (
65+ child : ChildProcess ,
66+ operation : ( ) => Promise < T > ,
67+ ) : Promise < T > {
68+ try {
69+ return await operation ( ) ;
70+ } finally {
71+ await stopFollowChild ( child ) ;
72+ }
73+ }
74+
2775export async function runRemoteLogTailing ( repoRoot : string , outputRoot : string ) {
2876 const logPath = path . join ( outputRoot , "gateway.jsonl" ) ;
2977 await mkdir ( outputRoot , { recursive : true } ) ;
@@ -127,32 +175,23 @@ export async function runRemoteLogTailing(repoRoot: string, outputRoot: string)
127175 child . stderr . on ( "data" , ( chunk ) => {
128176 stderr += String ( chunk ) ;
129177 } ) ;
130- await new Promise < void > ( ( resolve ) => {
131- setTimeout ( resolve , 250 ) ;
132- } ) ;
133- await appendFile ( logPath , logLine ( "qa-follow-line" ) ) ;
134- const deadline = Date . now ( ) + 10_000 ;
135- while ( ! stdout . includes ( "qa-follow-line" ) && Date . now ( ) < deadline ) {
178+ const followOutput = await withOwnedFollowChild ( child , async ( ) => {
136179 await new Promise < void > ( ( resolve ) => {
137- setTimeout ( resolve , 50 ) ;
138- } ) ;
139- }
140- if ( ! stdout . includes ( "qa-follow-line" ) ) {
141- child . kill ( "SIGINT" ) ;
142- throw new Error ( `follow did not receive appended record: ${ stderr } ` ) ;
143- }
144- child . kill ( "SIGINT" ) ;
145- await new Promise < void > ( ( resolve , reject ) => {
146- const timer = setTimeout (
147- ( ) => reject ( new Error ( "follow child did not exit after SIGINT" ) ) ,
148- 5000 ,
149- ) ;
150- child . once ( "close" , ( ) => {
151- clearTimeout ( timer ) ;
152- resolve ( ) ;
180+ setTimeout ( resolve , 250 ) ;
153181 } ) ;
182+ await appendFile ( logPath , logLine ( "qa-follow-line" ) ) ;
183+ const deadline = Date . now ( ) + 10_000 ;
184+ while ( ! stdout . includes ( "qa-follow-line" ) && Date . now ( ) < deadline ) {
185+ await new Promise < void > ( ( resolve ) => {
186+ setTimeout ( resolve , 50 ) ;
187+ } ) ;
188+ }
189+ if ( ! stdout . includes ( "qa-follow-line" ) ) {
190+ throw new Error ( `follow did not receive appended record: ${ stderr } ` ) ;
191+ }
192+ return stdout ;
154193 } ) ;
155- return { first, bounded, cursorTail, cliRecords, followOutput : stdout } ;
194+ return { first, bounded, cursorTail, cliRecords, followOutput } ;
156195 } finally {
157196 await gateway . stop ( ) ;
158197 }
0 commit comments