[SR-15686] Dispatch on Linux incorrectly inherits random thread names
weissi opened this issue · 1 comments
Previous ID | SR-15686 |
Radar | rdar://problem/87139515 |
Original Reporter | @weissi |
Type | Bug |
Environment
all Swift versions (incl. 5.4, 5.5 and the current main
snapshot (Swift version 5.6-dev (LLVM c8e0f2fe28693ea, Swift 68eb340a2b7690f))) that I've tried on Linux.
Additional Detail from JIRA
Votes | 0 |
Component/s | libdispatch |
Labels | Bug, linux |
Assignee | None |
Priority | Medium |
md5: 908cecea496804cb997a5bfc0ef74c2b
Issue Description:
On Linux, Dispatch incorrectly inherits essentially random thread names which makes debugging certain issues very hard. Usually, threads get labelled for a good reason, so it's pretty bad if that thread name then gets duplicated onto other threads.
Consider this test program which spawns one Thread
and then sets its label to "MyThread". Subsequently it dispatches some work to some DispatchGroup
and prints the dispatch queue's thread name (which should either be something chosen by Dispatch (ideal) or empty or maybe the program name but certainly not "MyThread").
import Foundation
class MyThread: Thread {
let group: DispatchGroup
init(group: DispatchGroup) {
self.group = group
}
override func main() {
Thread.current.name = "MyThread"
print("[on thread] thread name is", Thread.current.name ?? "n/a")
for f in 0..<10 {
DispatchQueue.global().async(group: self.group) {
print("[on global queue \(f)] thread name is", Thread.current.name ?? "n/a")
}
}
self.group.leave()
}
}
let group = DispatchGroup()
let t = MyThread(group: group)
group.enter()
t.start()
group.wait()
On Darwin, this program correctly prints
$ swift test.swift
[on thread] thread name is MyThread
[on global queue 0] thread name is
[on global queue 1] thread name is
[on global queue 3] thread name is
[on global queue 4] thread name is
[on global queue 6] thread name is
[on global queue 7] thread name is
[on global queue 5] thread name is
[on global queue 9] thread name is
[on global queue 2] thread name is
[on global queue 8] thread name is
ie, the thread names of the DispatchQueues are empty.
On Linux however, we see
$ docker run -it --rm -v "$PWD:$PWD" -w "$PWD" swift:5.5 swift test.swift
[on thread] thread name is MyThread
[on global queue 0] thread name is MyThread
[on global queue 2] thread name is MyThread
[on global queue 3] thread name is MyThread
[on global queue 4] thread name is MyThread
[on global queue 5] thread name is MyThread
[on global queue 6] thread name is MyThread
[on global queue 8] thread name is MyThread
[on global queue 1] thread name is MyThread
[on global queue 9] thread name is MyThread
[on global queue 7] thread name is MyThread
so "MyThread" got incorrectly inherited.
This got first discovered here: apple/swift-nio#1896 (I incorrectly filed it as a SwiftNIO issue).