Skip to content

[Concurrency] Fix ObjC weak references to actors. #42219

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
Apr 8, 2022

Conversation

mikeash
Copy link
Contributor

@mikeash mikeash commented Apr 6, 2022

Change actor destruction to call swift_deallocClassInstance instead of swift_deallocObject. When ObjC interop is enabled, swift_deallocClassInstance will check the "pure swift deallocation" bit and call into the ObjC runtime to destruct the instance when needed. This is what clears weak references and associated objects.

rdar://91270492

@mikeash mikeash requested review from DougGregor and rokhinip April 6, 2022 19:29
// Test that instances of Swift actors can be referenced using ObjC
// weak references.

actor A {
Copy link
Member

Choose a reason for hiding this comment

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

You will probably need some SwiftStdlib 5.1 availability guards on these

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. I should probably move this to a separate file too so we can put REQUIRES: concurrency only on this test.

@@ -1208,8 +1208,8 @@ void DefaultActorImpl::deallocateUnconditional() {
if (JobStorageHeapObject.metadata != nullptr)
JobStorage.~ProcessInlineJob();
auto metadata = cast<ClassMetadata>(this->metadata);
swift_deallocObject(this, metadata->getInstanceSize(),
metadata->getInstanceAlignMask());
swift_deallocClassInstance(this, metadata->getInstanceSize(),
Copy link
Member

Choose a reason for hiding this comment

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

Well, that's subtle! Very good catch.

Change actor destruction to call swift_deallocClassInstance instead of swift_deallocObject. When ObjC interop is enabled, swift_deallocClassInstance will check the "pure swift deallocation" bit and call into the ObjC runtime to destruct the instance when needed. This is what clears weak references and associated objects.

rdar://91270492
@mikeash
Copy link
Contributor Author

mikeash commented Apr 7, 2022

@swift-ci please test

Copy link
Contributor

@ktoso ktoso left a comment

Choose a reason for hiding this comment

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

nice catch

@mikeash mikeash merged commit 4399670 into swiftlang:main Apr 8, 2022
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