Skip to content

[6.2][Concurrency] Move tsan_release to point before task gets destroyed #82719

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

Conversation

DanBlackwell
Copy link
Contributor

Description: runJobInEstablishedExecutorContext contains an erroneous _tsan_release, which can mark memory that has already been freed as a synchronisation point for other threads. This patch moves the _tsan_release to a point before the deallocation.
Risk: Low, this change only affects thread sanitizer.
Testing: This issue was discovered by a failing debug check within TSan when applied to a large Xcode project. The debug check no longer fails with this patch applied; unfortunately any tests that would run in Swift CI would not have this debug check, as a release build of the TSan runtime is used.
Reviewed by: @ktoso

Original PR: #82158
Radar: rdar://152501929

@DanBlackwell DanBlackwell requested a review from a team as a code owner July 2, 2025 13:18
Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

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

It looks to me like the tsan acquire/release aren't balanced for jobs that aren't async tasks (e.g., one-off jobs that we create elsewhere in the runtime). Am I missing something?

@@ -256,8 +256,6 @@ void swift::runJobInEstablishedExecutorContext(Job *job) {
#if SWIFT_OBJC_INTEROP
objc_autoreleasePoolPop(pool);
#endif

_swift_tsan_release(job);
Copy link
Member

Choose a reason for hiding this comment

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

This operates on every kind of job, including async tasks...

@@ -859,6 +859,8 @@ struct AsyncTask::PrivateStorage {
}
}

_swift_tsan_release(task);
Copy link
Member

Choose a reason for hiding this comment

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

This only applies to async tasks.

@DanBlackwell
Copy link
Contributor Author

It looks to me like the tsan acquire/release aren't balanced for jobs that aren't async tasks (e.g., one-off jobs that we create elsewhere in the runtime). Am I missing something?

These 'acquire' and 'release' are referring to memory ordering, rather than reference-counting acquire and release. These don't necessarily have to be balanced; though we should check that there are also appropriate 'release' and 'acquire' calls when a Task gets interrupted and resumed.

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.

3 participants