From 9925e403ca5ebed1356e8e62f51b72f6074c43f2 Mon Sep 17 00:00:00 2001 From: Giovanny Aranda Date: Tue, 12 Sep 2023 12:15:08 +0200 Subject: [PATCH] Fix for Process.run() fails with "Bad file descriptor" and Swift 5.8 --- Package.swift | 2 +- Sources/wkhtmltopdf/Document+Generate.swift | 5 +++-- Tests/wkhtmltopdfTests/wkhtmltopdfTests.swift | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Package.swift b/Package.swift index 8cbabbf..abbf6c5 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.5 +// swift-tools-version:5.8 import PackageDescription let package = Package( diff --git a/Sources/wkhtmltopdf/Document+Generate.swift b/Sources/wkhtmltopdf/Document+Generate.swift index 611dc36..3ec4544 100644 --- a/Sources/wkhtmltopdf/Document+Generate.swift +++ b/Sources/wkhtmltopdf/Document+Generate.swift @@ -40,14 +40,15 @@ extension Document { // Call wkhtmltopdf and retrieve the result data let wk = Process() let stdout = Pipe() - wk.launchPath = self.launchPath + wk.executableURL = URL(fileURLWithPath: self.launchPath) wk.arguments = wkArgs wk.arguments?.append("-") // output to stdout wk.standardOutput = stdout - wk.launch() + try wk.run() let pdf = stdout.fileHandleForReading.readDataToEndOfFile() continuation.resume(returning: pdf) + try stdout.fileHandleForReading.close() }.flatMapErrorThrowing { err in continuation.resume(throwing: err) return diff --git a/Tests/wkhtmltopdfTests/wkhtmltopdfTests.swift b/Tests/wkhtmltopdfTests/wkhtmltopdfTests.swift index c43d611..f73c56c 100644 --- a/Tests/wkhtmltopdfTests/wkhtmltopdfTests.swift +++ b/Tests/wkhtmltopdfTests/wkhtmltopdfTests.swift @@ -28,7 +28,7 @@ class wkhtmltopdfTests: XCTestCase { let threadPool = NIOThreadPool(numberOfThreads: 1) threadPool.start() let data = try await document.generatePDF(on: threadPool, eventLoop: eventLoop) - try threadPool.syncShutdownGracefully() + try await threadPool.shutdownGracefully() // Cop-out test, just ensuring that the returned data is something XCTAssert(data.count > 50) // Visual test