@@ -71,34 +71,42 @@ public final class Terminal: TerminalType, @unchecked Sendable {
7171 continuation = cont
7272 }
7373
74- Task { [ continuation, self ] in
74+ Task { [ continuation, process, self ] in
75+ _ = self
7576 let notificationCenter = NotificationCenter . default
7677 let notifications = notificationCenter. notifications (
7778 named: FileHandle . readCompletionNotification,
7879 object: outputPipe. fileHandleForReading
7980 )
8081 for await notification in notifications {
8182 let userInfo = notification. userInfo
83+ guard let object = notification. object as? FileHandle ,
84+ object === outputPipe. fileHandleForReading
85+ else {
86+ continue
87+ }
8288 if let data = userInfo ? [ NSFileHandleNotificationDataItem] as? Data ,
8389 let content = String ( data: data, encoding: . utf8) ,
8490 !content. isEmpty
8591 {
8692 continuation? . yield ( content)
8793 }
88- if !( self . process? . isRunning ?? false ) {
89- let reason = self . process? . terminationReason ?? . exit
90- let status = self . process? . terminationStatus ?? 1
91- if let output = ( self . process? . standardOutput as? Pipe ) ? . fileHandleForReading
92- . readDataToEndOfFile ( ) ,
93- let content = String ( data: output, encoding: . utf8) ,
94+ if !process. isRunning {
95+ if let fileHandle = ( process. standardOutput as? Pipe ) ?
96+ . fileHandleForReading,
97+ let data = try ? fileHandle. readToEnd ( ) ,
98+ let content = String ( data: data, encoding: . utf8) ,
9499 !content. isEmpty
95100 {
96101 continuation? . yield ( content)
97102 }
98103
104+ let status = process. terminationStatus
105+
99106 if status == 0 {
100107 continuation? . finish ( )
101108 } else {
109+ let reason = process. terminationReason
102110 continuation? . finish ( throwing: TerminationError (
103111 reason: reason,
104112 status: status
0 commit comments