Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Fix leak calling [NSThread currentThread] when underlying thread created outside NSThread API - #1872

Merged
Dustin L. Howett (MSFT) (DHowett-MSFT) merged 7 commits into
microsoft:developfrom
ehren:external-currentthread-leak
Mar 3, 2017
Merged

Dustin L. Howett (MSFT) (DHowett-MSFT) merged 7 commits into
microsoft:developfrom
ehren:external-currentthread-leak

Conversation

@ehren

Copy link
Copy Markdown
Contributor

This is an alternative for the naive leak fix in #1834 (which introduces a use after free).

I took a look at supporting the cdecl destructor argument of pthread_key_create but it looks like any fixes there would complicate the implementation of pthread_setspecific/pthread_getspecific. So this commit just calls the Fls functions directly from NSThread.

@DHowett-MSFT

Copy link
Copy Markdown

Also in CI build 😄

Comment thread Frameworks/Foundation/NSThread.mm Outdated

// So create a new instance
currentThread = [[NSThread alloc] init];
[currentThread _associateWithCurrentThread];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, it seems like this could be wrapped up into the current thread machinery. At file scope, a thread_local StrongId<NSThread>, which _associateWith... and _threadObjectFrom... could refer to. That way, its lifetime would be managed by the same thing that keeps track of its current-threadiness.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. I'll include that + some cleanups in the next commit - I can see there's e.g. an unused pthread tls key in that file too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dustin L. Howett (MSFT) (@DHowett-MSFT) I don't think non-POD file scope thread_locals are supported by this version of clang (c2?).

e.g. this is fine at file scope

struct Blah {
    Blah() {
        abort();
    }

   ~Blah() {
        abort();
   }
};
 
thread_local Blah blah;

But even if thread_local worked the lazy approach avoids the static initialization/destruction overhead for every thread created outside the NS* APIs.

I also haven't confirmed but I think this means there's a leak from thread_local use in NSProgress: https://github.com/Microsoft/WinObjC/blob/develop/Frameworks/Foundation/NSProgress.mm#L50

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find and somewhat worrisome. Yes, with NSProgress.mm, there is a leak. s_currentProgressStack could become a thread local static inside _getProgressStackForCurrentThread much like you have done here.


// Ensure the reference grabbed above is
// the only one remaining after thread exit
EXPECT_EQ(1, [current retainCount]);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I understand how this is correct. alloc init should add a reference and currentThread retain should add one?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the body of the lambda we're not creating the NSThread via alloc init. We just get a non-owning reference by the call to |currentThread|. The call to |currentThread| does happen to create an owning reference (because it's called from a thread not created via the NSThread API), but this is relinquished on thread exit via the thread_local StrongId destructor.

I'll add some comments to the test case when I add in Dustin L. Howett (MSFT) (@DHowett-MSFT)'s suggested changes - likely sometime tonight. It might also be clearer to convert that test to ARC - but I had some difficulty attempting this initially.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rajsesh-msft there's a few more asserts in the unit test which should improve things

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would really like to see the changes suggested by Dustin L. Howett (MSFT) (@DHowett-MSFT)

StrongId with a few cleanups and removal of pthread tls function use.
Additional asserts in unit test.
Comment thread Frameworks/Foundation/NSThread.mm Outdated
static StrongId<NSThread> s_mainThread;
static BOOL s_isMultiThreaded = NO;

static NSThread* setOrGetCurrentThread(NSThread* thread) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: the private functions should be _set* per coding conventions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rajsesh-msft I'll open a pull with the NSProgress change

Comment thread Frameworks/Foundation/NSThread.mm Outdated

// So create a new instance
currentThread = [[NSThread alloc] init];
[currentThread _associateWithCurrentThread];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find and somewhat worrisome. Yes, with NSProgress.mm, there is a leak. s_currentProgressStack could become a thread local static inside _getProgressStackForCurrentThread much like you have done here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be great to get the nits addressed, but looks good otherwise.

@DHowett-MSFT

Copy link
Copy Markdown

Submitted these for CI build again.

@DHowett-MSFT

Copy link
Copy Markdown

It looks like these never completed build, but they did submit status updates... Pradip Dhara (@pradipd) do you know what happened?
I'll re-queue them in the mean time, but it was a week ago...

@rajsesh

Copy link
Copy Markdown
Contributor

Very timely Dustin L. Howett (MSFT) (@DHowett-MSFT). Take a look at #1937 that Pradip Dhara (@pradipd) just opened.

@DHowett-MSFT

Copy link
Copy Markdown

facepalm
I saw Pradip Dhara (@pradipd)'s pull request, but didn't put two and two together. Thank you!

@ehren

Copy link
Copy Markdown
Contributor Author

Pradip Dhara (@pradipd) thanks for the test fix!

I took the opportunity to push another change. No change in behavior - but this avoids creating a new thread-lifetime NSThread object unnecessarily (isMainThread is called frequently via UIView RunSynchronouslyOnMainThread)

@rajsesh

Copy link
Copy Markdown
Contributor

Back in CI build.

@DHowett-MSFT

Copy link
Copy Markdown

Back in CI build again. Sorry Ehren Metcalfe (@ehren)!

@DHowett-MSFT
Dustin L. Howett (MSFT) (DHowett-MSFT) merged commit 970bfa9 into microsoft:develop Mar 3, 2017
@DHowett-MSFT

Copy link
Copy Markdown

Thanks Ehren Metcalfe (@ehren)!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants