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-15686] Dispatch on Linux incorrectly inherits random thread names #600

Open
weissi opened this issue Jan 5, 2022 · 1 comment
Open

Comments

@weissi
Copy link
Member

weissi commented Jan 5, 2022

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).

@weissi
Copy link
Member Author

weissi commented Jan 5, 2022

@swift-ci create

@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
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