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

[SR-5320] Cannot get result of complex operation from DispatchQueue on Ubuntu #687

Closed
swift-ci opened this issue Jun 27, 2017 · 2 comments
Closed

Comments

@swift-ci
Copy link

Previous ID SR-5320
Radar None
Original Reporter fwgreen (JIRA User)
Type Bug
Status Closed
Resolution Invalid

Attachment: Download

Environment

Ubuntu 16.04, Swift 3.1.1

Additional Detail from JIRA
Votes 0
Component/s libdispatch
Labels Bug
Assignee None
Priority Medium

md5: 157f897fcdabad3f7104261c7d2989db

Issue Description:

Complex operations within a DispatchQueue seem to not execute on Ubuntu. The code below runs fine on macOS but fails to return the resultLength calculation from the DispatchQueue on Ubuntu 16.04. There are neither errors nor warnings during either compilation or execution. Sadly, I cannot come up with a smaller reproducible example as then the program will work. I'm using a part of the Benchmark game's Regex-Redux challenge: http://benchmarksgame.alioth.debian.org/

import Foundation
import Dispatch

let input = FileHandle.standardInput.readDataToEndOfFile()

var sequence = String(data: input, encoding: .utf8)!

let inputLength = input.count

let regex: (String) -> NSRegularExpression = { pattern in
  return try! NSRegularExpression(pattern: pattern, options: [])
}

sequence = regex(">[^\n]*\n|\n").stringByReplacingMatches(in: sequence, options: [], range: NSRange(location: 0, length: inputLength), withTemplate: "")

var resultLength: Int?

DispatchQueue.global(qos: .background).async {
  resultLength = [
    (regex: "tHa[Nt]",            replacement: "<4>"),
    (regex: "aND|caN|Ha[DS]|WaS", replacement: "<3>"),
    (regex: "a[NSt]|BY",          replacement: "<2>"),
    (regex: "<[^>]*>",            replacement: "|"),
    (regex: "\\|[^|][^|]*\\|",    replacement: "-")
  ].reduce(sequence) { buffer, iub in
    return regex(iub.regex).stringByReplacingMatches(in: buffer, options: [], range: NSRange(location: 0, length: buffer.utf16.count), withTemplate: iub.replacement)
  }.utf8.count
}

print("",  resultLength!, separator: "\n")
@swift-ci
Copy link
Author

Comment by David Grove (JIRA)

The program snippet is missing synchronization between the print statement that is reading resultLength and the async task submitted to the global queue that writes it. For example, create a DispatchGroup instance dg, pass as an argument to the async, and then wait on the group before printing.

@swift-ci
Copy link
Author

Comment by Francois Green (JIRA)

Thank you for the solution @david Grove. I suppose on macOS I've been consistently lucky.

DispatchQueue.global(qos: .background).async {
  group.enter()
  resultLength = [
    (regex: "tHa[Nt]",            replacement: "<4>"),
    (regex: "aND|caN|Ha[DS]|WaS", replacement: "<3>"),
    (regex: "a[NSt]|BY",          replacement: "<2>"),
    (regex: "<[^>]*>",            replacement: "|"),
    (regex: "\\|[^|][^|]*\\|",    replacement: "-")
  ].reduce(sequence) { buffer, iub in
    return regex(iub.regex).stringByReplacingMatches(in: buffer, options: [], range: NSRange(location: 0, length: buffer.utf16.count), withTemplate: iub.replacement)
  }.utf8.count
  group.leave()
}

group.wait()
print("",  resultLength!, separator: "\n")

Thanks again.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant