Skip to content

PackageToJS: Fail tests when continuation leaks are detected #365

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

Merged
merged 2 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// swift-tools-version: 6.0
import PackageDescription

let package = Package(
name: "Check",
dependencies: [.package(name: "JavaScriptKit", path: "../../../../../")],
targets: [
.testTarget(
name: "CheckTests",
dependencies: [
"JavaScriptKit",
.product(name: "JavaScriptEventLoopTestSupport", package: "JavaScriptKit"),
],
path: "Tests"
)
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Testing

@Test func never() async throws {
let _: Void = await withUnsafeContinuation { _ in }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// swift-tools-version: 6.0
import PackageDescription

let package = Package(
name: "Check",
dependencies: [.package(name: "JavaScriptKit", path: "../../../../../")],
targets: [
.testTarget(
name: "CheckTests",
dependencies: [
"JavaScriptKit",
.product(name: "JavaScriptEventLoopTestSupport", package: "JavaScriptKit"),
],
path: "Tests"
)
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import XCTest

final class CheckTests: XCTestCase {
func testNever() async throws {
let _: Void = await withUnsafeContinuation { _ in }
}
}
13 changes: 13 additions & 0 deletions Plugins/PackageToJS/Templates/bin/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ const harnesses = {
options = prelude.setupOptions(options, { isMainThread: true })
}
}
process.on("beforeExit", () => {
// NOTE: "beforeExit" is fired when the process exits gracefully without calling `process.exit`
// Either XCTest or swift-testing should always call `process.exit` through `proc_exit` even
// if the test succeeds. So exiting gracefully means something went wrong (e.g. withUnsafeContinuation is leaked)
// Therefore, we exit with code 1 to indicate that the test execution failed.
console.error(`

=================================================================================================
Detected that the test execution ended without a termination signal from the testing framework.
Hint: This typically means that a continuation leak occurred.
=================================================================================================`)
process.exit(1)
})
await instantiate(options)
} catch (e) {
if (e instanceof WebAssembly.CompileError) {
Expand Down
37 changes: 32 additions & 5 deletions Plugins/PackageToJS/Tests/ExampleTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ extension Trait where Self == ConditionTrait {
atPath: destinationPath.path,
withDestinationPath: linkDestination
)
enumerator.skipDescendants()
continue
}

Expand Down Expand Up @@ -117,8 +116,11 @@ extension Trait where Self == ConditionTrait {
typealias RunProcess = (_ executableURL: URL, _ args: [String], _ env: [String: String]) throws -> Void
typealias RunSwift = (_ args: [String], _ env: [String: String]) throws -> Void

func withPackage(at path: String, body: (URL, _ runProcess: RunProcess, _ runSwift: RunSwift) throws -> Void) throws
{
func withPackage(
at path: String,
assertTerminationStatus: (Int32) -> Bool = { $0 == 0 },
body: @escaping (URL, _ runProcess: RunProcess, _ runSwift: RunSwift) throws -> Void
) throws {
try withTemporaryDirectory { tempDir, retain in
let destination = tempDir.appending(path: Self.repoPath.lastPathComponent)
try Self.copyRepository(to: destination)
Expand All @@ -139,11 +141,11 @@ extension Trait where Self == ConditionTrait {

try process.run()
process.waitUntilExit()
if process.terminationStatus != 0 {
if !assertTerminationStatus(process.terminationStatus) {
retain = true
}
try #require(
process.terminationStatus == 0,
assertTerminationStatus(process.terminationStatus),
"""
Swift package should build successfully, check \(destination.appending(path: path).path) for details
stdout: \(stdoutPath.path)
Expand Down Expand Up @@ -275,4 +277,29 @@ extension Trait where Self == ConditionTrait {
)
}
}

@Test(.requireSwiftSDK)
func continuationLeakInTest_XCTest() throws {
let swiftSDKID = try #require(Self.getSwiftSDKID())
try withPackage(
at: "Plugins/PackageToJS/Fixtures/ContinuationLeakInTest/XCTest",
assertTerminationStatus: { $0 != 0 }
) { packageDir, _, runSwift in
try runSwift(["package", "--disable-sandbox", "--swift-sdk", swiftSDKID, "js", "test"], [:])
}
}

#if compiler(>=6.1)
// TODO: Remove triple restriction once swift-testing is shipped in p1-threads SDK
@Test(.requireSwiftSDK(triple: "wasm32-unknown-wasi"))
func continuationLeakInTest_SwiftTesting() throws {
let swiftSDKID = try #require(Self.getSwiftSDKID())
try withPackage(
at: "Plugins/PackageToJS/Fixtures/ContinuationLeakInTest/SwiftTesting",
assertTerminationStatus: { $0 != 0 }
) { packageDir, _, runSwift in
try runSwift(["package", "--disable-sandbox", "--swift-sdk", swiftSDKID, "js", "test"], [:])
}
}
#endif
}