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

MacOS backend can trigger UB if a Window is transferred between threads then had functions on NSView called. #873

Closed
goddessfreya opened this issue May 19, 2019 · 4 comments
Labels
B - bug Dang, that shouldn't have happened D - hard Likely harder than most tasks here DS - macos H - help wanted Someone please save us P - high Vital to have S - api Design and usability

Comments

@goddessfreya
Copy link
Contributor

goddessfreya commented May 19, 2019

As stated in the lengthy title, the MacOS backend can trigger UB if a Window is made from a non-main thread, or transferred between threads then had functions on NSView called. (Where the latter might not necessarily be happening, although I would not be suprized if it is, as I haven't looked too hard at the code.)

According to the cocoa docs:

Main Thread Only Classes

The following classes must be used only from the main thread of an application.

  • NSView and all of its descendants. For more information, see NSView Restrictions.

NSView Restrictions

The NSView class is generally not thread-safe. You should create, destroy, resize, move, and perform other operations on NSView objects only from the main thread of an application. Drawing from secondary threads is thread-safe as long as you bracket drawing calls with calls to lockFocusIfCanDraw and unlockFocus.

Yet for some reason, not only does winit create NSViews on non-main threads when creating a window, but somehow we for some reason implement Send + Sync on Window!

I could swear that the glutin docs mentioned at least in one place that the window isn't always Send or Sync, but after grepping the whole source I can't figure out where. Nevertheless, this discrepancy between what should be and what is has lead to numerous people asking about it - or maybe I just bug too many people about it.

@goddessfreya goddessfreya added B - bug Dang, that shouldn't have happened H - help wanted Someone please save us DS - macos S - api Design and usability D - hard Likely harder than most tasks here P - high Vital to have labels May 19, 2019
@goddessfreya goddessfreya changed the title MacOS backend can trigger UB if window is made from non-main thread. MacOS backend can trigger UB if window is made from non-main thread, or transfered between threads then had functions on NSView called. (Where the latter might not necessarily be happening, although I would not be suprized if it is) May 19, 2019
@goddessfreya goddessfreya changed the title MacOS backend can trigger UB if window is made from non-main thread, or transfered between threads then had functions on NSView called. (Where the latter might not necessarily be happening, although I would not be suprized if it is) MacOS backend can trigger UB if a Window is made from non-main thread, or transfered between threads then had functions on NSView called. May 19, 2019
@goddessfreya goddessfreya changed the title MacOS backend can trigger UB if a Window is made from non-main thread, or transfered between threads then had functions on NSView called. MacOS backend can trigger UB if a Window is made from a non-main thread, or transfered between threads then had functions on NSView called. May 19, 2019
@goddessfreya goddessfreya changed the title MacOS backend can trigger UB if a Window is made from a non-main thread, or transfered between threads then had functions on NSView called. MacOS backend can trigger UB if a Window is made from a non-main thread, or transferred between threads then had functions on NSView called. May 19, 2019
@goddessfreya goddessfreya mentioned this issue May 20, 2019
@goddessfreya
Copy link
Contributor Author

Might not necessarily be the case, as pointed out by @mtak-. #871 (comment)

@ghost
Copy link

ghost commented Jun 14, 2019

As of eventloop-2.0, doesn't winit ensure here that windows can only be created from the main thread?

@Osspial
Copy link
Contributor

Osspial commented Jun 14, 2019

@aleksijuvani That's a good point. Also, since EventLoop is neither Send nor Sync, I'm not sure it's even possible to create a window off the EventLoop thread.

@goddessfreya goddessfreya changed the title MacOS backend can trigger UB if a Window is made from a non-main thread, or transferred between threads then had functions on NSView called. MacOS backend can trigger UB if a Window is transferred between threads then had functions on NSView called. Jun 14, 2019
@francesca64
Copy link
Member

Closing this, since all the magic I did with GCD (which has since been improved by others!) made this a non-issue as of EL2's release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B - bug Dang, that shouldn't have happened D - hard Likely harder than most tasks here DS - macos H - help wanted Someone please save us P - high Vital to have S - api Design and usability
Development

No branches or pull requests

3 participants