@@ -233,7 +233,8 @@ package struct DiagnoseCommand: AsyncParsableCommand {
233
233
throw GenericError ( " Failed to create log.txt " )
234
234
}
235
235
let fileHandle = try FileHandle ( forWritingTo: outputFileUrl)
236
- var bytesCollected = 0
236
+ let bytesCollected = AtomicInt32 ( initialValue: 0 )
237
+ let processExited = AtomicBool ( initialValue: false )
237
238
// 50 MB is an average log size collected by sourcekit-lsp diagnose.
238
239
// It's a good proxy to show some progress indication for the majority of the time.
239
240
let expectedLogSize = 50_000_000
@@ -247,21 +248,28 @@ package struct DiagnoseCommand: AsyncParsableCommand {
247
248
" --signpost " ,
248
249
] ,
249
250
outputRedirection: . stream(
250
- stdout: { bytes in
251
+ stdout: { @ Sendable bytes in
251
252
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)
254
255
if progress > 1 {
255
256
// The log is larger than we expected. Halt at 100%
256
257
progress = 1
257
258
}
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
+ }
259
266
} ,
260
- stderr: { _ in }
267
+ stderr: { @ Sendable _ in }
261
268
)
262
269
)
263
270
try process. launch ( )
264
271
try await process. waitUntilExit ( )
272
+ processExited. value = true
265
273
#endif
266
274
}
267
275
@@ -343,8 +351,8 @@ package struct DiagnoseCommand: AsyncParsableCommand {
343
351
let process = Process (
344
352
arguments: [ try swiftUrl. filePath, " --version " ] ,
345
353
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 }
348
356
)
349
357
)
350
358
try process. launch ( )
0 commit comments