Skip to content

Instantly share code, notes, and snippets.

@marcpalmer
Created September 7, 2020 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcpalmer/ca97e6e56544d4cd869b2ba9b1a1c97f to your computer and use it in GitHub Desktop.
Save marcpalmer/ca97e6e56544d4cd869b2ba9b1a1c97f to your computer and use it in GitHub Desktop.
Example of infinite recursion crash when asking for URL.bookmarkData in a unit test
///
/// Example of infinite recursion crash when asking for URL.bookmarkData in a unit test,
/// from a non-main queue.
///
import XCTest
class BookmarkDataIssueTests: XCTestCase {
func testExample() throws {
let dir = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
let fileURL = dir.appendingPathComponent(UUID().uuidString)
let fileData = "Hello, world".data(using: .utf8)!
try fileData.write(to: fileURL)
// This works fine
do {
let _ = try fileURL.bookmarkData()
} catch let error {
XCTFail("Couldn't get bookmark data: \(error)")
}
// This crashes inside Foundation
let asyncBookmark = expectation(description: "asyncBookmark")
DispatchQueue.global().async {
do {
let _ = try fileURL.bookmarkData()
} catch let error {
XCTFail("Couldn't get bookmark data: \(error)")
}
asyncBookmark.fulfill()
}
wait(for: [asyncBookmark], timeout: 10)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment