Skip to content

Conversation

@owenv
Copy link
Contributor

@owenv owenv commented Oct 17, 2025

Alternative to #9191

Begin adopting swift-subprocess, beginning with test execution, in an effort to standardize on one implementation for process output streaming across platforms.

This required a bit of additional refactoring of the parallel test runner to make it async

Depends on swiftlang/swift#84947

let processConfig = Subprocess.Configuration(.path(.init(args[0])), arguments: .init(Array(args.dropFirst())), environment: .custom(env))
let result = try await Subprocess.run(processConfig, input: .none, error: .standardOutput) { execution, outputSequence in
let token = cancellator.register(name: "Test Execution", handler: { _ in
await execution.teardown(using: [.gracefulShutDown(allowedDurationToNextStep: .seconds(5))])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is unsafe. Because deregister is synchronous, it may return before the cancellation block has finished executing, meaning teardown could be called when the process is already dead. It is NOT safe to use execution once the body block has returned, as the pid may have already been recycled or the pidfd may have become invalid. This is one reason why Execution would probably ideally be ~Escapable...

Subprocess.run already response to Swift Concurrency task cancellation, so you'd want something like this:

extension Cancellator {
    func run<T: Sendable>(_ block: @escaping @Sendable () async throws -> T) async throws -> T {
        let task = Task { try await block() }
        let token = register(name: "Test Execution", handler: { _ in task.cancel() })
        defer {
            token.map { deregister($0) }
        }
        return try await task.value
    }
}

try await cancellator.run {
    try await Subprocess.run(...)
}

That said, do you even need the cancellator, or is cooperative cancellation on its own, enough?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed. I think we do need the explicit cancellation here to ensure tests can be interrupted interactively

library: .xctest // swift-testing does not use ParallelTestRunner
)
var output = ""
let outputLock = NSLock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit but also kinda seriously: I'd either use an AsyncStream or at least use a LockedValue or similar abstraction.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied this in the refactor, but looking at it more closely this lock seems unnecessary, I deleted it

Copy link
Contributor

@bkhouri bkhouri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we refactor the SwiftTestCommand so we can write 'small' or 'medium' tests, that is, we test the modified functions/code in isolation, as opposed to relying on 'large' (ie: end-to-end tests)?

Copy link
Contributor

@bkhouri bkhouri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meant to mark this as required changed instead of approve

@owenv owenv force-pushed the owenv/adopt-subprocess branch from 0a067be to d4aa4ca Compare October 20, 2025 06:05
@owenv
Copy link
Contributor Author

owenv commented Oct 20, 2025

Depends on swiftlang/swift-subprocess#201 for the CMake build

@owenv
Copy link
Contributor Author

owenv commented Oct 20, 2025

Can we refactor the SwiftTestCommand so we can write 'small' or 'medium' tests, that is, we test the modified functions/code in isolation, as opposed to relying on 'large' (ie: end-to-end tests)?

This PR isn intended to be NFC so I've been relying on the existing tests so far

@owenv owenv force-pushed the owenv/adopt-subprocess branch from d4aa4ca to 02696e4 Compare October 20, 2025 06:09
@owenv
Copy link
Contributor Author

owenv commented Oct 20, 2025

@swift-ci test

@owenv owenv force-pushed the owenv/adopt-subprocess branch from 02696e4 to e9601b4 Compare October 20, 2025 16:41
@bkhouri
Copy link
Contributor

bkhouri commented Oct 22, 2025

Can we refactor the SwiftTestCommand so we can write 'small' or 'medium' tests, that is, we test the modified functions/code in isolation, as opposed to relying on 'large' (ie: end-to-end tests)?

This PR isn intended to be NFC so I've been relying on the existing tests so far

I understand we are relying on existing tests, but we really should use the Test Pyramid to validate the code changes instead of relying on all the tests. Although the code changes may be exercises via existing tests, they tests to no guarantee we will exercise all the code paths. As such, we should try to write tests at different layers of the Test Pyramid.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants