Skip to content

[Windows] Fix thread local storage cleanup #2902

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

Merged
merged 1 commit into from
Oct 26, 2020

Conversation

lxbndr
Copy link
Contributor

@lxbndr lxbndr commented Oct 16, 2020

CoreFoundation thread local storage on Windows is based on FLS API. FLS provides automatic cleanup of storage, and it is already got used. Old manual cleanup is not needed anymore.

We have observed often crashes in thread cleanup routines of CRT. Despite I didn't manage to extract the issue out of live project code, I suppose that mixing TLS and FLS in cleanup code could eventually corrupt the storage. Replacing TlsGetValue with FlsGetValue brings desired stability, but, as the code is obsolete, we could just remove it.

@lxbndr
Copy link
Contributor Author

lxbndr commented Oct 16, 2020

CC @compnerd

@compnerd
Copy link
Member

Thanks for finding and fixing this. Yes, the CoreFoundation implementation switched to Fls* to get a destructor-enabled thread local implementation. This should have been changed to FlsGetValue when that change was made.

@compnerd
Copy link
Member

@swift-ci please test

@lxbndr
Copy link
Contributor Author

lxbndr commented Oct 17, 2020

@compnerd BTW do we still need __CFFinalizeWindowsThreadData? It is called from DllMain on DLL_THREAD_DETACH event. That was needed for TLS-based implementation, but FLS provides destructor mechanics. I see that __CFTSDFinalize is actually called twice. First time is from __CFFinalizeWindowsThreadData. I guess second call is from FLS itself.

I could make sure it is actual behavior and remove __CFFinalizeWindowsThreadData in this patch, instead of fixing it. WDYT?

@compnerd
Copy link
Member

Hmm, if the DLL_THREAD_DETACH is only doing this to handle the cleanup on the unload, we should remove it as the FLS will be sufficient for the cleanup.

@lxbndr
Copy link
Contributor Author

lxbndr commented Oct 20, 2020

I will run extended tests to make sure everything is ok without DLL_THREAD_DETACH cleanup. Converting this to draft for now.

@lxbndr lxbndr marked this pull request as draft October 20, 2020 15:50
Foundation uses Fls now. Fls provides automatic cleanup, and
manual finalization on DLL_THREAD_DETACH is not needed.
@lxbndr
Copy link
Contributor Author

lxbndr commented Oct 24, 2020

Seems like everything is ok and we could just remove old finalization instead of fixing it. Did not run into any new issues for last days.

@lxbndr lxbndr marked this pull request as ready for review October 24, 2020 16:04
@lxbndr lxbndr requested a review from compnerd October 24, 2020 16:04
@compnerd
Copy link
Member

@swift-ci please test

@compnerd
Copy link
Member

CC: @millenomi any objections to the change to CF here?

@millenomi millenomi merged commit 6787b8a into swiftlang:main Oct 26, 2020
@lxbndr lxbndr deleted the tls-cleanup branch October 26, 2020 23:21
@spevans
Copy link
Contributor

spevans commented Oct 28, 2020

Is this bugfix worth backporting to 5.3?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants