Skip to content

Commit 8d28ed1

Browse files
authored
Merge pull request swiftlang#1919 from ahoppen/output-redirection-sendable
Mark all closures in the `.stream` outputRedirection of TSC as `@Sendable`
2 parents 127f1c3 + 7fffc04 commit 8d28ed1

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -653,8 +653,8 @@ package actor SwiftPMBuildSystem: BuiltInBuildSystem {
653653
arguments: arguments,
654654
workingDirectory: nil,
655655
outputRedirection: .stream(
656-
stdout: { stdoutHandler.handleDataFromPipe(Data($0)) },
657-
stderr: { stderrHandler.handleDataFromPipe(Data($0)) }
656+
stdout: { @Sendable bytes in stdoutHandler.handleDataFromPipe(Data(bytes)) },
657+
stderr: { @Sendable bytes in stderrHandler.handleDataFromPipe(Data(bytes)) }
658658
)
659659
)
660660
let exitStatus = result.exitStatus.exhaustivelySwitchable

Sources/Diagnose/DiagnoseCommand.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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()

Sources/SemanticIndex/UpdateIndexStoreTaskDescription.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,8 @@ package struct UpdateIndexStoreTaskDescription: IndexTaskDescription {
404404
arguments: processArguments,
405405
workingDirectory: workingDirectory,
406406
outputRedirection: .stream(
407-
stdout: { stdoutHandler.handleDataFromPipe(Data($0)) },
408-
stderr: { stderrHandler.handleDataFromPipe(Data($0)) }
407+
stdout: { @Sendable bytes in stdoutHandler.handleDataFromPipe(Data(bytes)) },
408+
stderr: { @Sendable bytes in stderrHandler.handleDataFromPipe(Data(bytes)) }
409409
)
410410
)
411411
}

0 commit comments

Comments
 (0)