Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

On Windows swift test does not exit until all subprocesses of the test function have exited #8389

Open
ahoppen opened this issue Mar 20, 2025 · 2 comments

Comments

@ahoppen
Copy link
Member

ahoppen commented Mar 20, 2025

When an XCTest function launches a subprocess, swift test will not exit on Windows until that subprocess has terminated. For example, the test below will keep swift test running for 15s even though the xctest bundle finishes after 1s.

This is particularly problematic because the subprocesses that swift test is waiting for are now dangling processes without a parent. As far as I can tell, they thus don’t have anyone reading their stdout and stderr and can block if their pipes fill up, putting the swift test invocation into an infinite hang. We have seen SourceKit-LSP tests appear to run for days in CI because of this.

import XCTest
import Foundation

class MyTests: XCTestCase {
    func testSubprocessKeepsRunningInBackground() async throws {
        let subprocess = Process()
        // Use `ping` as a dummy process that takes a while to finish
        subprocess.executableURL = URL(fileURLWithPath: #"C:\Windows\System32\ping.exe"#)
        subprocess.arguments = ["1.1.1.1", "-n", "15"]
        try subprocess.run()
        try await Task.sleep(for: .seconds(1))
    }
}
@ahoppen
Copy link
Member Author

ahoppen commented Mar 20, 2025

Looking at this again, on macOS swift test also waits for all subprocesses to terminate. It just prints the test results (how many tests passed etc) when all the test functions have returned and before all subprocesses have finished, while on Windows the test results are only printed when all subprocesses have terminated.

Reproducer on macOS

import Foundation
import XCTest

class MyTests: XCTestCase {
    func testSubprocessKeepsRunningInBackground() async throws {
        Task {
            let subprocess = Process()
            subprocess.executableURL = URL(fileURLWithPath: #"/bin/sleep"#)
            subprocess.arguments = ["15"]
            try subprocess.run()
            subprocess.waitUntilExit()
        }
        try await Task.sleep(for: .seconds(1))
    }
}

@jakepetroules
Copy link
Contributor

Note that waitUntilExit() is unsafe to call in an asynchronous context.

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

No branches or pull requests

2 participants