@@ -233,7 +233,8 @@ package struct DiagnoseCommand: AsyncParsableCommand {
233233 throw GenericError ( " Failed to create log.txt " )
234234 }
235235 let fileHandle = try FileHandle ( forWritingTo: outputFileUrl)
236- var bytesCollected = 0
236+ let bytesCollected = AtomicInt32 ( initialValue: 0 )
237+ let processExited = AtomicBool ( initialValue: false )
237238 // 50 MB is an average log size collected by sourcekit-lsp diagnose.
238239 // It's a good proxy to show some progress indication for the majority of the time.
239240 let expectedLogSize = 50_000_000
@@ -247,21 +248,28 @@ package struct DiagnoseCommand: AsyncParsableCommand {
247248 " --signpost " ,
248249 ] ,
249250 outputRedirection: . stream(
250- stdout: { bytes in
251+ stdout: { @ Sendable bytes in
251252 try ? fileHandle. write ( contentsOf: bytes)
252- bytesCollected += bytes. count
253- var progress = Double ( bytesCollected) / Double( expectedLogSize)
253+ bytesCollected. value += Int32 ( bytes. count)
254+ var progress = Double ( bytesCollected. value ) / Double( expectedLogSize)
254255 if progress > 1 {
255256 // The log is larger than we expected. Halt at 100%
256257 progress = 1
257258 }
258- reportProgress ( . collectingLogMessages( progress: progress) , message: " Collecting log messages " )
259+ Task ( priority: . high) {
260+ // We have launched an async task to call `reportProgress`, which means that the process might have exited
261+ // before we execute this task. To avoid overriding a more recent progress, add a guard.
262+ if !processExited. value {
263+ await reportProgress ( . collectingLogMessages( progress: progress) , message: " Collecting log messages " )
264+ }
265+ }
259266 } ,
260- stderr: { _ in }
267+ stderr: { @ Sendable _ in }
261268 )
262269 )
263270 try process. launch ( )
264271 try await process. waitUntilExit ( )
272+ processExited. value = true
265273 #endif
266274 }
267275
@@ -343,8 +351,8 @@ package struct DiagnoseCommand: AsyncParsableCommand {
343351 let process = Process (
344352 arguments: [ try swiftUrl. filePath, " --version " ] ,
345353 outputRedirection: . stream(
346- stdout: { try ? fileHandle. write ( contentsOf: $0 ) } ,
347- stderr: { _ in }
354+ stdout: { @ Sendable bytes in try ? fileHandle. write ( contentsOf: bytes ) } ,
355+ stderr: { @ Sendable _ in }
348356 )
349357 )
350358 try process. launch ( )
0 commit comments