PureDarwin / CoreFoundation

Built from CoreFoundation found in https://github.com/apple/swift-corelibs-foundation/tree/master/CoreFoundation
14 stars 0 forks source link

CFRunLoopGetCurrent() returns junk value #1

Open sjc opened 6 years ago

sjc commented 6 years ago

If you:

This is caused by CF retrieving a junk value from the runloop slot in the pthread thread storage.

No calls to set the runloop thread storage (TDS) value appears to be made anywhere in CF's initialization code.

copumpkin commented 5 years ago

@sjc I just fixed this upstream in a PR, after encountering the same issue in my project. See https://github.com/apple/swift-corelibs-foundation/pull/1695. Judging by your codebase, the preprocessor logic is a little different wrapping the TSD initializer, but just take out the swift runtime guard and it should work.

Also, just FYI, the TSD does get set by CFRunLoopGet0. So the "first run" logic should be:

  1. Call CFRunLoopGetCurrent
  2. CFRunLoopGetCurrent checks the TSD, which should return NULL/0
  3. CFRunLoopGetCurrent falls back to the more expensive CFRunLoopGet0 which actually gets a legit run loop, then sets the TSD with it
  4. Subsequent calls to CFRunLoopGetCurrent find the valid TSD and don't call CFRunLoopGet0 anymore

At least that's what I could make of it as I was debugging this. Either way, I've tested that the current state of the upstream CF repo gives me a junk address and after my PR, it gives me a valid CFRunLoopRef that I can use elsewhere.

sjc commented 5 years ago

@copumpkin Thanks for letting us know! I'll take a look at your fix over the weekend.